Skip to content

Commit

Permalink
Fix server-related crashes/bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
SamB440 committed Mar 25, 2023
1 parent 9db78d2 commit 68eea7a
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ protected void build(FlowLayout rootComponent) {
}

entity.build(player, build, kingdom);
MinecraftClient.getInstance().currentScreen.close();
}).active(canAffordBuild)
.tooltip(List.of(
Text.literal(pluralText).append(build.getDisplayName()).append(Text.literal(" costs:")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ public void handleIncomingPacket(PacketContext context, PacketByteBuf attachedDa
final PlayerEntity player = context.player();
player.sendMessage(Text.literal("Received sync, " + instance));
final TaleOfKingdomsAPI api = TaleOfKingdoms.getAPI();
api.getConquestInstanceStorage().addConquest(player.getUuidAsString(), instance, true);
final ConquestInstance existing = api.getConquestInstanceStorage().getConquestInstance(player.getUuidAsString()).orElse(null);
if (existing != null) {
existing.uploadData(instance);
} else {
api.getConquestInstanceStorage().addConquest(player.getUuidAsString(), instance, true);
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
public final class OutgoingBuildKingdomPacket extends ClientPacketHandler {

public OutgoingBuildKingdomPacket() {
super(Packets.FIX_GUILD);
super(Packets.BUILD_KINGDOM);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
public final class OutgoingForemanCollectPacketHandler extends ClientPacketHandler {

public OutgoingForemanCollectPacketHandler() {
super(Packets.FOREMAN_BUY_WORKER);
super(Packets.FOREMAN_COLLECT);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.convallyria.taleofkingdoms.client.packet.ClientPacketHandler;
import com.convallyria.taleofkingdoms.common.packet.Packets;
import com.convallyria.taleofkingdoms.common.packet.context.PacketContext;
import com.convallyria.taleofkingdoms.common.shop.ShopParser;
import io.netty.buffer.Unpooled;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.network.PacketByteBuf;
Expand All @@ -24,6 +25,7 @@ public void handleIncomingPacket(PacketContext context, PacketByteBuf attachedDa
public void handleOutgoingPacket(@NotNull PlayerEntity player, @Nullable Object... data) {
PacketByteBuf passedData = new PacketByteBuf(Unpooled.buffer());
passedData.writeBoolean((Boolean) data[0]);
passedData.writeEnumConstant((ShopParser.GUI) data[1]);
sendPacket(player, passedData);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;

import java.util.concurrent.CompletableFuture;

public class CityBuilderEntity extends TOKEntity implements InventoryOwner {

private static final TrackedData<Boolean> MOVING_TO_LOCATION;
Expand Down Expand Up @@ -116,11 +118,17 @@ protected ActionResult interactMob(PlayerEntity player, Hand hand) {
}

public void give64wood(PlayerEntity player) {
System.out.println("64 wood");
final int playerWoodCount = InventoryUtils.count(player.getInventory(), ItemTags.LOGS);
TaleOfKingdoms.getAPI().executeOnServerEnvironment((server) -> {
System.out.println("server");
final ItemStack stack = InventoryUtils.getStack(player.getInventory(), ItemTags.LOGS, 64);
final ServerPlayerEntity serverPlayer = server.getPlayerManager().getPlayer(player.getUuid());
final CityBuilderEntity serverCityBuilder = (CityBuilderEntity) serverPlayer.getWorld().getEntityById(this.getId());
System.out.println("playerwoodcount: " + playerWoodCount);
System.out.println("wood: " + getWood());
System.out.println("stack: " + stack);
System.out.println("can insert? " + serverCityBuilder.getInventory().canInsert(stack));
if (stack != null && playerWoodCount >= 64 && getWood() <= (320 - 64) && serverCityBuilder.getInventory().canInsert(stack)) {
int slot = serverPlayer.getInventory().getSlotWithStack(stack);
serverPlayer.getInventory().removeStack(slot);
Expand Down Expand Up @@ -164,18 +172,20 @@ public void fixKingdom(PlayerEntity player, PlayerKingdom kingdom) {
});
}

public void build(PlayerEntity player, BuildCosts build, PlayerKingdom kingdom) {
public CompletableFuture<Void> build(PlayerEntity player, BuildCosts build, PlayerKingdom kingdom) {
CompletableFuture<Void> future = new CompletableFuture<>();
TaleOfKingdoms.getAPI().executeOnServerEnvironment(server -> {
final ServerPlayerEntity serverPlayer = server.getPlayerManager().getPlayer(player.getUuid());
final CityBuilderEntity serverCityBuilder = (CityBuilderEntity) serverPlayer.getWorld().getEntityById(this.getId());
MinecraftClient.getInstance().currentScreen.close();
kingdom.addBuilt(build.getKingdomPOI());
TaleOfKingdoms.LOGGER.info("Placing " + build + "...");
TaleOfKingdoms.getAPI().getSchematicHandler().pasteSchematic(build.getSchematic(), serverPlayer, kingdom.getPOIPos(build.getKingdomPOI()), build.getSchematicRotation());

serverCityBuilder.getInventory().removeItem(Items.OAK_LOG, build.getWood());
serverCityBuilder.getInventory().removeItem(Items.COBBLESTONE, build.getStone());
future.complete(null);
});
return future;
}

public void followPlayer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,26 @@ public static DefaultAttributeContainer.Builder createMobAttributes() {
@Override
protected ActionResult interactMob(final PlayerEntity player, final Hand hand) {
if (hand == Hand.OFF_HAND) return ActionResult.FAIL;
final boolean client = player.world.isClient();
final TaleOfKingdomsAPI api = TaleOfKingdoms.getAPI();
api.getConquestInstanceStorage().mostRecentInstance().ifPresent(instance -> {
if (instance.hasAttacked(player.getUuid())) {
if (player.getMainHandStack().getItem() == Items.WOODEN_SWORD) {
this.setStackInHand(Hand.MAIN_HAND, player.getMainHandStack());
if (player.world.isClient()) Translations.GUILDMEMBER_START_FIGHT.send(player);
if (!client) Translations.GUILDMEMBER_START_FIGHT.send(player);
final int[] countdown = {3};
api.getScheduler().repeatN(server -> {
boolean send = FabricLoader.getInstance().getEnvironmentType() != EnvType.CLIENT || player.world.isClient();
boolean send = FabricLoader.getInstance().getEnvironmentType() != EnvType.CLIENT || !client;
if (send) player.sendMessage(Text.literal("" + countdown[0]), false);
countdown[0] = countdown[0] - 1;
}, 3, 0, 20);
api.getScheduler().queue(server -> {
final ImprovedFollowTargetGoal<PlayerEntity> goal = new ImprovedFollowTargetGoal<>(this, EntityType.PLAYER, true);
this.targetSelector.add(0, goal);
if (player.world.isClient()) Translations.GUILDMEMBER_BEGIN.send(player);
if (!client) Translations.GUILDMEMBER_BEGIN.send(player);
api.getScheduler().queue(server2 -> {
this.targetSelector.remove(goal);
if (player.world.isClient()) Translations.GUILDMEMBER_GOOD_FIGHTER.send(player);
if (!client) Translations.GUILDMEMBER_GOOD_FIGHTER.send(player);
final GuildPlayer guildPlayer = instance.getPlayer(player);
guildPlayer.setWorthiness(guildPlayer.getWorthiness() + 2);
this.setStackInHand(Hand.MAIN_HAND, new ItemStack(Items.IRON_SWORD));
Expand All @@ -86,9 +87,9 @@ protected ActionResult interactMob(final PlayerEntity player, final Hand hand) {
player.getInventory().removeOne(player.getMainHandStack());
return;
}
if (player.world.isClient()) Translations.GUILDMEMBER_FIGHTER.send(player);
if (!client) Translations.GUILDMEMBER_FIGHTER.send(player);
} else {
if (player.world.isClient()) Translations.GUILDMEMBER_START.send(player);
if (!client) Translations.GUILDMEMBER_START.send(player);
}
});
return ActionResult.PASS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,24 @@ public class ConquestInstance {
Codec.unboundedMap(Uuids.CODEC, GuildPlayer.CODEC).fieldOf("guild_players").forGetter(ConquestInstance::getGuildPlayers)
).apply(instance, (name, hasLoaded, start, end, origin, underAttack, attackLocations, attackers, guildPlayers) -> {
ConquestInstance conquestInstance = new ConquestInstance(name, start, end, origin);
conquestInstance.setLoaded(hasLoaded);
conquestInstance.setUnderAttack(underAttack);
conquestInstance.getReficuleAttackLocations().addAll(attackLocations);
conquestInstance.getReficuleAttackers().addAll(attackers);
conquestInstance.getGuildPlayers().putAll(guildPlayers);
conquestInstance.uploadData(start, end, hasLoaded, underAttack, attackLocations, attackers, guildPlayers);
return conquestInstance;
}
));

public void uploadData(ConquestInstance newData) {
this.uploadData(newData.start, newData.end, newData.hasLoaded, newData.underAttack, newData.reficuleAttackLocations, newData.reficuleAttackers, newData.guildPlayers);
}

public void uploadData(BlockPos start, BlockPos end, boolean hasLoaded, boolean underAttack, List<BlockPos> attackLocations, List<UUID> attackers, Map<UUID, GuildPlayer> guildPlayers) {
this.setStart(start);
this.setEnd(end);
this.setLoaded(hasLoaded);
this.setUnderAttack(underAttack);
this.getReficuleAttackLocations().addAll(attackLocations);
this.getReficuleAttackers().addAll(attackers);
this.getGuildPlayers().putAll(guildPlayers);
}

private final String name;
@Deprecated(forRemoval = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ public IncomingBuildKingdomPacket() {
@Override
public void handleIncomingPacket(PacketContext context, PacketByteBuf attachedData) {
ServerPlayerEntity player = (ServerPlayerEntity) context.player();

final int entityId = attachedData.readInt();
context.taskQueue().execute(() -> {
final TaleOfKingdomsAPI api = TaleOfKingdoms.getAPI();
api.getConquestInstanceStorage().mostRecentInstance().ifPresent(instance -> {
final int entityId = attachedData.readInt();
final Entity entity = player.world.getEntityById(entityId);
if (!(entity instanceof CityBuilderEntity cityBuilderEntity) || player.distanceTo(cityBuilderEntity) > 5) {
reject(player, "Invalid entity ID / Distance");
Expand Down Expand Up @@ -76,8 +75,9 @@ public void handleIncomingPacket(PacketContext context, PacketByteBuf attachedDa
cityBuilderEntity.refreshPositionAfterTeleport(player.getX(), player.getY(), player.getZ());
// Now move to the well location
cityBuilderEntity.setTarget(playerKingdom.getPOIPos(KingdomPOI.CITY_BUILDER_WELL_POI));

ServerConquestInstance.sync(player, instance);
});
ServerConquestInstance.sync(player, instance);
});
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.convallyria.taleofkingdoms.common.packet.context.PacketContext;
import com.convallyria.taleofkingdoms.common.world.guild.GuildPlayer;
import com.convallyria.taleofkingdoms.server.packet.ServerPacketHandler;
import com.convallyria.taleofkingdoms.server.world.ServerConquestInstance;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.network.PacketByteBuf;
Expand All @@ -19,7 +20,7 @@
public final class IncomingCityBuilderActionPacketHandler extends ServerPacketHandler {

public IncomingCityBuilderActionPacketHandler() {
super(Packets.FOREMAN_COLLECT);
super(Packets.CITYBUILDER_ACTION);
}

@Override
Expand Down Expand Up @@ -58,6 +59,7 @@ public void handleIncomingPacket(PacketContext context, PacketByteBuf attachedDa
return;
}

System.out.println("action: " + action);
switch (action) {
case GIVE_64_WOOD -> cityBuilderEntity.give64wood(player);
case GIVE_64_STONE -> cityBuilderEntity.give64stone(player);
Expand All @@ -68,7 +70,7 @@ public void handleIncomingPacket(PacketContext context, PacketByteBuf attachedDa
return;
}

cityBuilderEntity.build(player, buildCosts, kingdom);
cityBuilderEntity.build(player, buildCosts, kingdom).thenAccept((v) -> ServerConquestInstance.sync(player, instance));
}
}
}));
Expand Down

0 comments on commit 68eea7a

Please sign in to comment.