From 95bb1de2311f6cb99a299f964fb56e5d0013d332 Mon Sep 17 00:00:00 2001 From: someaddons <38401808+someaddons@users.noreply.github.com> Date: Sun, 25 Aug 2024 10:31:07 +0200 Subject: [PATCH] Add a few npe fixes and small adjustments to info commands (#10161) Add a few npe fixes and small adjustments to info commands --- .../colonycommands/CommandColonyInfo.java | 5 ++-- .../colonycommands/CommandListColonies.java | 12 ++++++---- .../colonycommands/CommandSetDeletable.java | 1 + .../core/entity/citizen/EntityCitizen.java | 24 +++++++++++++++---- .../core/items/ItemResourceScroll.java | 7 +++--- .../colony/PlaySoundForCitizenMessage.java | 17 +++++++------ .../messages/server/GetColonyInfoMessage.java | 7 +++++- 7 files changed, 46 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/minecolonies/core/commands/colonycommands/CommandColonyInfo.java b/src/main/java/com/minecolonies/core/commands/colonycommands/CommandColonyInfo.java index f37b07c66e2..55145e48abe 100755 --- a/src/main/java/com/minecolonies/core/commands/colonycommands/CommandColonyInfo.java +++ b/src/main/java/com/minecolonies/core/commands/colonycommands/CommandColonyInfo.java @@ -22,7 +22,7 @@ public class CommandColonyInfo implements IMCColonyOfficerCommand { public static final String ID_TEXT = "ID: "; public static final String NAME_TEXT = "Name: "; - private static final String MAYOR_TEXT = "Mayor: "; + public static final String MAYOR_TEXT = "Mayor: "; private static final String COORDINATES_TEXT = "Coordinates: "; private static final String COORDINATES_XYZ = "x=%s y=%s z=%s"; private static final String CITIZENS = "Citizens: "; @@ -54,7 +54,7 @@ public int onExecute(final CommandContext context) } final BlockPos position = colony.getCenter(); - context.getSource().sendSuccess(() -> Component.literal(ID_TEXT + colony.getID() + NAME_TEXT + colony.getName()), true); + context.getSource().sendSuccess(() -> Component.literal(ID_TEXT + colony.getID() + " " + NAME_TEXT + colony.getName()), true); final String mayor = colony.getPermissions().getOwnerName(); context.getSource().sendSuccess(() -> Component.literal(MAYOR_TEXT + mayor), true); context.getSource() @@ -63,7 +63,6 @@ public int onExecute(final CommandContext context) .sendSuccess(() -> Component.literal(COORDINATES_TEXT + String.format(COORDINATES_XYZ, position.getX(), position.getY(), position.getZ())).setStyle(Style.EMPTY.withColor( ChatFormatting.GREEN)), true); context.getSource().sendSuccess(() -> Component.literal(String.format(LAST_CONTACT_TEXT, colony.getLastContactInHours())), true); - context.getSource().sendSuccess(() -> Component.literal(IS_DELETABLE + !colony.canBeAutoDeleted()), true); if (!colony.getRaiderManager().canHaveRaiderEvents()) { diff --git a/src/main/java/com/minecolonies/core/commands/colonycommands/CommandListColonies.java b/src/main/java/com/minecolonies/core/commands/colonycommands/CommandListColonies.java index 3b28cc4b5de..f2745ebafd3 100755 --- a/src/main/java/com/minecolonies/core/commands/colonycommands/CommandListColonies.java +++ b/src/main/java/com/minecolonies/core/commands/colonycommands/CommandListColonies.java @@ -6,17 +6,18 @@ import com.mojang.brigadier.arguments.IntegerArgumentType; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.context.CommandContext; +import net.minecraft.ChatFormatting; import net.minecraft.commands.CommandSourceStack; import net.minecraft.core.BlockPos; import net.minecraft.network.chat.ClickEvent; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.MutableComponent; +import net.minecraft.network.chat.Style; import java.util.ArrayList; import java.util.List; -import net.minecraft.ChatFormatting; -import net.minecraft.network.chat.Component; -import net.minecraft.network.chat.MutableComponent; -import net.minecraft.network.chat.Style; +import static com.minecolonies.core.commands.colonycommands.CommandColonyInfo.MAYOR_TEXT; public class CommandListColonies implements IMCCommand { @@ -98,7 +99,8 @@ private int executeCommand(final CommandContext context, fin for (final IColony colony : coloniesPage) { context.getSource().sendSuccess(() -> Component.literal(String.format( - ID_AND_NAME_TEXT, colony.getID(), colony.getName())).setStyle(Style.EMPTY.withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, + ID_AND_NAME_TEXT, colony.getID(), colony.getName()) + " " + MAYOR_TEXT + colony.getPermissions().getOwnerName()) + .setStyle(Style.EMPTY.withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, String.format(COMMAND_COLONY_INFO, colony.getID())))), true); final BlockPos center = colony.getCenter(); diff --git a/src/main/java/com/minecolonies/core/commands/colonycommands/CommandSetDeletable.java b/src/main/java/com/minecolonies/core/commands/colonycommands/CommandSetDeletable.java index 723b6bdd5fa..951c170f58b 100755 --- a/src/main/java/com/minecolonies/core/commands/colonycommands/CommandSetDeletable.java +++ b/src/main/java/com/minecolonies/core/commands/colonycommands/CommandSetDeletable.java @@ -15,6 +15,7 @@ import static com.minecolonies.api.util.constant.translation.CommandTranslationConstants.COMMAND_COLONY_ID_NOT_FOUND; import static com.minecolonies.core.commands.CommandArgumentNames.COLONYID_ARG; +// TODO: Unused, maybe drop or add an auto delete feature public class CommandSetDeletable implements IMCOPCommand { diff --git a/src/main/java/com/minecolonies/core/entity/citizen/EntityCitizen.java b/src/main/java/com/minecolonies/core/entity/citizen/EntityCitizen.java index 50f1c9d9d58..e74e3b3e16b 100755 --- a/src/main/java/com/minecolonies/core/entity/citizen/EntityCitizen.java +++ b/src/main/java/com/minecolonies/core/entity/citizen/EntityCitizen.java @@ -28,9 +28,6 @@ import com.minecolonies.api.entity.citizen.citizenhandlers.*; import com.minecolonies.api.entity.citizen.happiness.ExpirationBasedHappinessModifier; import com.minecolonies.api.entity.citizen.happiness.StaticHappinessSupplier; -import com.minecolonies.api.util.constant.TranslationConstants; -import com.minecolonies.core.entity.ai.minimal.EntityAIFloat; -import com.minecolonies.core.entity.pathfinding.pathresults.PathResult; import com.minecolonies.api.entity.pathfinding.proxy.IWalkToProxy; import com.minecolonies.api.inventory.InventoryCitizen; import com.minecolonies.api.inventory.container.ContainerCitizenInventory; @@ -39,6 +36,7 @@ import com.minecolonies.api.util.*; import com.minecolonies.api.util.MessageUtils.MessagePriority; import com.minecolonies.api.util.constant.HappinessConstants; +import com.minecolonies.api.util.constant.TranslationConstants; import com.minecolonies.api.util.constant.TypeConstants; import com.minecolonies.core.MineColonies; import com.minecolonies.core.Network; @@ -52,14 +50,16 @@ import com.minecolonies.core.colony.jobs.JobNetherWorker; import com.minecolonies.core.colony.jobs.JobRanger; import com.minecolonies.core.entity.ai.minimal.EntityAICitizenChild; +import com.minecolonies.core.entity.ai.minimal.EntityAIFloat; import com.minecolonies.core.entity.ai.minimal.EntityAIInteractToggleAble; import com.minecolonies.core.entity.ai.minimal.LookAtEntityGoal; import com.minecolonies.core.entity.ai.workers.AbstractEntityAIBasic; import com.minecolonies.core.entity.ai.workers.CitizenAI; import com.minecolonies.core.entity.ai.workers.guard.AbstractEntityAIGuard; import com.minecolonies.core.entity.citizen.citizenhandlers.*; -import com.minecolonies.core.entity.pathfinding.proxy.EntityCitizenWalkToProxy; import com.minecolonies.core.entity.pathfinding.navigation.MovementHandler; +import com.minecolonies.core.entity.pathfinding.pathresults.PathResult; +import com.minecolonies.core.entity.pathfinding.proxy.EntityCitizenWalkToProxy; import com.minecolonies.core.event.EventHandler; import com.minecolonies.core.network.messages.client.ItemParticleEffectMessage; import com.minecolonies.core.network.messages.client.VanillaParticleMessage; @@ -92,7 +92,10 @@ import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.AbstractContainerMenu; -import net.minecraft.world.item.*; +import net.minecraft.world.item.BlockItem; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; +import net.minecraft.world.item.ShieldItem; import net.minecraft.world.level.ChunkPos; import net.minecraft.world.level.Level; import net.minecraft.world.phys.AABB; @@ -104,6 +107,7 @@ import net.minecraftforge.common.util.LazyOptional; import net.minecraftforge.items.IItemHandler; import net.minecraftforge.network.PacketDistributor; +import net.minecraftforge.registries.ForgeRegistries; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -1930,6 +1934,11 @@ public void refreshDimensions() @Override public void queueSound(@NotNull final SoundEvent soundEvent, final BlockPos pos, final int length, final int repetitions) { + if (soundEvent == null || !ForgeRegistries.SOUND_EVENTS.containsKey(soundEvent.getLocation())) + { + return; + } + Network.getNetwork().sendToPosition(new PlaySoundForCitizenMessage(this.getId(), soundEvent, this.getSoundSource(), pos, level, length, repetitions), new PacketDistributor.TargetPoint(pos.getX(), pos.getY(), pos.getZ(), BLOCK_BREAK_SOUND_RANGE, level.dimension())); } @@ -1937,6 +1946,11 @@ public void queueSound(@NotNull final SoundEvent soundEvent, final BlockPos pos, @Override public void queueSound(@NotNull final SoundEvent soundEvent, final BlockPos pos, final int length, final int repetitions, final float volume, final float pitch) { + if (soundEvent == null || !ForgeRegistries.SOUND_EVENTS.containsKey(soundEvent.getLocation())) + { + return; + } + Network.getNetwork() .sendToPosition(new PlaySoundForCitizenMessage(this.getId(), soundEvent, this.getSoundSource(), pos, level, volume, pitch, length, repetitions), new PacketDistributor.TargetPoint(pos.getX(), pos.getY(), pos.getZ(), BLOCK_BREAK_SOUND_RANGE, level.dimension())); diff --git a/src/main/java/com/minecolonies/core/items/ItemResourceScroll.java b/src/main/java/com/minecolonies/core/items/ItemResourceScroll.java index ee26dec9d29..936b2cc5df7 100755 --- a/src/main/java/com/minecolonies/core/items/ItemResourceScroll.java +++ b/src/main/java/com/minecolonies/core/items/ItemResourceScroll.java @@ -5,17 +5,16 @@ import com.minecolonies.api.colony.buildings.views.IBuildingView; import com.minecolonies.api.colony.workorders.IWorkOrderView; import com.minecolonies.api.tileentities.AbstractTileEntityColonyBuilding; -import com.minecolonies.core.client.gui.WindowResourceList; -import com.minecolonies.core.tileentities.TileEntityRack; import com.minecolonies.api.util.BlockPosUtil; import com.minecolonies.api.util.MessageUtils; import com.minecolonies.api.util.constant.TranslationConstants; -import com.minecolonies.core.MineColonies; import com.minecolonies.core.Network; +import com.minecolonies.core.client.gui.WindowResourceList; import com.minecolonies.core.colony.buildings.moduleviews.BuildingResourcesModuleView; import com.minecolonies.core.colony.buildings.workerbuildings.BuildingBuilder; import com.minecolonies.core.colony.buildings.workerbuildings.BuildingWareHouse; import com.minecolonies.core.network.messages.server.ResourceScrollSaveWarehouseSnapshotMessage; +import com.minecolonies.core.tileentities.TileEntityRack; import com.minecolonies.core.tileentities.TileEntityWareHouse; import net.minecraft.ChatFormatting; import net.minecraft.client.Minecraft; @@ -267,7 +266,7 @@ else if (buildingEntity.getBuilding() instanceof BuildingWareHouse) { MessageUtils.format(COM_MINECOLONIES_SCROLL_SNAPSHOT).sendTo(ctx.getPlayer()); } - else + else if (buildingEntity.getBuilding() != null) { final MutableComponent buildingTypeComponent = MessageUtils.format(buildingEntity.getBuilding().getBuildingType().getTranslationKey()).create(); MessageUtils.format(COM_MINECOLONIES_SCROLL_WRONG_BUILDING, buildingTypeComponent, buildingEntity.getColony().getName()).sendTo(ctx.getPlayer()); diff --git a/src/main/java/com/minecolonies/core/network/messages/client/colony/PlaySoundForCitizenMessage.java b/src/main/java/com/minecolonies/core/network/messages/client/colony/PlaySoundForCitizenMessage.java index 173c8e729de..8c4d959f51c 100644 --- a/src/main/java/com/minecolonies/core/network/messages/client/colony/PlaySoundForCitizenMessage.java +++ b/src/main/java/com/minecolonies/core/network/messages/client/colony/PlaySoundForCitizenMessage.java @@ -1,6 +1,5 @@ package com.minecolonies.core.network.messages.client.colony; -import com.minecolonies.api.colony.IColonyManager; import com.minecolonies.api.entity.citizen.AbstractCivilianEntity; import com.minecolonies.api.network.IMessage; import com.minecolonies.api.sounds.SoundManager; @@ -152,14 +151,14 @@ public PlaySoundForCitizenMessage(final int entityID, final SoundEvent event, fi public void toBytes(final FriendlyByteBuf buf) { buf.writeResourceLocation(ForgeRegistries.SOUND_EVENTS.getKey(this.soundEvent)); - buf.writeInt(this.soundSource.ordinal()); - buf.writeBlockPos(this.pos); - buf.writeUtf(this.dimensionID.location().toString()); - buf.writeFloat(this.volume); - buf.writeFloat(this.pitch); - buf.writeInt(this.length); - buf.writeInt(this.repetitions); - buf.writeInt(this.entityid); + buf.writeInt(soundSource.ordinal()); + buf.writeBlockPos(pos); + buf.writeUtf(dimensionID.location().toString()); + buf.writeFloat(volume); + buf.writeFloat(pitch); + buf.writeInt(length); + buf.writeInt(repetitions); + buf.writeInt(entityid); } @Override diff --git a/src/main/java/com/minecolonies/core/network/messages/server/GetColonyInfoMessage.java b/src/main/java/com/minecolonies/core/network/messages/server/GetColonyInfoMessage.java index 9bcc1a1d4f5..ebc7825b712 100755 --- a/src/main/java/com/minecolonies/core/network/messages/server/GetColonyInfoMessage.java +++ b/src/main/java/com/minecolonies/core/network/messages/server/GetColonyInfoMessage.java @@ -23,7 +23,7 @@ import org.jetbrains.annotations.Nullable; import static com.minecolonies.api.util.constant.BuildingConstants.DEACTIVATED; -import static com.minecolonies.api.util.constant.TranslationConstants.*; +import static com.minecolonies.api.util.constant.TranslationConstants.HUT_BLOCK_MISSING_BUILDING; import static com.minecolonies.core.MineColonies.getConfig; /** @@ -111,6 +111,11 @@ else if (world.getBlockEntity(pos) instanceof TileEntityColonyBuilding townhall } else { + if (nextColony == null) + { + return; + } + final int blockRange = Math.max(MineColonies.getConfig().getServer().minColonyDistance.get(), getConfig().getServer().initialColonySize.get()) << 4; final int distance = (int) BlockPosUtil.getDistance(pos, nextColony.getCenter());