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 authored Aug 20, 2024
1 parent 95a1b50 commit 1b510b9
Show file tree
Hide file tree
Showing 13 changed files with 135 additions and 20 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 @@ -648,7 +648,14 @@ public void onWorldLoad(@NotNull final Level w)
c.onWorldLoad(world);
}

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

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

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

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

NeoForge.EVENT_BUS.post(new ColonyViewUpdatedEvent(view));
try
{
NeoForge.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 @@ -423,6 +423,13 @@ public void handleCustomRecipeManagerMessage(final RegistryFriendlyByteBuf buff)
lootTables.put(id, drops);
}

NeoForge.EVENT_BUS.post(new CustomRecipesReloadedEvent());
try
{
NeoForge.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 @@ -188,7 +189,14 @@ public void onServerResponseTriggered(final int responseId, final Player player,
MessageUtils.format(MESSAGE_RECRUITMENT_SUCCESS, data.getName()).sendTo(colony).forAllPlayers();
}

NeoForge.EVENT_BUS.post(new CitizenAddedEvent(newCitizen, CitizenAddedEvent.Source.HIRED));
try
{
NeoForge.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 @@ -358,8 +358,14 @@ public ICitizenData resurrectCivilianData(@NotNull final CompoundTag compoundNBT
citizens.put(citizenData.getId(), citizenData);
spawnOrCreateCitizen(citizenData, world, spawnPos);

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

try
{
NeoForge.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 @@ -614,8 +620,14 @@ else if (femaleCount < (getCitizens().size() - 1) / 2.0)

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

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

try
{
NeoForge.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.translatableEscape(COMMAND_CITIZEN_SPAWN_SUCCESS, newCitizen.getName()), true);

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

try
{
NeoForge.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 @@ -423,7 +423,14 @@ public void executeSpecificCompleteActions()
if (wo instanceof WorkOrderBuilding)
{
final IBuilding building = colony.getBuildingManager().getBuilding(wo.getLocation());
NeoForge.EVENT_BUS.post(new BuildingConstructionEvent(building, BuildingConstructionEvent.EventType.fromWorkOrderType(wo.getWorkOrderType())));
try
{
NeoForge.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 @@ -1654,7 +1654,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));

NeoForge.EVENT_BUS.post(new CitizenRemovedEvent(citizenData, damageSource));
try
{
NeoForge.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 @@ -7,6 +7,7 @@
import com.minecolonies.api.colony.IColonyManager;
import com.minecolonies.api.colony.buildings.IBuilding;
import com.minecolonies.api.colony.event.ColonyCreatedEvent;
import com.minecolonies.api.util.Log;
import com.minecolonies.core.network.messages.client.colony.OpenBuildingUIMessage;
import com.minecolonies.core.tileentities.TileEntityColonyBuilding;
import com.minecolonies.api.util.BlockPosUtil;
Expand Down Expand Up @@ -171,8 +172,14 @@ else if (spawnDistance > MineColonies.getConfig().getServer().maxDistanceFromWor

new OpenBuildingUIMessage(building).sendToPlayer(sender);;


NeoForge.EVENT_BUS.post(new ColonyCreatedEvent(createdColony));
try
{
NeoForge.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 @@ -5,6 +5,7 @@
import com.minecolonies.api.colony.IColony;
import com.minecolonies.api.colony.IColonyManager;
import com.minecolonies.api.colony.event.ColonyDeletedEvent;
import com.minecolonies.api.util.Log;
import com.minecolonies.api.util.MessageUtils;
import com.minecolonies.api.util.constant.Constants;
import net.minecraft.network.RegistryFriendlyByteBuf;
Expand Down Expand Up @@ -51,7 +52,14 @@ protected void onExecute(final IPayloadContext ctxIn, final ServerPlayer player)
IColonyManager.getInstance().deleteColonyByDimension(colony.getID(), false, colony.getDimension());
MessageUtils.format(MESSAGE_INFO_COLONY_DESTROY_SUCCESS).sendTo(player);

NeoForge.EVENT_BUS.post(new ColonyDeletedEvent(colony));
try
{
NeoForge.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 @@ -3,6 +3,7 @@
import com.ldtteam.common.network.PlayMessageType;
import com.minecolonies.api.colony.IColony;
import com.minecolonies.api.colony.event.ColonyInformationChangedEvent;
import com.minecolonies.api.util.Log;
import com.minecolonies.api.util.Utils;
import com.minecolonies.api.util.constant.Constants;
import com.minecolonies.core.network.messages.server.AbstractColonyServerMessage;
Expand Down Expand Up @@ -37,7 +38,14 @@ public ColonyFlagChangeMessage(final IColony colony, final BannerPatternLayers p
protected void onExecute(final IPayloadContext ctxIn, final ServerPlayer player, final IColony colony)
{
colony.setColonyFlag(patterns);
NeoForge.EVENT_BUS.post(new ColonyInformationChangedEvent(colony, ColonyInformationChangedEvent.Type.FLAG));
try
{
NeoForge.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 @@ -4,6 +4,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.api.util.constant.Constants;
import com.minecolonies.core.network.messages.server.AbstractColonyServerMessage;
import net.minecraft.ChatFormatting;
Expand Down Expand Up @@ -65,6 +66,13 @@ protected void toBytes(@NotNull final RegistryFriendlyByteBuf buf)
protected void onExecute(final IPayloadContext ctxIn, final ServerPlayer player, final IColony colony)
{
colony.setColonyColor(ChatFormatting.values()[colorOrdinal]);
NeoForge.EVENT_BUS.post(new ColonyInformationChangedEvent(colony, ColonyInformationChangedEvent.Type.TEAM_COLOR));
try
{
NeoForge.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 @@ -4,6 +4,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.api.util.constant.Constants;
import com.minecolonies.core.network.messages.server.AbstractColonyServerMessage;
import net.minecraft.network.RegistryFriendlyByteBuf;
Expand Down Expand Up @@ -53,6 +54,13 @@ protected void toBytes(@NotNull final RegistryFriendlyByteBuf buf)
protected void onExecute(final IPayloadContext ctxIn, final ServerPlayer player, final IColony colony)
{
colony.setName(name);
NeoForge.EVENT_BUS.post(new ColonyInformationChangedEvent(colony, ColonyInformationChangedEvent.Type.NAME));
try
{
NeoForge.EVENT_BUS.post(new ColonyInformationChangedEvent(colony, ColonyInformationChangedEvent.Type.NAME));
}
catch (final Exception e)
{
Log.getLogger().error("Error during ColonyInformationChangedEvent", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,14 @@ public void handle(
rotMir,
packName,
blueprintPath);
NeoForge.EVENT_BUS.post(new BlockEvent.EntityPlaceEvent(BlockSnapshot.create(world.dimension(), world, blockPos), world.getBlockState(blockPos.below()), player));
try
{
NeoForge.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 1b510b9

Please sign in to comment.