From 27cc9d0730c72fdb9ed2283d0e33e6acfa5a8c84 Mon Sep 17 00:00:00 2001 From: modmuss50 Date: Wed, 2 Mar 2022 01:50:38 +0000 Subject: [PATCH] Refactor sendNoSpamMessage to use a packet, fixes a number of issues where no chat messsage was being received at all. More work towards splitting client and server. --- .../network/ClientBoundPacketHandlers.java | 19 +++++- .../common/network/ClientBoundPackets.java | 7 +++ .../reborncore/common/util/ChatUtils.java | 33 ++-------- .../reborncore/common/util/ItemUtils.java | 61 ++++++++++--------- .../machine/misc/AlarmBlockEntity.java | 10 ++- .../machine/tier1/PlayerDetectorBlock.java | 19 +++--- .../techreborn/blocks/misc/BlockAlarm.java | 4 +- .../java/techreborn/items/BatteryItem.java | 4 +- .../items/FrequencyTransmitterItem.java | 45 +++++++------- .../tool/advanced/AdvancedJackhammerItem.java | 4 +- .../industrial/IndustrialChainsawItem.java | 4 +- .../tool/industrial/IndustrialDrillItem.java | 4 +- .../industrial/IndustrialJackhammerItem.java | 21 ++++--- .../items/tool/industrial/NanosaberItem.java | 4 +- 14 files changed, 123 insertions(+), 116 deletions(-) diff --git a/RebornCore/src/main/java/reborncore/common/network/ClientBoundPacketHandlers.java b/RebornCore/src/main/java/reborncore/common/network/ClientBoundPacketHandlers.java index 9cf3d7ba3..92407c3ce 100644 --- a/RebornCore/src/main/java/reborncore/common/network/ClientBoundPacketHandlers.java +++ b/RebornCore/src/main/java/reborncore/common/network/ClientBoundPacketHandlers.java @@ -30,22 +30,25 @@ import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.block.entity.BlockEntity; import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.hud.ChatHud; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.screen.ingame.HandledScreen; import net.minecraft.nbt.NbtCompound; import net.minecraft.screen.ScreenHandler; +import net.minecraft.text.Text; import net.minecraft.util.Identifier; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; -import org.slf4j.LoggerFactory; import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import reborncore.RebornCore; import reborncore.client.ClientChunkManager; -import reborncore.common.screen.BuiltScreenHandler; import reborncore.common.blockentity.FluidConfiguration; import reborncore.common.blockentity.MachineBaseBlockEntity; import reborncore.common.blockentity.SlotConfiguration; import reborncore.common.chunkloading.ChunkLoaderManager; +import reborncore.common.screen.BuiltScreenHandler; +import reborncore.mixin.client.AccessorChatHud; @Environment(EnvType.CLIENT) public class ClientBoundPacketHandlers { @@ -136,6 +139,18 @@ public class ClientBoundPacketHandlers { }); NetworkManager.registerClientBoundHandler(new Identifier("reborncore", "sync_chunks"), ChunkLoaderManager.CODEC, ClientChunkManager::setLoadedChunks); + + NetworkManager.registerClientBoundHandler(new Identifier("reborncore", "no_spam_chat"), (client, handler, buf, responseSender) -> { + final int messageId = buf.readInt(); + final Text text = buf.readText(); + + client.execute(() -> { + int deleteID = RebornCore.MOD_ID.hashCode() + messageId; + ChatHud chat = MinecraftClient.getInstance().inGameHud.getChatHud(); + AccessorChatHud accessorChatHud = (AccessorChatHud) chat; + accessorChatHud.invokeAddMessage(text, deleteID); + }); + }); } } diff --git a/RebornCore/src/main/java/reborncore/common/network/ClientBoundPackets.java b/RebornCore/src/main/java/reborncore/common/network/ClientBoundPackets.java index b2e788372..23c7a7705 100644 --- a/RebornCore/src/main/java/reborncore/common/network/ClientBoundPackets.java +++ b/RebornCore/src/main/java/reborncore/common/network/ClientBoundPackets.java @@ -28,6 +28,7 @@ import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import net.minecraft.block.entity.BlockEntity; import net.minecraft.nbt.NbtCompound; import net.minecraft.screen.ScreenHandler; +import net.minecraft.text.Text; import net.minecraft.util.Identifier; import net.minecraft.util.math.BlockPos; import reborncore.common.blockentity.FluidConfiguration; @@ -79,4 +80,10 @@ public class ClientBoundPackets { return NetworkManager.createClientBoundPacket(new Identifier("reborncore", "sync_chunks"), ChunkLoaderManager.CODEC, chunks); } + public static IdentifiedPacket createPacketNoSpamMessage(int messageId, Text text) { + return NetworkManager.createClientBoundPacket(new Identifier("reborncore", "no_spam_chat"), packetBuffer -> { + packetBuffer.writeInt(messageId); + packetBuffer.writeText(text); + }); + } } diff --git a/RebornCore/src/main/java/reborncore/common/util/ChatUtils.java b/RebornCore/src/main/java/reborncore/common/util/ChatUtils.java index c3b7fc348..9937262aa 100644 --- a/RebornCore/src/main/java/reborncore/common/util/ChatUtils.java +++ b/RebornCore/src/main/java/reborncore/common/util/ChatUtils.java @@ -24,37 +24,14 @@ package reborncore.common.util; -import net.fabricmc.api.EnvType; -import net.fabricmc.api.Environment; -import net.fabricmc.loader.api.FabricLoader; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.gui.hud.ChatHud; +import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; -import reborncore.mixin.client.AccessorChatHud; - -/** - * Class stolen from SteamAgeRevolution, which I stole from BloodMagic, which was stolen from EnderCore, which stole the - * idea from ExtraUtilities, who stole it from vanilla. - *

- * Original class link: - * https://github.com/SleepyTrousers/EnderCore/blob/master/src/main/java/com/enderio/core/common/util/ChatUtil.java - *

- */ +import reborncore.common.network.ClientBoundPackets; +import reborncore.common.network.NetworkManager; public class ChatUtils { - private static final int DELETION_ID = 1337; // MAKE THIS UNIQUE PER MOD THAT USES THIS - public static void sendNoSpamMessages(int messageID, Text message) { - if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) { - sendNoSpamMessage(messageID, message); - } - } - - @Environment(EnvType.CLIENT) - private static void sendNoSpamMessage(int messageID, Text message) { - int deleteID = DELETION_ID + messageID; - ChatHud chat = MinecraftClient.getInstance().inGameHud.getChatHud(); - AccessorChatHud accessorChatHud = (AccessorChatHud) chat; - accessorChatHud.invokeAddMessage(message, deleteID); + public static void sendNoSpamMessage(ServerPlayerEntity player, int messageID, Text message) { + NetworkManager.sendToPlayer(ClientBoundPackets.createPacketNoSpamMessage(messageID, message), player); } } diff --git a/RebornCore/src/main/java/reborncore/common/util/ItemUtils.java b/RebornCore/src/main/java/reborncore/common/util/ItemUtils.java index bbb4fbfe7..04cf38190 100644 --- a/RebornCore/src/main/java/reborncore/common/util/ItemUtils.java +++ b/RebornCore/src/main/java/reborncore/common/util/ItemUtils.java @@ -28,9 +28,11 @@ import net.fabricmc.fabric.api.transfer.v1.context.ContainerItemContext; import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant; import net.fabricmc.fabric.api.transfer.v1.item.PlayerInventoryStorage; import net.fabricmc.fabric.api.transfer.v1.storage.base.SingleSlotStorage; +import net.minecraft.entity.Entity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NbtCompound; +import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; import net.minecraft.text.TranslatableText; import net.minecraft.util.Formatting; @@ -159,26 +161,27 @@ public class ItemUtils { * * @param stack {@link ItemStack} Stack to check * @param cost {@link int} Cost of operation performed by tool - * @param isClient {@link boolean} Client side * @param messageId {@link int} MessageID for sending no spam message */ - public static void checkActive(ItemStack stack, int cost, boolean isClient, int messageId) { + public static void checkActive(ItemStack stack, int cost, int messageId, Entity player) { if (!ItemUtils.isActive(stack)) { return; } if (((RcEnergyItem) stack.getItem()).getStoredEnergy(stack) >= cost) { return; } - if (isClient) { - ChatUtils.sendNoSpamMessages(messageId, new TranslatableText("reborncore.message.energyError") - .formatted(Formatting.GRAY) - .append(" ") - .append( - new TranslatableText("reborncore.message.deactivating") - .formatted(Formatting.GOLD) - ) + + if (player instanceof ServerPlayerEntity serverPlayerEntity) { + ChatUtils.sendNoSpamMessage(serverPlayerEntity, messageId, new TranslatableText("reborncore.message.energyError") + .formatted(Formatting.GRAY) + .append(" ") + .append( + new TranslatableText("reborncore.message.deactivating") + .formatted(Formatting.GOLD) + ) ); } + stack.getOrCreateNbt().putBoolean("isActive", false); } @@ -187,36 +190,34 @@ public class ItemUtils { * * @param stack {@link ItemStack} Stack to switch state * @param cost {@code int} Cost of operation performed by tool - * @param isClient {@code boolean} Are we on client side * @param messageId {@code int} MessageID for sending no spam message */ - public static void switchActive(ItemStack stack, int cost, boolean isClient, int messageId) { - ItemUtils.checkActive(stack, cost, isClient, messageId); + public static void switchActive(ItemStack stack, int cost, int messageId, Entity entity) { + ItemUtils.checkActive(stack, cost, messageId, entity); if (!ItemUtils.isActive(stack)) { stack.getOrCreateNbt().putBoolean("isActive", true); - if (isClient) { - - ChatUtils.sendNoSpamMessages(messageId, new TranslatableText("reborncore.message.setTo") - .formatted(Formatting.GRAY) - .append(" ") - .append( - new TranslatableText("reborncore.message.active") - .formatted(Formatting.GOLD) - ) + if (entity instanceof ServerPlayerEntity serverPlayerEntity) { + ChatUtils.sendNoSpamMessage(serverPlayerEntity, messageId, new TranslatableText("reborncore.message.setTo") + .formatted(Formatting.GRAY) + .append(" ") + .append( + new TranslatableText("reborncore.message.active") + .formatted(Formatting.GOLD) + ) ); } } else { stack.getOrCreateNbt().putBoolean("isActive", false); - if (isClient) { - ChatUtils.sendNoSpamMessages(messageId, new TranslatableText("reborncore.message.setTo") - .formatted(Formatting.GRAY) - .append(" ") - .append( - new TranslatableText("reborncore.message.inactive") - .formatted(Formatting.GOLD) - ) + if (entity instanceof ServerPlayerEntity serverPlayerEntity) { + ChatUtils.sendNoSpamMessage(serverPlayerEntity, messageId, new TranslatableText("reborncore.message.setTo") + .formatted(Formatting.GRAY) + .append(" ") + .append( + new TranslatableText("reborncore.message.inactive") + .formatted(Formatting.GOLD) + ) ); } } diff --git a/src/main/java/techreborn/blockentity/machine/misc/AlarmBlockEntity.java b/src/main/java/techreborn/blockentity/machine/misc/AlarmBlockEntity.java index 14e701eaa..95a1bc4c0 100644 --- a/src/main/java/techreborn/blockentity/machine/misc/AlarmBlockEntity.java +++ b/src/main/java/techreborn/blockentity/machine/misc/AlarmBlockEntity.java @@ -27,9 +27,11 @@ package techreborn.blockentity.machine.misc; import net.minecraft.block.BlockState; import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.BlockEntityTicker; +import net.minecraft.entity.Entity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NbtCompound; +import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.sound.SoundCategory; import net.minecraft.text.TranslatableText; import net.minecraft.util.Formatting; @@ -51,8 +53,8 @@ public class AlarmBlockEntity extends BlockEntity super(TRBlockEntities.ALARM, pos, state); } - public void rightClick() { - if (world == null || world.isClient) return; + public void rightClick(Entity entity) { + if (world == null) return; if (selectedSound < 3) { selectedSound++; @@ -60,10 +62,12 @@ public class AlarmBlockEntity extends BlockEntity selectedSound = 1; } - ChatUtils.sendNoSpamMessages(MessageIDs.alarmID, new TranslatableText("techreborn.message.alarm") + if (entity instanceof ServerPlayerEntity serverPlayerEntity) { + ChatUtils.sendNoSpamMessage(serverPlayerEntity, MessageIDs.alarmID, new TranslatableText("techreborn.message.alarm") .formatted(Formatting.GRAY) .append(" Alarm ") .append(String.valueOf(selectedSound))); + } } // BlockEntity diff --git a/src/main/java/techreborn/blocks/machine/tier1/PlayerDetectorBlock.java b/src/main/java/techreborn/blocks/machine/tier1/PlayerDetectorBlock.java index 2cefd1814..d24a9b9d8 100644 --- a/src/main/java/techreborn/blocks/machine/tier1/PlayerDetectorBlock.java +++ b/src/main/java/techreborn/blocks/machine/tier1/PlayerDetectorBlock.java @@ -32,6 +32,7 @@ import net.minecraft.block.entity.BlockEntity; import net.minecraft.entity.LivingEntity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemStack; +import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.state.StateManager; import net.minecraft.state.property.EnumProperty; import net.minecraft.text.LiteralText; @@ -123,15 +124,15 @@ public class PlayerDetectorBlock extends BlockMachineBase { } } - if (worldIn.isClient) { - ChatUtils.sendNoSpamMessages(MessageIDs.playerDetectorID, - new TranslatableText("techreborn.message.detects") - .formatted(Formatting.GRAY) - .append(" ") - .append( - new LiteralText(StringUtils.toFirstCapital(newType.asString())) - .formatted(color) - ) + if (playerIn instanceof ServerPlayerEntity serverPlayerEntity) { + ChatUtils.sendNoSpamMessage(serverPlayerEntity, MessageIDs.playerDetectorID, + new TranslatableText("techreborn.message.detects") + .formatted(Formatting.GRAY) + .append(" ") + .append( + new LiteralText(StringUtils.toFirstCapital(newType.asString())) + .formatted(color) + ) ); } diff --git a/src/main/java/techreborn/blocks/misc/BlockAlarm.java b/src/main/java/techreborn/blocks/misc/BlockAlarm.java index d80f3e7d3..ed77cefc5 100644 --- a/src/main/java/techreborn/blocks/misc/BlockAlarm.java +++ b/src/main/java/techreborn/blocks/misc/BlockAlarm.java @@ -138,8 +138,8 @@ public class BlockAlarm extends BaseBlockEntityProvider { } } - if (!worldIn.isClient && playerIn.isSneaking()) { - ((AlarmBlockEntity) blockEntity).rightClick(); + if (playerIn.isSneaking()) { + ((AlarmBlockEntity) blockEntity).rightClick(playerIn); return ActionResult.SUCCESS; } diff --git a/src/main/java/techreborn/items/BatteryItem.java b/src/main/java/techreborn/items/BatteryItem.java index e24461782..c2f40120c 100644 --- a/src/main/java/techreborn/items/BatteryItem.java +++ b/src/main/java/techreborn/items/BatteryItem.java @@ -65,7 +65,7 @@ public class BatteryItem extends Item implements RcEnergyItem { public TypedActionResult use(final World world, final PlayerEntity player, final Hand hand) { final ItemStack stack = player.getStackInHand(hand); if (player.isSneaking()) { - ItemUtils.switchActive(stack, 1, world.isClient, MessageIDs.poweredToolID); + ItemUtils.switchActive(stack, 1, MessageIDs.poweredToolID, player); return new TypedActionResult<>(ActionResult.SUCCESS, stack); } return new TypedActionResult<>(ActionResult.PASS, stack); @@ -73,7 +73,7 @@ public class BatteryItem extends Item implements RcEnergyItem { @Override public void inventoryTick(ItemStack stack, World world, Entity entity, int slot, boolean selected) { - ItemUtils.checkActive(stack, 1, entity.world.isClient, MessageIDs.poweredToolID); + ItemUtils.checkActive(stack, 1, MessageIDs.poweredToolID, entity); if (world.isClient) { return; } diff --git a/src/main/java/techreborn/items/FrequencyTransmitterItem.java b/src/main/java/techreborn/items/FrequencyTransmitterItem.java index 82c777b18..8a13e5f07 100644 --- a/src/main/java/techreborn/items/FrequencyTransmitterItem.java +++ b/src/main/java/techreborn/items/FrequencyTransmitterItem.java @@ -32,6 +32,7 @@ import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.item.ItemUsageContext; import net.minecraft.nbt.NbtOps; +import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.LiteralText; import net.minecraft.text.Text; import net.minecraft.text.TranslatableText; @@ -66,20 +67,20 @@ public class FrequencyTransmitterItem extends Item { GlobalPos.CODEC.encodeStart(NbtOps.INSTANCE, globalPos).result() .ifPresent(tag -> stack.getOrCreateNbt().put("pos", tag)); - if (!world.isClient) { - - ChatUtils.sendNoSpamMessages(MessageIDs.freqTransmitterID, new TranslatableText("techreborn.message.setTo") - .append(new LiteralText(" X:").formatted(Formatting.GRAY)) - .append(new LiteralText(String.valueOf(pos.getX())).formatted(Formatting.GOLD)) - .append(new LiteralText(" Y:").formatted(Formatting.GRAY)) - .append(new LiteralText(String.valueOf(pos.getY())).formatted(Formatting.GOLD)) - .append(new LiteralText(" Z:").formatted(Formatting.GRAY)) - .append(new LiteralText(String.valueOf(pos.getZ())).formatted(Formatting.GOLD)) - .append(" ") - .append(new TranslatableText("techreborn.message.in").formatted(Formatting.GRAY)) - .append(" ") - .append(new LiteralText(getDimName(globalPos.getDimension()).toString()).formatted(Formatting.GOLD))); + if (context.getPlayer() instanceof ServerPlayerEntity serverPlayerEntity) { + ChatUtils.sendNoSpamMessage(serverPlayerEntity, MessageIDs.freqTransmitterID, new TranslatableText("techreborn.message.setTo") + .append(new LiteralText(" X:").formatted(Formatting.GRAY)) + .append(new LiteralText(String.valueOf(pos.getX())).formatted(Formatting.GOLD)) + .append(new LiteralText(" Y:").formatted(Formatting.GRAY)) + .append(new LiteralText(String.valueOf(pos.getY())).formatted(Formatting.GOLD)) + .append(new LiteralText(" Z:").formatted(Formatting.GRAY)) + .append(new LiteralText(String.valueOf(pos.getZ())).formatted(Formatting.GOLD)) + .append(" ") + .append(new TranslatableText("techreborn.message.in").formatted(Formatting.GRAY)) + .append(" ") + .append(new LiteralText(getDimName(globalPos.getDimension()).toString()).formatted(Formatting.GOLD))); } + return ActionResult.SUCCESS; } @@ -96,16 +97,16 @@ public class FrequencyTransmitterItem extends Item { ItemStack stack = player.getStackInHand(hand); if (player.isSneaking()) { stack.setNbt(null); - if (!world.isClient) { - ChatUtils.sendNoSpamMessages(MessageIDs.freqTransmitterID, - new TranslatableText("techreborn.message.coordsHaveBeen") - .formatted(Formatting.GRAY) - .append(" ") - .append( - new TranslatableText("techreborn.message.cleared") - .formatted(Formatting.GOLD) - ) + if (player instanceof ServerPlayerEntity serverPlayerEntity) { + ChatUtils.sendNoSpamMessage(serverPlayerEntity, MessageIDs.freqTransmitterID, + new TranslatableText("techreborn.message.coordsHaveBeen") + .formatted(Formatting.GRAY) + .append(" ") + .append( + new TranslatableText("techreborn.message.cleared") + .formatted(Formatting.GOLD) + ) ); } } diff --git a/src/main/java/techreborn/items/tool/advanced/AdvancedJackhammerItem.java b/src/main/java/techreborn/items/tool/advanced/AdvancedJackhammerItem.java index a6ce6224e..c2a905e4d 100644 --- a/src/main/java/techreborn/items/tool/advanced/AdvancedJackhammerItem.java +++ b/src/main/java/techreborn/items/tool/advanced/AdvancedJackhammerItem.java @@ -90,7 +90,7 @@ public class AdvancedJackhammerItem extends JackhammerItem implements MultiBlock public TypedActionResult use(final World world, final PlayerEntity player, final Hand hand) { final ItemStack stack = player.getStackInHand(hand); if (player.isSneaking()) { - ItemUtils.switchActive(stack, cost, world.isClient, MessageIDs.poweredToolID); + ItemUtils.switchActive(stack, cost, MessageIDs.poweredToolID, player); return new TypedActionResult<>(ActionResult.SUCCESS, stack); } return new TypedActionResult<>(ActionResult.PASS, stack); @@ -98,7 +98,7 @@ public class AdvancedJackhammerItem extends JackhammerItem implements MultiBlock @Override public void inventoryTick(ItemStack stack, World world, Entity entity, int slot, boolean selected) { - ItemUtils.checkActive(stack, cost, entity.world.isClient, MessageIDs.poweredToolID); + ItemUtils.checkActive(stack, cost, MessageIDs.poweredToolID, entity); } @Environment(EnvType.CLIENT) diff --git a/src/main/java/techreborn/items/tool/industrial/IndustrialChainsawItem.java b/src/main/java/techreborn/items/tool/industrial/IndustrialChainsawItem.java index 967462ab0..a39522f51 100644 --- a/src/main/java/techreborn/items/tool/industrial/IndustrialChainsawItem.java +++ b/src/main/java/techreborn/items/tool/industrial/IndustrialChainsawItem.java @@ -108,7 +108,7 @@ public class IndustrialChainsawItem extends ChainsawItem { public TypedActionResult use(final World world, final PlayerEntity player, final Hand hand) { final ItemStack stack = player.getStackInHand(hand); if (player.isSneaking()) { - ItemUtils.switchActive(stack, cost, world.isClient, MessageIDs.poweredToolID); + ItemUtils.switchActive(stack, cost, MessageIDs.poweredToolID, player); return new TypedActionResult<>(ActionResult.SUCCESS, stack); } return new TypedActionResult<>(ActionResult.PASS, stack); @@ -116,7 +116,7 @@ public class IndustrialChainsawItem extends ChainsawItem { @Override public void usageTick(World world, LivingEntity entity, ItemStack stack, int i) { - ItemUtils.checkActive(stack, cost, entity.world.isClient, MessageIDs.poweredToolID); + ItemUtils.checkActive(stack, cost, MessageIDs.poweredToolID, entity); } @Environment(EnvType.CLIENT) diff --git a/src/main/java/techreborn/items/tool/industrial/IndustrialDrillItem.java b/src/main/java/techreborn/items/tool/industrial/IndustrialDrillItem.java index 4408a7a3f..8aa6e73ab 100644 --- a/src/main/java/techreborn/items/tool/industrial/IndustrialDrillItem.java +++ b/src/main/java/techreborn/items/tool/industrial/IndustrialDrillItem.java @@ -103,7 +103,7 @@ public class IndustrialDrillItem extends DrillItem { public TypedActionResult use(final World world, final PlayerEntity player, final Hand hand) { final ItemStack stack = player.getStackInHand(hand); if (player.isSneaking()) { - ItemUtils.switchActive(stack, cost, world.isClient, MessageIDs.poweredToolID); + ItemUtils.switchActive(stack, cost, MessageIDs.poweredToolID, player); return new TypedActionResult<>(ActionResult.SUCCESS, stack); } return new TypedActionResult<>(ActionResult.PASS, stack); @@ -111,7 +111,7 @@ public class IndustrialDrillItem extends DrillItem { @Override public void usageTick(World world, LivingEntity entity, ItemStack stack, int i) { - ItemUtils.checkActive(stack, cost, entity.world.isClient, MessageIDs.poweredToolID); + ItemUtils.checkActive(stack, cost, MessageIDs.poweredToolID, entity); } @Environment(EnvType.CLIENT) diff --git a/src/main/java/techreborn/items/tool/industrial/IndustrialJackhammerItem.java b/src/main/java/techreborn/items/tool/industrial/IndustrialJackhammerItem.java index 180d41faa..a3af1c5d8 100644 --- a/src/main/java/techreborn/items/tool/industrial/IndustrialJackhammerItem.java +++ b/src/main/java/techreborn/items/tool/industrial/IndustrialJackhammerItem.java @@ -32,6 +32,7 @@ import net.minecraft.entity.Entity; import net.minecraft.entity.LivingEntity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemStack; +import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.LiteralText; import net.minecraft.text.Text; import net.minecraft.text.TranslatableText; @@ -64,22 +65,22 @@ public class IndustrialJackhammerItem extends JackhammerItem implements MultiBlo } // Cycle Inactive, Active 3*3 and Active 5*5 - private void switchAOE(ItemStack stack, int cost, boolean isClient, int messageId) { - ItemUtils.checkActive(stack, cost, isClient, messageId); + private void switchAOE(ItemStack stack, int cost, int messageId, Entity entity) { + ItemUtils.checkActive(stack, cost, messageId, entity); if (!ItemUtils.isActive(stack)) { - ItemUtils.switchActive(stack, cost, isClient, messageId); + ItemUtils.switchActive(stack, cost, messageId, entity); stack.getOrCreateNbt().putBoolean("AOE5", false); - if (isClient) { - ChatUtils.sendNoSpamMessages(messageId, new TranslatableText("techreborn.message.setTo").formatted(Formatting.GRAY).append(" ").append(new LiteralText("3*3").formatted(Formatting.GOLD))); + if (entity instanceof ServerPlayerEntity serverPlayerEntity) { + ChatUtils.sendNoSpamMessage(serverPlayerEntity, messageId, new TranslatableText("techreborn.message.setTo").formatted(Formatting.GRAY).append(" ").append(new LiteralText("3*3").formatted(Formatting.GOLD))); } } else { if (isAOE5(stack)) { - ItemUtils.switchActive(stack, cost, isClient, messageId); + ItemUtils.switchActive(stack, cost, messageId, entity); stack.getOrCreateNbt().putBoolean("AOE5", false); } else { stack.getOrCreateNbt().putBoolean("AOE5", true); - if (isClient) { - ChatUtils.sendNoSpamMessages(messageId, new TranslatableText("techreborn.message.setTo").formatted(Formatting.GRAY).append(" ").append(new LiteralText("5*5").formatted(Formatting.GOLD))); + if (entity instanceof ServerPlayerEntity serverPlayerEntity) { + ChatUtils.sendNoSpamMessage(serverPlayerEntity, messageId, new TranslatableText("techreborn.message.setTo").formatted(Formatting.GRAY).append(" ").append(new LiteralText("5*5").formatted(Formatting.GOLD))); } } } @@ -134,7 +135,7 @@ public class IndustrialJackhammerItem extends JackhammerItem implements MultiBlo public TypedActionResult use(final World world, final PlayerEntity player, final Hand hand) { final ItemStack stack = player.getStackInHand(hand); if (player.isSneaking()) { - switchAOE(stack, cost, world.isClient, MessageIDs.poweredToolID); + switchAOE(stack, cost, MessageIDs.poweredToolID, player); return new TypedActionResult<>(ActionResult.SUCCESS, stack); } return new TypedActionResult<>(ActionResult.PASS, stack); @@ -142,7 +143,7 @@ public class IndustrialJackhammerItem extends JackhammerItem implements MultiBlo @Override public void inventoryTick(ItemStack stack, World world, Entity entity, int slot, boolean selected) { - ItemUtils.checkActive(stack, cost, entity.world.isClient, MessageIDs.poweredToolID); + ItemUtils.checkActive(stack, cost, MessageIDs.poweredToolID, entity); } @Environment(EnvType.CLIENT) diff --git a/src/main/java/techreborn/items/tool/industrial/NanosaberItem.java b/src/main/java/techreborn/items/tool/industrial/NanosaberItem.java index 80e44f482..c69bfd4f9 100644 --- a/src/main/java/techreborn/items/tool/industrial/NanosaberItem.java +++ b/src/main/java/techreborn/items/tool/industrial/NanosaberItem.java @@ -81,14 +81,14 @@ public class NanosaberItem extends SwordItem implements RcEnergyItem, ItemStackM // Item @Override public void inventoryTick(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) { - ItemUtils.checkActive(stack, cost, entityIn.world.isClient, MessageIDs.poweredToolID); + ItemUtils.checkActive(stack, cost, MessageIDs.poweredToolID, entityIn); } @Override public TypedActionResult use(final World world, final PlayerEntity player, final Hand hand) { final ItemStack stack = player.getStackInHand(hand); if (player.isSneaking()) { - ItemUtils.switchActive(stack, cost, world.isClient, MessageIDs.poweredToolID); + ItemUtils.switchActive(stack, cost, MessageIDs.poweredToolID, player); return new TypedActionResult<>(ActionResult.SUCCESS, stack); } return new TypedActionResult<>(ActionResult.PASS, stack);