Skip to content

Commit

Permalink
Try catch events we post (#10139)
Browse files Browse the repository at this point in the history
Try catch events we post
  • Loading branch information
Raycoms committed Aug 20, 2024
1 parent d15fb4c commit c7e59c9
Show file tree
Hide file tree
Showing 13 changed files with 122 additions and 26 deletions.
27 changes: 24 additions & 3 deletions src/main/java/com/minecolonies/core/colony/ColonyManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,14 @@ public void onWorldLoad(@NotNull final Level world)
c.onWorldLoad(world);
}

MinecraftForge.EVENT_BUS.post(new ColonyManagerLoadedEvent(this));
try
{
MinecraftForge.EVENT_BUS.post(new ColonyManagerLoadedEvent(this));
}
catch (final Exception e)
{
Log.getLogger().error("Error during ColonyManagerLoadedEvent", e);
}
}
}

Expand All @@ -660,7 +667,14 @@ public void onWorldUnload(@NotNull final Level world)
BackUpHelper.backupColonyData();
}

MinecraftForge.EVENT_BUS.post(new ColonyManagerUnloadedEvent(this));
try
{
MinecraftForge.EVENT_BUS.post(new ColonyManagerUnloadedEvent(this));
}
catch (final Exception e)
{
Log.getLogger().error("Error during ColonyManagerUnloadedEvent", e);
}
}
}

Expand Down Expand Up @@ -689,7 +703,14 @@ public void handleColonyViewMessage(
}
view.handleColonyViewMessage(colonyData, world, isNewSubscription);

MinecraftForge.EVENT_BUS.post(new ColonyViewUpdatedEvent(view));
try
{
MinecraftForge.EVENT_BUS.post(new ColonyViewUpdatedEvent(view));
}
catch (final Exception e)
{
Log.getLogger().error("Error during ColonyViewUpdatedEvent", e);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,13 @@ public void handleCustomRecipeManagerMessage(final FriendlyByteBuf buff)
lootTables.put(id, drops);
}

