Skip to content

Commit

Permalink
fix: NPE related to home when player logs in.
Browse files Browse the repository at this point in the history
  • Loading branch information
hamusuke0323 committed Nov 10, 2023
1 parent acce510 commit 9481fbc
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 23 deletions.
4 changes: 4 additions & 0 deletions src/main/java/emu/grasscutter/data/GameData.java
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,10 @@ public final class GameData {
private static final Int2ObjectMap<HomeWorldLevelData> homeWorldLevelDataMap =
new Int2ObjectOpenHashMap<>();

@Getter
private static final Int2ObjectMap<HomeWorldModuleData> homeWorldModuleDataMap =
new Int2ObjectOpenHashMap<>();

@Getter
private static final Int2ObjectMap<HomeWorldNPCData> homeWorldNPCDataMap =
new Int2ObjectOpenHashMap<>();
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/emu/grasscutter/data/excels/HomeWorldModuleData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package emu.grasscutter.data.excels;

import emu.grasscutter.data.GameResource;
import emu.grasscutter.data.ResourceType;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.experimental.FieldDefaults;

@ResourceType(name = "HomeworldModuleExcelConfigData.json")
@FieldDefaults(level = AccessLevel.PRIVATE)
@Getter
public class HomeWorldModuleData extends GameResource {
int Id;
boolean isFree;
int worldSceneId;
int defaultRoomSceneId;
}
6 changes: 6 additions & 0 deletions src/main/java/emu/grasscutter/game/home/GameHome.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import emu.grasscutter.net.proto.HomeAvatarTalkFinishInfoOuterClass;
import emu.grasscutter.server.packet.send.*;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.IntSets;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Data;
Expand All @@ -37,6 +38,9 @@ public class GameHome {
|| sceneData.getSceneType() == SceneType.SCENE_HOME_ROOM)
.map(SceneData::getId)
.collect(Collectors.toUnmodifiableSet());
public static final Set<Integer> HOME_MODULE_IDS =
GameData.getHomeWorldModuleDataMap().isEmpty() ?
IntSets.fromTo(1, 6) : GameData.getHomeWorldModuleDataMap().keySet();

@Id String id;

Expand Down Expand Up @@ -201,6 +205,8 @@ private void fixModuleIdIfInvalid() {
return;
}

this.player.getRealmList().removeIf(integer -> !HOME_MODULE_IDS.contains(integer)); // Delete invalid module ids.

if (this.player.getRealmList().isEmpty()) {
this.player.setRealmList(null);
return;
Expand Down
36 changes: 19 additions & 17 deletions src/main/java/emu/grasscutter/game/home/HomeModuleManager.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package emu.grasscutter.game.home;

import com.github.davidmoten.guavamini.Lists;
import emu.grasscutter.game.home.suite.HomeSuiteItem;
import emu.grasscutter.game.home.suite.event.HomeAvatarRewardEvent;
import emu.grasscutter.game.home.suite.event.HomeAvatarSummonEvent;
import emu.grasscutter.game.home.suite.event.SuiteEventType;
import emu.grasscutter.game.inventory.GameItem;
import emu.grasscutter.game.player.Player;
import emu.grasscutter.net.proto.HomeAvatarRewardEventNotifyOuterClass;
import emu.grasscutter.net.proto.HomeAvatarSummonAllEventNotifyOuterClass;
import emu.grasscutter.net.proto.RetcodeOuterClass;
import emu.grasscutter.net.proto.HomeAvatarRewardEventNotifyOuterClass.HomeAvatarRewardEventNotify;
import emu.grasscutter.net.proto.HomeAvatarSummonAllEventNotifyOuterClass.HomeAvatarSummonAllEventNotify;
import emu.grasscutter.net.proto.RetcodeOuterClass.Retcode;
import emu.grasscutter.server.packet.send.PacketHomeAvatarSummonAllEventNotify;
import emu.grasscutter.utils.Either;
import lombok.AccessLevel;
Expand Down Expand Up @@ -139,49 +140,51 @@ private void cancelSummonEventsIfAvatarLeave() {

public Either<List<GameItem>, Integer> claimAvatarRewards(int eventId) {
if (this.rewardEvents.isEmpty()) {
return Either.right(RetcodeOuterClass.Retcode.RET_FAIL_VALUE);
return Either.right(Retcode.RET_FAIL_VALUE);
}

var event = this.rewardEvents.remove(0);
if (event.getEventId() != eventId) {
return Either.right(RetcodeOuterClass.Retcode.RET_FAIL_VALUE);
return Either.right(Retcode.RET_FAIL_VALUE);
}

if (!this.homeOwner.getHome().onClaimAvatarRewards(eventId)) {
return Either.right(RetcodeOuterClass.Retcode.RET_FAIL_VALUE);
return Either.right(Retcode.RET_FAIL_VALUE);
}

return Either.left(event.giveRewards());
}

public Either<HomeAvatarSummonEvent, Integer> fireAvatarSummonEvent(
Player owner, int avatarId, int guid, int suiteId) {
var targetSuite =
((HomeScene) owner.getScene())
HomeSuiteItem targetSuite = null;
if (owner.getScene() instanceof HomeScene homeScene) {
targetSuite = homeScene
.getSceneItem().getBlockItems().values().stream()
.map(HomeBlockItem::getSuiteList)
.flatMap(Collection::stream)
.filter(suite -> suite.getGuid() == guid)
.findFirst()
.orElse(null);
}

if (this.isInRewardEvent(avatarId)) {
return Either.right(RetcodeOuterClass.Retcode.RET_DUPLICATE_AVATAR_VALUE);
return Either.right(Retcode.RET_DUPLICATE_AVATAR_VALUE);
}

if (this.rewardEvents.stream().anyMatch(event -> event.getGuid() == guid)) {
return Either.right(RetcodeOuterClass.Retcode.RET_HOME_FURNITURE_GUID_ERROR_VALUE);
return Either.right(Retcode.RET_HOME_FURNITURE_GUID_ERROR_VALUE);
}

this.summonEvents.removeIf(event -> event.getGuid() == guid || event.getAvatarId() == avatarId);

if (targetSuite == null) {
return Either.right(RetcodeOuterClass.Retcode.RET_HOME_CLIENT_PARAM_INVALID_VALUE);
return Either.right(Retcode.RET_HOME_CLIENT_PARAM_INVALID_VALUE);
}

var eventData = SuiteEventType.HOME_AVATAR_SUMMON_EVENT.getEventDataFrom(avatarId, suiteId);
if (eventData == null) {
return Either.right(RetcodeOuterClass.Retcode.RET_HOME_CLIENT_PARAM_INVALID_VALUE);
return Either.right(Retcode.RET_HOME_CLIENT_PARAM_INVALID_VALUE);
}

var event =
Expand All @@ -196,8 +199,8 @@ public void onFinishSummonEvent(int eventId) {
this.summonEvents.removeIf(event -> event.getEventId() == eventId);
}

public HomeAvatarRewardEventNotifyOuterClass.HomeAvatarRewardEventNotify toRewardEventProto() {
var notify = HomeAvatarRewardEventNotifyOuterClass.HomeAvatarRewardEventNotify.newBuilder();
public HomeAvatarRewardEventNotify toRewardEventProto() {
var notify = HomeAvatarRewardEventNotify.newBuilder();
if (!this.rewardEvents.isEmpty()) {
notify.setRewardEvent(this.rewardEvents.get(0).toProto()).setIsEventTrigger(true);

Expand All @@ -210,9 +213,8 @@ public HomeAvatarRewardEventNotifyOuterClass.HomeAvatarRewardEventNotify toRewar
return notify.build();
}

public HomeAvatarSummonAllEventNotifyOuterClass.HomeAvatarSummonAllEventNotify
toSummonEventProto() {
return HomeAvatarSummonAllEventNotifyOuterClass.HomeAvatarSummonAllEventNotify.newBuilder()
public HomeAvatarSummonAllEventNotify toSummonEventProto() {
return HomeAvatarSummonAllEventNotify.newBuilder()
.addAllSummonEventList(
this.summonEvents.stream().map(HomeAvatarSummonEvent::toProto).toList())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import emu.grasscutter.net.packet.Opcodes;
import emu.grasscutter.net.packet.PacketHandler;
import emu.grasscutter.net.packet.PacketOpcodes;
import emu.grasscutter.net.proto.HomeChangeModuleReqOuterClass;
import emu.grasscutter.net.proto.RetcodeOuterClass;
import emu.grasscutter.net.proto.HomeChangeModuleReqOuterClass.HomeChangeModuleReq;
import emu.grasscutter.net.proto.RetcodeOuterClass.Retcode;
import emu.grasscutter.server.event.player.PlayerTeleportEvent.TeleportType;
import emu.grasscutter.server.game.GameSession;
import emu.grasscutter.server.packet.send.PacketHomeAvatarTalkFinishInfoNotify;
Expand All @@ -18,12 +18,11 @@ public class HandlerHomeChangeModuleReq extends PacketHandler {

@Override
public void handle(GameSession session, byte[] header, byte[] payload) throws Exception {
HomeChangeModuleReqOuterClass.HomeChangeModuleReq req =
HomeChangeModuleReqOuterClass.HomeChangeModuleReq.parseFrom(payload);
var req = HomeChangeModuleReq.parseFrom(payload);

var homeWorld = session.getPlayer().getCurHomeWorld();
if (!homeWorld.getGuests().isEmpty()) {
session.send(new PacketHomeChangeModuleRsp(RetcodeOuterClass.Retcode.RET_HOME_HAS_GUEST));
session.send(new PacketHomeChangeModuleRsp(Retcode.RET_HOME_HAS_GUEST));
return;
}

Expand All @@ -32,7 +31,7 @@ public void handle(GameSession session, byte[] header, byte[] payload) throws Ex

if (scene == null) {
Grasscutter.getLogger().warn("scene == null! Changing module will fail.");
session.send(new PacketHomeChangeModuleRsp(RetcodeOuterClass.Retcode.RET_INVALID_SCENE_ID));
session.send(new PacketHomeChangeModuleRsp(Retcode.RET_INVALID_SCENE_ID));
return;
}

Expand Down

0 comments on commit 9481fbc

Please sign in to comment.