diff --git a/src/main/java/com/minecolonies/core/blocks/MinecoloniesCropBlock.java b/src/main/java/com/minecolonies/core/blocks/MinecoloniesCropBlock.java index 3f7cf35b8d5..0a1b19608d5 100644 --- a/src/main/java/com/minecolonies/core/blocks/MinecoloniesCropBlock.java +++ b/src/main/java/com/minecolonies/core/blocks/MinecoloniesCropBlock.java @@ -167,17 +167,6 @@ public void registerBlockItem(final IForgeRegistry registry, final Item.Pr registry.register(getRegistryName(), new ItemCrop(this, properties, preferredBiome)); } - /** - * Get the preferred biome for this crop. - * - * @return the preferred biome. - */ - @Nullable - public TagKey getPreferredBiome() - { - return preferredBiome; - } - /** * Get the preferred farmland for this crop. * diff --git a/src/main/java/com/minecolonies/core/client/gui/townhall/WindowTownHallExpeditions.java b/src/main/java/com/minecolonies/core/client/gui/townhall/WindowTownHallExpeditions.java index 39b6be55f46..1de452d7a97 100644 --- a/src/main/java/com/minecolonies/core/client/gui/townhall/WindowTownHallExpeditions.java +++ b/src/main/java/com/minecolonies/core/client/gui/townhall/WindowTownHallExpeditions.java @@ -19,8 +19,8 @@ import com.minecolonies.core.colony.expeditions.colony.types.ColonyExpeditionType; import com.minecolonies.core.colony.expeditions.colony.types.ColonyExpeditionTypeDifficulty; import com.minecolonies.core.colony.expeditions.encounters.ExpeditionEncounter; -import com.minecolonies.core.colony.expeditions.encounters.ExpeditionEncounterManager; import com.minecolonies.core.datalistener.ColonyExpeditionTypeListener; +import com.minecolonies.core.datalistener.ExpeditionEncounterListener; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.MutableComponent; import net.minecraft.resources.ResourceLocation; @@ -283,10 +283,10 @@ public void updateElement(final int i, final Pane pane) final MobKill item = currentStage.getKills().get(itemIndex); final EntityIcon entityIcon = childKills.findPaneOfTypeByID(PARTIAL_ITEM_PREFIX + colIndex, EntityIcon.class); - final ExpeditionEncounter encounter = ExpeditionEncounterManager.getInstance().getEncounter(item.encounterId()); + final ExpeditionEncounter encounter = ExpeditionEncounterListener.getEncounter(item.encounterId()); if (encounter != null) { - entityIcon.setEntity(encounter.getEntityType()); + entityIcon.setEntity(encounter.entityType()); entityIcon.setCount(item.count()); } else diff --git a/src/main/java/com/minecolonies/core/colony/events/ColonyExpeditionEvent.java b/src/main/java/com/minecolonies/core/colony/events/ColonyExpeditionEvent.java index 1d944cab3b5..737ae506c54 100644 --- a/src/main/java/com/minecolonies/core/colony/events/ColonyExpeditionEvent.java +++ b/src/main/java/com/minecolonies/core/colony/events/ColonyExpeditionEvent.java @@ -18,8 +18,8 @@ import com.minecolonies.core.colony.expeditions.ExpeditionVisitorMember; import com.minecolonies.core.colony.expeditions.colony.types.ColonyExpeditionType; import com.minecolonies.core.colony.expeditions.encounters.ExpeditionEncounter; -import com.minecolonies.core.colony.expeditions.encounters.ExpeditionEncounterManager; import com.minecolonies.core.datalistener.ColonyExpeditionTypeListener; +import com.minecolonies.core.datalistener.ExpeditionEncounterListener; import com.minecolonies.core.entity.visitor.ExpeditionaryVisitorType.DespawnTimeData.DespawnTime; import com.minecolonies.core.items.ItemAdventureToken; import net.minecraft.nbt.CompoundTag; @@ -245,7 +245,7 @@ private void processMobFight(final @NotNull ExpeditionEncounter encounter, final MobType mobType = MobType.UNDEFINED; try { - final Entity entity = encounter.getEntityType().create(colony.getWorld()); + final Entity entity = encounter.entityType().create(colony.getWorld()); if (entity instanceof Mob mob) { mobType = mob.getMobType(); @@ -259,7 +259,7 @@ private void processMobFight(final @NotNull ExpeditionEncounter encounter, final for (int i = 0; i < amount; i++) { - double encounterHealth = encounter.getHealth(); + double encounterHealth = encounter.health(); // Keep the fight going as long as the mob is not dead. while (encounterHealth > 0) @@ -273,7 +273,7 @@ private void processMobFight(final @NotNull ExpeditionEncounter encounter, final } final ItemStack weapon = attacker.getPrimaryWeapon(); - encounterHealth -= CombatRules.getDamageAfterAbsorb(getWeaponDamage(weapon, mobType), encounter.getArmor(), 0); + encounterHealth -= CombatRules.getDamageAfterAbsorb(getWeaponDamage(weapon, mobType), encounter.armor(), 0); if (weapon.hurt(1, random, null)) { attacker.setPrimaryWeapon(ItemStack.EMPTY); @@ -281,9 +281,9 @@ private void processMobFight(final @NotNull ExpeditionEncounter encounter, final if (encounterHealth > 0) { - final float damageAmount = handleDamageReduction(attacker, encounter.getDamage()); + final float damageAmount = handleDamageReduction(attacker, encounter.damage()); attacker.setHealth(Math.max(0, attacker.getHealth() - damageAmount)); - encounterHealth -= encounter.getReflectingDamage(); + encounterHealth -= encounter.reflectingDamage(); if (attacker.isDead()) { @@ -294,8 +294,8 @@ private void processMobFight(final @NotNull ExpeditionEncounter encounter, final if (encounterHealth <= 0) { - expedition.mobKilled(encounter.getId()); - final List loot = processLootTable(encounter.getLootTable(), expeditionType); + expedition.mobKilled(encounter.id()); + final List loot = processLootTable(encounter.lootTable(), expeditionType); loot.forEach(this::processReward); } } @@ -352,7 +352,7 @@ private void processAdventureToken(final ItemStack itemStack) case TOKEN_TAG_EXPEDITION_TYPE_ENCOUNTER: { final String encounterId = compound.getString(TOKEN_TAG_EXPEDITION_ENCOUNTER); - final ExpeditionEncounter encounter = ExpeditionEncounterManager.getInstance().getEncounter(new ResourceLocation(encounterId)); + final ExpeditionEncounter encounter = ExpeditionEncounterListener.getEncounter(new ResourceLocation(encounterId)); if (encounter == null) { Log.getLogger().warn("Expedition loot table referred to encounter '{}' which does not exist.", encounterId); diff --git a/src/main/java/com/minecolonies/core/colony/expeditions/encounters/ExpeditionEncounter.java b/src/main/java/com/minecolonies/core/colony/expeditions/encounters/ExpeditionEncounter.java index eb335567aca..c6a98e6cffa 100644 --- a/src/main/java/com/minecolonies/core/colony/expeditions/encounters/ExpeditionEncounter.java +++ b/src/main/java/com/minecolonies/core/colony/expeditions/encounters/ExpeditionEncounter.java @@ -6,82 +6,19 @@ /** * Encounters are mob encounters that can be found on expeditions. + * + * @param id The id of the encounter. + * @param entityType The entity type of the encounter. + * @param damage The damage this encounter will deal. + * @param reflectingDamage The damage this encounter will reflect upon itself after dealing damage. + * @param health The health this encounter has. + * @param armor The armor level this encounter has. + * @param lootTable The loot table killing this encounter will give. + * @param xp The experience killing this encounter will give. */ -public class ExpeditionEncounter +public record ExpeditionEncounter(ResourceLocation id, EntityType entityType, float damage, float reflectingDamage, double health, int armor, ResourceLocation lootTable, + double xp) { - /** - * The id of the encounter. - */ - private final ResourceLocation id; - - /** - * The entity type of the encounter. - */ - private final EntityType entityType; - - /** - * The damage this encounter will deal. - */ - private final float damage; - - /** - * The damage this encounter will reflect upon itself after dealing damage. - */ - private final float reflectingDamage; - - /** - * The health this encounter has. - */ - private final double health; - - /** - * The armor level this encounter has. - */ - private final int armor; - - /** - * The loot table killing this encounter will give. - */ - private final ResourceLocation lootTable; - - /** - * The experience killing this encounter will give. - */ - private final double xp; - - /** - * Default constructor. - */ - public ExpeditionEncounter( - final ResourceLocation id, - final EntityType entityType, - final float damage, - final float reflectingDamage, - final double health, - final int armor, - final ResourceLocation lootTable, - final double xp) - { - this.id = id; - this.entityType = entityType; - this.damage = damage; - this.reflectingDamage = reflectingDamage; - this.health = health; - this.armor = armor; - this.lootTable = lootTable; - this.xp = xp; - } - - /** - * Get the id of the encounter. - * - * @return the id. - */ - public ResourceLocation getId() - { - return id; - } - /** * Get the name of the encounter. * @@ -91,74 +28,4 @@ public Component getName() { return entityType.getDescription(); } - - /** - * Get the entity type of the encounter. - * - * @return the entity type. - */ - public EntityType getEntityType() - { - return entityType; - } - - /** - * Get the damage this encounter will deal. - * - * @return the damage amount. - */ - public float getDamage() - { - return damage; - } - - /** - * Get the damage this encounter will reflect upon itself after dealing damage. - * - * @return the reflecting damage amount. - */ - public float getReflectingDamage() - { - return reflectingDamage; - } - - /** - * Get the health this encounter has. - * - * @return the health amount. - */ - public double getHealth() - { - return health; - } - - /** - * Get the armor level this encounter has. - * - * @return the armor amount. - */ - public int getArmor() - { - return armor; - } - - /** - * Get the loot table killing this encounter will give. - * - * @return the loot table id. - */ - public ResourceLocation getLootTable() - { - return lootTable; - } - - /** - * Get the experience killing this encounter will give. - * - * @return the experience amount. - */ - public double getXp() - { - return xp; - } } diff --git a/src/main/java/com/minecolonies/core/colony/expeditions/encounters/ExpeditionEncounterManager.java b/src/main/java/com/minecolonies/core/colony/expeditions/encounters/ExpeditionEncounterManager.java deleted file mode 100644 index 5e2b22023da..00000000000 --- a/src/main/java/com/minecolonies/core/colony/expeditions/encounters/ExpeditionEncounterManager.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.minecolonies.core.colony.expeditions.encounters; - -import com.minecolonies.core.datalistener.ColonyExpeditionTypeListener; -import net.minecraft.resources.ResourceLocation; - -import java.util.HashMap; -import java.util.Map; - -/** - * Manager class for all the possible {@link ExpeditionEncounter} instances. - */ -public class ExpeditionEncounterManager -{ - /** - * The singleton instance. - */ - private static ExpeditionEncounterManager instance; - - /** - * The map of all possible expedition types. - */ - private final Map possibleTypes = new HashMap<>(); - - /** - * Internal constructor. - */ - private ExpeditionEncounterManager() - { - } - - /** - * Get the singleton instance for the expedition encounter manager. - * - * @return the singleton instance. - */ - public static ExpeditionEncounterManager getInstance() - { - if (instance == null) - { - instance = new ExpeditionEncounterManager(); - } - return instance; - } - - /** - * Reload all types, initiated by {@link ColonyExpeditionTypeListener}. - * - * @param newTypes the new map of types. - */ - public void reloadEncounters(final Map newTypes) - { - this.possibleTypes.clear(); - this.possibleTypes.putAll(newTypes); - } - - /** - * Get an encounter from its id. - * - * @param encounter the encounter id. - * @return the encounter instance or null. - */ - public ExpeditionEncounter getEncounter(final ResourceLocation encounter) - { - return this.possibleTypes.get(encounter); - } -} diff --git a/src/main/java/com/minecolonies/core/colony/expeditions/encounters/ExpeditionEncounterParser.java b/src/main/java/com/minecolonies/core/colony/expeditions/encounters/ExpeditionEncounterParser.java index d9fb5f72d9e..278f32df78e 100644 --- a/src/main/java/com/minecolonies/core/colony/expeditions/encounters/ExpeditionEncounterParser.java +++ b/src/main/java/com/minecolonies/core/colony/expeditions/encounters/ExpeditionEncounterParser.java @@ -2,8 +2,10 @@ import com.google.gson.JsonObject; import com.google.gson.JsonParseException; +import net.minecraft.network.FriendlyByteBuf; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.entity.EntityType; +import net.minecraftforge.registries.ForgeRegistries; import org.jetbrains.annotations.NotNull; /** @@ -59,13 +61,50 @@ public static ExpeditionEncounter parse(final ResourceLocation id, final JsonObj public static JsonObject toJson(final ExpeditionEncounter encounter) { final JsonObject object = new JsonObject(); - object.addProperty(PROP_ENTITY_TYPE, EntityType.getKey(encounter.getEntityType()).toString()); - object.addProperty(PROP_DAMAGE, encounter.getDamage()); - object.addProperty(PROP_REFLECTING_DAMAGE, encounter.getReflectingDamage()); - object.addProperty(PROP_HEALTH, encounter.getHealth()); - object.addProperty(PROP_ARMOR, encounter.getArmor()); - object.addProperty(PROP_LOOT_TABLE, encounter.getLootTable().toString()); - object.addProperty(PROP_XP, encounter.getXp()); + object.addProperty(PROP_ENTITY_TYPE, EntityType.getKey(encounter.entityType()).toString()); + object.addProperty(PROP_DAMAGE, encounter.damage()); + object.addProperty(PROP_REFLECTING_DAMAGE, encounter.reflectingDamage()); + object.addProperty(PROP_HEALTH, encounter.health()); + object.addProperty(PROP_ARMOR, encounter.armor()); + object.addProperty(PROP_LOOT_TABLE, encounter.lootTable().toString()); + object.addProperty(PROP_XP, encounter.xp()); return object; } + + /** + * Turns an expedition encounter instance into NBT format. + * + * @param encounter the expedition encounter instance. + * @param buf the buf to write into. + */ + public static void toBuffer(final ExpeditionEncounter encounter, final FriendlyByteBuf buf) + { + buf.writeResourceLocation(encounter.id()); + buf.writeRegistryId(ForgeRegistries.ENTITY_TYPES, encounter.entityType()); + buf.writeFloat(encounter.damage()); + buf.writeFloat(encounter.reflectingDamage()); + buf.writeDouble(encounter.health()); + buf.writeInt(encounter.armor()); + buf.writeResourceLocation(encounter.lootTable()); + buf.writeDouble(encounter.xp()); + } + + /** + * Attempt to parse an expedition encounter instance from a network buffer. + * + * @param buf the network buffer. + * @return the expedition encounter instance, or null. + */ + public static ExpeditionEncounter fromBuffer(final FriendlyByteBuf buf) + { + final ResourceLocation id = buf.readResourceLocation(); + final EntityType entityType = buf.readRegistryIdSafe(EntityType.class); + final float damage = buf.readFloat(); + final float reflectingDamage = buf.readFloat(); + final double health = buf.readDouble(); + final int armor = buf.readInt(); + final ResourceLocation lootTable = buf.readResourceLocation(); + final double xp = buf.readDouble(); + return new ExpeditionEncounter(id, entityType, damage, reflectingDamage, health, armor, lootTable, xp); + } } diff --git a/src/main/java/com/minecolonies/core/datalistener/ColonyExpeditionTypeListener.java b/src/main/java/com/minecolonies/core/datalistener/ColonyExpeditionTypeListener.java index 477628a89a1..082e7448ced 100644 --- a/src/main/java/com/minecolonies/core/datalistener/ColonyExpeditionTypeListener.java +++ b/src/main/java/com/minecolonies/core/datalistener/ColonyExpeditionTypeListener.java @@ -9,7 +9,7 @@ import com.minecolonies.core.Network; import com.minecolonies.core.colony.expeditions.colony.types.ColonyExpeditionType; import com.minecolonies.core.colony.expeditions.colony.types.ColonyExpeditionTypeParser; -import com.minecolonies.core.network.messages.client.GlobalExpeditionTypeSyncMessage; +import com.minecolonies.core.network.messages.client.GlobalColonyExpeditionTypeSyncMessage; import io.netty.buffer.Unpooled; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.resources.ResourceKey; @@ -103,7 +103,7 @@ public static void sendGlobalExpeditionTypePacket(final ServerPlayer player) { ColonyExpeditionTypeParser.toBuffer(entry.getValue(), byteBuf); } - Network.getNetwork().sendToPlayer(new GlobalExpeditionTypeSyncMessage(byteBuf), player); + Network.getNetwork().sendToPlayer(new GlobalColonyExpeditionTypeSyncMessage(byteBuf), player); } /** diff --git a/src/main/java/com/minecolonies/core/datalistener/ExpeditionEncounterListener.java b/src/main/java/com/minecolonies/core/datalistener/ExpeditionEncounterListener.java index 47555bb916a..ad75f71f6d0 100644 --- a/src/main/java/com/minecolonies/core/datalistener/ExpeditionEncounterListener.java +++ b/src/main/java/com/minecolonies/core/datalistener/ExpeditionEncounterListener.java @@ -5,16 +5,21 @@ import com.google.gson.JsonElement; import com.google.gson.JsonParseException; import com.minecolonies.api.util.Log; +import com.minecolonies.core.Network; import com.minecolonies.core.colony.expeditions.encounters.ExpeditionEncounter; -import com.minecolonies.core.colony.expeditions.encounters.ExpeditionEncounterManager; import com.minecolonies.core.colony.expeditions.encounters.ExpeditionEncounterParser; +import com.minecolonies.core.network.messages.client.GlobalExpeditionEncounterSyncMessage; +import io.netty.buffer.Unpooled; +import net.minecraft.network.FriendlyByteBuf; import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.packs.resources.ResourceManager; import net.minecraft.server.packs.resources.SimpleJsonResourceReloadListener; import net.minecraft.util.profiling.ProfilerFiller; import org.apache.logging.log4j.message.FormattedMessage; import org.jetbrains.annotations.NotNull; +import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -28,6 +33,11 @@ public class ExpeditionEncounterListener extends SimpleJsonResourceReloadListene */ private static final Gson GSON = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create(); + /** + * The map of all possible expedition encounters. + */ + private static Map POSSIBLE_TYPES = new HashMap<>(); + /** * Set up the core loading, with the directory in the datapack that contains this data * Directory is: (namespace)/colony/expedition_encounters/(path) @@ -37,6 +47,50 @@ public ExpeditionEncounterListener() super(GSON, "colony/expedition_encounters"); } + /** + * Get an encounter from its id. + * + * @param encounter the encounter id. + * @return the encounter instance or null. + */ + public static ExpeditionEncounter getEncounter(final ResourceLocation encounter) + { + return POSSIBLE_TYPES.get(encounter); + } + + /** + * Sync to client. + * + * @param player to send it to. + */ + public static void sendGlobalExpeditionEncounterPacket(final ServerPlayer player) + { + final FriendlyByteBuf byteBuf = new FriendlyByteBuf(Unpooled.buffer()); + byteBuf.writeInt(POSSIBLE_TYPES.size()); + for (final Map.Entry entry : POSSIBLE_TYPES.entrySet()) + { + ExpeditionEncounterParser.toBuffer(entry.getValue(), byteBuf); + } + Network.getNetwork().sendToPlayer(new GlobalExpeditionEncounterSyncMessage(byteBuf), player); + } + + /** + * Read the data from the packet and parse it. + * + * @param byteBuf pck. + */ + public static void readGlobalExpeditionEncounterPackets(final FriendlyByteBuf byteBuf) + { + final Map newTypes = new HashMap<>(); + final int size = byteBuf.readInt(); + for (int i = 0; i < size; i++) + { + final ExpeditionEncounter encounter = ExpeditionEncounterParser.fromBuffer(byteBuf); + newTypes.put(encounter.id(), encounter); + } + POSSIBLE_TYPES = Collections.unmodifiableMap(newTypes); + } + @Override protected void apply( @NotNull final Map object, @@ -60,7 +114,6 @@ protected void apply( } } - final ExpeditionEncounterManager manager = ExpeditionEncounterManager.getInstance(); - manager.reloadEncounters(newTypes); + POSSIBLE_TYPES = Collections.unmodifiableMap(newTypes); } } \ No newline at end of file diff --git a/src/main/java/com/minecolonies/core/event/DataPackSyncEventHandler.java b/src/main/java/com/minecolonies/core/event/DataPackSyncEventHandler.java index e73c1d49294..be1dea2e7b5 100644 --- a/src/main/java/com/minecolonies/core/event/DataPackSyncEventHandler.java +++ b/src/main/java/com/minecolonies/core/event/DataPackSyncEventHandler.java @@ -7,6 +7,7 @@ import com.minecolonies.core.colony.crafting.CustomRecipeManager; import com.minecolonies.core.compatibility.CraftingTagAuditor; import com.minecolonies.core.datalistener.ColonyExpeditionTypeListener; +import com.minecolonies.core.datalistener.ExpeditionEncounterListener; import com.minecolonies.core.datalistener.QuestJsonListener; import com.minecolonies.core.network.messages.client.UpdateClientWithCompatibilityMessage; import com.minecolonies.core.util.FurnaceRecipes; @@ -70,6 +71,7 @@ private static void sendPackets(@NotNull final ServerPlayer player, CustomRecipeManager.getInstance().sendCustomRecipeManagerPackets(player); IGlobalResearchTree.getInstance().sendGlobalResearchTreePackets(player); QuestJsonListener.sendGlobalQuestPackets(player); + ExpeditionEncounterListener.sendGlobalExpeditionEncounterPacket(player); ColonyExpeditionTypeListener.sendGlobalExpeditionTypePacket(player); } diff --git a/src/main/java/com/minecolonies/core/generation/defaults/DefaultExpeditionEncountersProvider.java b/src/main/java/com/minecolonies/core/generation/defaults/DefaultExpeditionEncountersProvider.java index 24dd08567cd..3e5b68150a5 100644 --- a/src/main/java/com/minecolonies/core/generation/defaults/DefaultExpeditionEncountersProvider.java +++ b/src/main/java/com/minecolonies/core/generation/defaults/DefaultExpeditionEncountersProvider.java @@ -123,7 +123,7 @@ public CompletableFuture run(final @NotNull CachedOutput cachedOutput) for (int i = 0; i < expeditionEncounters.size(); i++) { futures[i] = - DataProvider.saveStable(cachedOutput, ExpeditionEncounterParser.toJson(expeditionEncounters.get(i)), pathProvider.json(expeditionEncounters.get(i).getId())); + DataProvider.saveStable(cachedOutput, ExpeditionEncounterParser.toJson(expeditionEncounters.get(i)), pathProvider.json(expeditionEncounters.get(i).id())); } return CompletableFuture.allOf(futures); } diff --git a/src/main/java/com/minecolonies/core/network/NetworkChannel.java b/src/main/java/com/minecolonies/core/network/NetworkChannel.java index edaafc9d60b..51fa65128e3 100755 --- a/src/main/java/com/minecolonies/core/network/NetworkChannel.java +++ b/src/main/java/com/minecolonies/core/network/NetworkChannel.java @@ -233,7 +233,8 @@ public void registerCommonMessages() registerMessage(++idx, OpenPlantationFieldBuildWindowMessage.class, OpenPlantationFieldBuildWindowMessage::new); registerMessage(++idx, SaveStructureNBTMessage.class, SaveStructureNBTMessage::new); registerMessage(++idx, GlobalQuestSyncMessage.class, GlobalQuestSyncMessage::new); - registerMessage(++idx, GlobalExpeditionTypeSyncMessage.class, GlobalExpeditionTypeSyncMessage::new); + registerMessage(++idx, GlobalExpeditionEncounterSyncMessage.class, GlobalExpeditionEncounterSyncMessage::new); + registerMessage(++idx, GlobalColonyExpeditionTypeSyncMessage.class, GlobalColonyExpeditionTypeSyncMessage::new); registerMessage(++idx, OpenColonyFoundingCovenantMessage.class, OpenColonyFoundingCovenantMessage::new); registerMessage(++idx, OpenBuildingUIMessage.class, OpenBuildingUIMessage::new); registerMessage(++idx, OpenCantFoundColonyWarningMessage.class, OpenCantFoundColonyWarningMessage::new); diff --git a/src/main/java/com/minecolonies/core/network/messages/client/GlobalExpeditionTypeSyncMessage.java b/src/main/java/com/minecolonies/core/network/messages/client/GlobalColonyExpeditionTypeSyncMessage.java similarity index 84% rename from src/main/java/com/minecolonies/core/network/messages/client/GlobalExpeditionTypeSyncMessage.java rename to src/main/java/com/minecolonies/core/network/messages/client/GlobalColonyExpeditionTypeSyncMessage.java index 4f0455f748b..2530d72d727 100644 --- a/src/main/java/com/minecolonies/core/network/messages/client/GlobalExpeditionTypeSyncMessage.java +++ b/src/main/java/com/minecolonies/core/network/messages/client/GlobalColonyExpeditionTypeSyncMessage.java @@ -12,9 +12,9 @@ import org.jetbrains.annotations.Nullable; /** - * The message used to synchronize global expedition type data from a server to a remote client. + * The message used to synchronize global colony expedition type data from a server to a remote client. */ -public class GlobalExpeditionTypeSyncMessage implements IMessage +public class GlobalColonyExpeditionTypeSyncMessage implements IMessage { /** * The buffer with the data. @@ -24,7 +24,7 @@ public class GlobalExpeditionTypeSyncMessage implements IMessage /** * Empty constructor used when registering the message */ - public GlobalExpeditionTypeSyncMessage() + public GlobalColonyExpeditionTypeSyncMessage() { super(); } @@ -34,7 +34,7 @@ public GlobalExpeditionTypeSyncMessage() * * @param buf the bytebuffer. */ - public GlobalExpeditionTypeSyncMessage(final FriendlyByteBuf buf) + public GlobalColonyExpeditionTypeSyncMessage(final FriendlyByteBuf buf) { this.buffer = new FriendlyByteBuf(buf.copy()); } diff --git a/src/main/java/com/minecolonies/core/network/messages/client/GlobalExpeditionEncounterSyncMessage.java b/src/main/java/com/minecolonies/core/network/messages/client/GlobalExpeditionEncounterSyncMessage.java new file mode 100644 index 00000000000..e91607f9649 --- /dev/null +++ b/src/main/java/com/minecolonies/core/network/messages/client/GlobalExpeditionEncounterSyncMessage.java @@ -0,0 +1,72 @@ +package com.minecolonies.core.network.messages.client; + +import com.minecolonies.api.network.IMessage; +import com.minecolonies.core.datalistener.ExpeditionEncounterListener; +import net.minecraft.client.Minecraft; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraftforge.api.distmarker.Dist; +import net.minecraftforge.api.distmarker.OnlyIn; +import net.minecraftforge.fml.LogicalSide; +import net.minecraftforge.network.NetworkEvent; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * The message used to synchronize global expedition type data from a server to a remote client. + */ +public class GlobalExpeditionEncounterSyncMessage implements IMessage +{ + /** + * The buffer with the data. + */ + private FriendlyByteBuf buffer; + + /** + * Empty constructor used when registering the message + */ + public GlobalExpeditionEncounterSyncMessage() + { + super(); + } + + /** + * Add or Update expedition type data on the client. + * + * @param buf the bytebuffer. + */ + public GlobalExpeditionEncounterSyncMessage(final FriendlyByteBuf buf) + { + this.buffer = new FriendlyByteBuf(buf.copy()); + } + + @Override + public void fromBytes(@NotNull final FriendlyByteBuf buf) + { + buffer = new FriendlyByteBuf(buf.retain()); + } + + @Override + public void toBytes(@NotNull final FriendlyByteBuf buf) + { + buffer.resetReaderIndex(); + buf.writeBytes(buffer); + } + + @Nullable + @Override + public LogicalSide getExecutionSide() + { + return LogicalSide.CLIENT; + } + + @OnlyIn(Dist.CLIENT) + @Override + public void onExecute(final NetworkEvent.Context ctxIn, final boolean isLogicalServer) + { + if (Minecraft.getInstance().level != null) + { + ExpeditionEncounterListener.readGlobalExpeditionEncounterPackets(buffer); + } + buffer.release(); + } +}