MinecraftForge.EVENT_BUS.post(new CustomRecipesReloadedEvent());
try
{
MinecraftForge.EVENT_BUS.post(new CustomRecipesReloadedEvent());
}
catch (final Exception e)
{
Log.getLogger().error("Error during CustomRecipesReloadedEvent", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.minecolonies.api.colony.interactionhandling.IInteractionResponseHandler;
import com.minecolonies.api.colony.interactionhandling.ModInteractionResponseHandlers;
import com.minecolonies.api.util.InventoryUtils;
import com.minecolonies.api.util.Log;
import com.minecolonies.api.util.MessageUtils;
import com.minecolonies.api.util.Tuple;
import com.minecolonies.api.util.constant.Constants;
Expand Down Expand Up @@ -189,7 +190,14 @@ public void onServerResponseTriggered(final int responseId, final Player player,
MessageUtils.format(MESSAGE_RECRUITMENT_SUCCESS, data.getName()).sendTo(colony).forAllPlayers();
}

MinecraftForge.EVENT_BUS.post(new CitizenAddedEvent(newCitizen, CitizenAddedEvent.Source.HIRED));
try
{
MinecraftForge.EVENT_BUS.post(new CitizenAddedEvent(newCitizen, CitizenAddedEvent.Source.HIRED));
}
catch (final Exception e)
{
Log.getLogger().error("Error during CitizenAddedEvent", e);
}
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,14 @@ public ICitizenData resurrectCivilianData(@NotNull final CompoundTag compoundNBT
citizens.put(citizenData.getId(), citizenData);
spawnOrCreateCitizen(citizenData, world, spawnPos);

MinecraftForge.EVENT_BUS.post(new CitizenAddedEvent(citizenData, CitizenAddedEvent.Source.RESURRECTED));

try
{
MinecraftForge.EVENT_BUS.post(new CitizenAddedEvent(citizenData, CitizenAddedEvent.Source.RESURRECTED));
}
catch (final Exception e)
{
Log.getLogger().error("Error during CitizenAddedEvent", e);
}
return citizenData;
}

Expand Down Expand Up @@ -618,8 +624,14 @@ else if (femaleCount < (getCitizens().size() - 1) / 2.0)

spawnOrCreateCivilian(newCitizen, colony.getWorld(), null, true);

MinecraftForge.EVENT_BUS.post(new CitizenAddedEvent(newCitizen, CitizenAddedEvent.Source.INITIAL));

try
{
MinecraftForge.EVENT_BUS.post(new CitizenAddedEvent(newCitizen, CitizenAddedEvent.Source.INITIAL));
}
catch (final Exception e)
{
Log.getLogger().error("Error during CitizenAddedEvent", e);
}
colony.getEventDescriptionManager().addEventDescription(new CitizenSpawnedEvent(colony.getBuildingManager().getTownHall().getPosition(),
newCitizen.getName()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.minecolonies.api.colony.IColony;
import com.minecolonies.api.colony.IColonyManager;
import com.minecolonies.api.colony.citizens.event.CitizenAddedEvent;
import com.minecolonies.api.util.Log;
import com.minecolonies.core.commands.commandTypes.IMCCommand;
import com.minecolonies.core.commands.commandTypes.IMCOPCommand;
import com.mojang.brigadier.arguments.IntegerArgumentType;
Expand Down Expand Up @@ -42,8 +43,14 @@ public int onExecute(final CommandContext<CommandSourceStack> context)
final ICitizenData newCitizen = colony.getCitizenManager().spawnOrCreateCivilian(null, colony.getWorld(), null, true);
context.getSource().sendSuccess(() -> Component.translatable(COMMAND_CITIZEN_SPAWN_SUCCESS, newCitizen.getName()), true);

MinecraftForge.EVENT_BUS.post(new CitizenAddedEvent(newCitizen, CitizenAddedEvent.Source.COMMANDS));

try
{
MinecraftForge.EVENT_BUS.post(new CitizenAddedEvent(newCitizen, CitizenAddedEvent.Source.COMMANDS));
}
catch (final Exception e)
{
Log.getLogger().error("Error during CitizenAddedEvent", e);
}
return 1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,14 @@ public void executeSpecificCompleteActions()
if (wo instanceof WorkOrderBuilding)
{
final IBuilding building = colony.getBuildingManager().getBuilding(wo.getLocation());
MinecraftForge.EVENT_BUS.post(new BuildingConstructionEvent(building, BuildingConstructionEvent.EventType.fromWorkOrderType(wo.getWorkOrderType())));
try
{
MinecraftForge.EVENT_BUS.post(new BuildingConstructionEvent(building, BuildingConstructionEvent.EventType.fromWorkOrderType(wo.getWorkOrderType())));
}
catch (final Exception e)
{
Log.getLogger().error("Error during BuildingConstructionEvent", e);
}
switch (wo.getWorkOrderType())
{
case BUILD:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1642,7 +1642,14 @@ public void die(@NotNull final DamageSource damageSource)
Component.literal(damageSource.getLocalizedDeathMessage(this).getString()).getString().replaceFirst(this.getDisplayName().getString(), "Citizen");
citizenColonyHandler.getColony().getEventDescriptionManager().addEventDescription(new CitizenDiedEvent(blockPosition(), citizenData.getName(), deathCause));

MinecraftForge.EVENT_BUS.post(new CitizenRemovedEvent(citizenData, damageSource));
try
{
MinecraftForge.EVENT_BUS.post(new CitizenRemovedEvent(citizenData, damageSource));
}
catch (final Exception e)
{
Log.getLogger().error("Error during CitizenRemovedEvent", e);
}
}
super.die(damageSource);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.minecolonies.api.colony.buildings.IBuilding;
import com.minecolonies.api.colony.event.ColonyCreatedEvent;
import com.minecolonies.api.network.IMessage;
import com.minecolonies.api.util.Log;
import com.minecolonies.core.Network;
import com.minecolonies.core.network.messages.client.colony.OpenBuildingUIMessage;
import com.minecolonies.core.tileentities.TileEntityColonyBuilding;
Expand Down Expand Up @@ -189,10 +190,14 @@ else if (spawnDistance > MineColonies.getConfig().getServer().maxDistanceFromWor

Network.getNetwork().sendToPlayer(new OpenBuildingUIMessage(building), sender);

if (isLogicalServer)
try
{
MinecraftForge.EVENT_BUS.post(new ColonyCreatedEvent(createdColony));
}
catch (final Exception e)
{
Log.getLogger().error("Error during ColonyCreatedEvent", e);
}
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.minecolonies.api.colony.IColonyManager;
import com.minecolonies.api.colony.event.ColonyDeletedEvent;
import com.minecolonies.api.network.IMessage;
import com.minecolonies.api.util.Log;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.common.MinecraftForge;
import net.minecraft.network.FriendlyByteBuf;
Expand Down Expand Up @@ -55,7 +56,14 @@ public void onExecute(final NetworkEvent.Context ctxIn, final boolean isLogicalS

if (isLogicalServer)
{
MinecraftForge.EVENT_BUS.post(new ColonyDeletedEvent(colony));
try
{
MinecraftForge.EVENT_BUS.post(new ColonyDeletedEvent(colony));
}
catch (final Exception e)
{
Log.getLogger().error("Error during ColonyDeletedEvent", e);
}
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.minecolonies.api.colony.IColony;
import com.minecolonies.api.colony.event.ColonyInformationChangedEvent;
import com.minecolonies.api.util.Log;
import com.minecolonies.api.util.constant.Constants;
import com.minecolonies.core.network.messages.server.AbstractColonyServerMessage;
import net.minecraft.nbt.CompoundTag;
Expand Down Expand Up @@ -39,11 +40,14 @@ public ColonyFlagChangeMessage (IColony colony, ListTag patternList)
protected void onExecute(NetworkEvent.Context ctxIn, boolean isLogicalServer, IColony colony)
{
colony.setColonyFlag(patterns);

if (isLogicalServer)
try
{
MinecraftForge.EVENT_BUS.post(new ColonyInformationChangedEvent(colony, ColonyInformationChangedEvent.Type.FLAG));
}
catch (final Exception e)
{
Log.getLogger().error("Error during ColonyInformationChangedEvent", e);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.minecolonies.api.colony.IColony;
import com.minecolonies.api.colony.buildings.views.IBuildingView;
import com.minecolonies.api.colony.event.ColonyInformationChangedEvent;
import com.minecolonies.api.util.Log;
import com.minecolonies.core.network.messages.server.AbstractColonyServerMessage;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.ChatFormatting;
Expand Down Expand Up @@ -67,10 +68,13 @@ public void toBytesOverride(@NotNull final FriendlyByteBuf buf)
protected void onExecute(final NetworkEvent.Context ctxIn, final boolean isLogicalServer, final IColony colony)
{
colony.setColonyColor(ChatFormatting.values()[colorOrdinal]);

if (isLogicalServer)
try
{
MinecraftForge.EVENT_BUS.post(new ColonyInformationChangedEvent(colony, ColonyInformationChangedEvent.Type.TEAM_COLOR));
}
catch (final Exception e)
{
Log.getLogger().error("Error during ColonyInformationChangedEvent", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.minecolonies.api.colony.IColony;
import com.minecolonies.api.colony.IColonyView;
import com.minecolonies.api.colony.event.ColonyInformationChangedEvent;
import com.minecolonies.api.util.Log;
import com.minecolonies.core.Network;
import com.minecolonies.core.network.messages.server.AbstractColonyServerMessage;
import net.minecraft.network.FriendlyByteBuf;
Expand Down Expand Up @@ -56,15 +57,13 @@ protected void onExecute(final NetworkEvent.Context ctxIn, final boolean isLogic
{
name = (name.length() <= MAX_NAME_LENGTH) ? name : name.substring(0, SUBSTRING_LENGTH);
colony.setName(name);

if (ctxIn.getSender() != null)
try
{
Network.getNetwork().sendToEveryone(this);
MinecraftForge.EVENT_BUS.post(new ColonyInformationChangedEvent(colony, ColonyInformationChangedEvent.Type.NAME));
}

if (isLogicalServer)
catch (final Exception e)
{
MinecraftForge.EVENT_BUS.post(new ColonyInformationChangedEvent(colony, ColonyInformationChangedEvent.Type.NAME));
Log.getLogger().error("Error during ColonyInformationChangedEvent", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,14 @@ public void handle(
placementSettings.getMirror() != Mirror.NONE,
packName,
blueprintPath);
MinecraftForge.EVENT_BUS.post(new BlockEvent.EntityPlaceEvent(BlockSnapshot.create(world.dimension(), world, blockPos), world.getBlockState(blockPos.below()), player));
try
{
MinecraftForge.EVENT_BUS.post(new BlockEvent.EntityPlaceEvent(BlockSnapshot.create(world.dimension(), world, blockPos), world.getBlockState(blockPos.below()), player));
}
catch (final Exception e)
{
Log.getLogger().error("Error during EntityPlaceEvent", e);
}
InventoryUtils.reduceStackInItemHandler(new InvWrapper(player.getInventory()), inventoryStack, 1);

if (tempColony == null)
Expand Down

0 comments on commit c7e59c9

Please sign in to comment.