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.
This commit is contained in:
modmuss50 2022-03-02 01:50:38 +00:00
parent 45c1e7412c
commit 27cc9d0730
14 changed files with 123 additions and 116 deletions

View file

@ -30,22 +30,25 @@ import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment; import net.fabricmc.api.Environment;
import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.BlockEntity;
import net.minecraft.client.MinecraftClient; 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.Screen;
import net.minecraft.client.gui.screen.ingame.HandledScreen; import net.minecraft.client.gui.screen.ingame.HandledScreen;
import net.minecraft.nbt.NbtCompound; import net.minecraft.nbt.NbtCompound;
import net.minecraft.screen.ScreenHandler; import net.minecraft.screen.ScreenHandler;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World; import net.minecraft.world.World;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reborncore.RebornCore; import reborncore.RebornCore;
import reborncore.client.ClientChunkManager; import reborncore.client.ClientChunkManager;
import reborncore.common.screen.BuiltScreenHandler;
import reborncore.common.blockentity.FluidConfiguration; import reborncore.common.blockentity.FluidConfiguration;
import reborncore.common.blockentity.MachineBaseBlockEntity; import reborncore.common.blockentity.MachineBaseBlockEntity;
import reborncore.common.blockentity.SlotConfiguration; import reborncore.common.blockentity.SlotConfiguration;
import reborncore.common.chunkloading.ChunkLoaderManager; import reborncore.common.chunkloading.ChunkLoaderManager;
import reborncore.common.screen.BuiltScreenHandler;
import reborncore.mixin.client.AccessorChatHud;
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public class ClientBoundPacketHandlers { 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", "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);
});
});
} }
} }

View file

@ -28,6 +28,7 @@ import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.BlockEntity;
import net.minecraft.nbt.NbtCompound; import net.minecraft.nbt.NbtCompound;
import net.minecraft.screen.ScreenHandler; import net.minecraft.screen.ScreenHandler;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import reborncore.common.blockentity.FluidConfiguration; import reborncore.common.blockentity.FluidConfiguration;
@ -79,4 +80,10 @@ public class ClientBoundPackets {
return NetworkManager.createClientBoundPacket(new Identifier("reborncore", "sync_chunks"), ChunkLoaderManager.CODEC, chunks); 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);
});
}
} }

View file

@ -24,37 +24,14 @@
package reborncore.common.util; package reborncore.common.util;
import net.fabricmc.api.EnvType; import net.minecraft.server.network.ServerPlayerEntity;
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.text.Text; import net.minecraft.text.Text;
import reborncore.mixin.client.AccessorChatHud; import reborncore.common.network.ClientBoundPackets;
import reborncore.common.network.NetworkManager;
/**
* 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.
* <p>
* Original class link:
* https://github.com/SleepyTrousers/EnderCore/blob/master/src/main/java/com/enderio/core/common/util/ChatUtil.java
* </p>
*/
public class ChatUtils { 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) { public static void sendNoSpamMessage(ServerPlayerEntity player, int messageID, Text message) {
if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) { NetworkManager.sendToPlayer(ClientBoundPackets.createPacketNoSpamMessage(messageID, message), player);
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);
} }
} }

View file

@ -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.ItemVariant;
import net.fabricmc.fabric.api.transfer.v1.item.PlayerInventoryStorage; import net.fabricmc.fabric.api.transfer.v1.item.PlayerInventoryStorage;
import net.fabricmc.fabric.api.transfer.v1.storage.base.SingleSlotStorage; import net.fabricmc.fabric.api.transfer.v1.storage.base.SingleSlotStorage;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound; import net.minecraft.nbt.NbtCompound;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText; import net.minecraft.text.TranslatableText;
import net.minecraft.util.Formatting; import net.minecraft.util.Formatting;
@ -159,26 +161,27 @@ public class ItemUtils {
* *
* @param stack {@link ItemStack} Stack to check * @param stack {@link ItemStack} Stack to check
* @param cost {@link int} Cost of operation performed by tool * @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 * @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)) { if (!ItemUtils.isActive(stack)) {
return; return;
} }
if (((RcEnergyItem) stack.getItem()).getStoredEnergy(stack) >= cost) { if (((RcEnergyItem) stack.getItem()).getStoredEnergy(stack) >= cost) {
return; return;
} }
if (isClient) {
ChatUtils.sendNoSpamMessages(messageId, new TranslatableText("reborncore.message.energyError") if (player instanceof ServerPlayerEntity serverPlayerEntity) {
.formatted(Formatting.GRAY) ChatUtils.sendNoSpamMessage(serverPlayerEntity, messageId, new TranslatableText("reborncore.message.energyError")
.append(" ") .formatted(Formatting.GRAY)
.append( .append(" ")
new TranslatableText("reborncore.message.deactivating") .append(
.formatted(Formatting.GOLD) new TranslatableText("reborncore.message.deactivating")
) .formatted(Formatting.GOLD)
)
); );
} }
stack.getOrCreateNbt().putBoolean("isActive", false); stack.getOrCreateNbt().putBoolean("isActive", false);
} }
@ -187,36 +190,34 @@ public class ItemUtils {
* *
* @param stack {@link ItemStack} Stack to switch state * @param stack {@link ItemStack} Stack to switch state
* @param cost {@code int} Cost of operation performed by tool * @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 * @param messageId {@code int} MessageID for sending no spam message
*/ */
public static void switchActive(ItemStack stack, int cost, boolean isClient, int messageId) { public static void switchActive(ItemStack stack, int cost, int messageId, Entity entity) {
ItemUtils.checkActive(stack, cost, isClient, messageId); ItemUtils.checkActive(stack, cost, messageId, entity);
if (!ItemUtils.isActive(stack)) { if (!ItemUtils.isActive(stack)) {
stack.getOrCreateNbt().putBoolean("isActive", true); stack.getOrCreateNbt().putBoolean("isActive", true);
if (isClient) {
if (entity instanceof ServerPlayerEntity serverPlayerEntity) {
ChatUtils.sendNoSpamMessages(messageId, new TranslatableText("reborncore.message.setTo") ChatUtils.sendNoSpamMessage(serverPlayerEntity, messageId, new TranslatableText("reborncore.message.setTo")
.formatted(Formatting.GRAY) .formatted(Formatting.GRAY)
.append(" ") .append(" ")
.append( .append(
new TranslatableText("reborncore.message.active") new TranslatableText("reborncore.message.active")
.formatted(Formatting.GOLD) .formatted(Formatting.GOLD)
) )
); );
} }
} else { } else {
stack.getOrCreateNbt().putBoolean("isActive", false); stack.getOrCreateNbt().putBoolean("isActive", false);
if (isClient) { if (entity instanceof ServerPlayerEntity serverPlayerEntity) {
ChatUtils.sendNoSpamMessages(messageId, new TranslatableText("reborncore.message.setTo") ChatUtils.sendNoSpamMessage(serverPlayerEntity, messageId, new TranslatableText("reborncore.message.setTo")
.formatted(Formatting.GRAY) .formatted(Formatting.GRAY)
.append(" ") .append(" ")
.append( .append(
new TranslatableText("reborncore.message.inactive") new TranslatableText("reborncore.message.inactive")
.formatted(Formatting.GOLD) .formatted(Formatting.GOLD)
) )
); );
} }
} }

View file

@ -27,9 +27,11 @@ package techreborn.blockentity.machine.misc;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityTicker; import net.minecraft.block.entity.BlockEntityTicker;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound; import net.minecraft.nbt.NbtCompound;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.sound.SoundCategory; import net.minecraft.sound.SoundCategory;
import net.minecraft.text.TranslatableText; import net.minecraft.text.TranslatableText;
import net.minecraft.util.Formatting; import net.minecraft.util.Formatting;
@ -51,8 +53,8 @@ public class AlarmBlockEntity extends BlockEntity
super(TRBlockEntities.ALARM, pos, state); super(TRBlockEntities.ALARM, pos, state);
} }
public void rightClick() { public void rightClick(Entity entity) {
if (world == null || world.isClient) return; if (world == null) return;
if (selectedSound < 3) { if (selectedSound < 3) {
selectedSound++; selectedSound++;
@ -60,10 +62,12 @@ public class AlarmBlockEntity extends BlockEntity
selectedSound = 1; 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) .formatted(Formatting.GRAY)
.append(" Alarm ") .append(" Alarm ")
.append(String.valueOf(selectedSound))); .append(String.valueOf(selectedSound)));
}
} }
// BlockEntity // BlockEntity

View file

@ -32,6 +32,7 @@ import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.LivingEntity; import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.state.StateManager; import net.minecraft.state.StateManager;
import net.minecraft.state.property.EnumProperty; import net.minecraft.state.property.EnumProperty;
import net.minecraft.text.LiteralText; import net.minecraft.text.LiteralText;
@ -123,15 +124,15 @@ public class PlayerDetectorBlock extends BlockMachineBase {
} }
} }
if (worldIn.isClient) { if (playerIn instanceof ServerPlayerEntity serverPlayerEntity) {
ChatUtils.sendNoSpamMessages(MessageIDs.playerDetectorID, ChatUtils.sendNoSpamMessage(serverPlayerEntity, MessageIDs.playerDetectorID,
new TranslatableText("techreborn.message.detects") new TranslatableText("techreborn.message.detects")
.formatted(Formatting.GRAY) .formatted(Formatting.GRAY)
.append(" ") .append(" ")
.append( .append(
new LiteralText(StringUtils.toFirstCapital(newType.asString())) new LiteralText(StringUtils.toFirstCapital(newType.asString()))
.formatted(color) .formatted(color)
) )
); );
} }

View file

@ -138,8 +138,8 @@ public class BlockAlarm extends BaseBlockEntityProvider {
} }
} }
if (!worldIn.isClient && playerIn.isSneaking()) { if (playerIn.isSneaking()) {
((AlarmBlockEntity) blockEntity).rightClick(); ((AlarmBlockEntity) blockEntity).rightClick(playerIn);
return ActionResult.SUCCESS; return ActionResult.SUCCESS;
} }

View file

@ -65,7 +65,7 @@ public class BatteryItem extends Item implements RcEnergyItem {
public TypedActionResult<ItemStack> use(final World world, final PlayerEntity player, final Hand hand) { public TypedActionResult<ItemStack> use(final World world, final PlayerEntity player, final Hand hand) {
final ItemStack stack = player.getStackInHand(hand); final ItemStack stack = player.getStackInHand(hand);
if (player.isSneaking()) { 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.SUCCESS, stack);
} }
return new TypedActionResult<>(ActionResult.PASS, stack); return new TypedActionResult<>(ActionResult.PASS, stack);
@ -73,7 +73,7 @@ public class BatteryItem extends Item implements RcEnergyItem {
@Override @Override
public void inventoryTick(ItemStack stack, World world, Entity entity, int slot, boolean selected) { 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) { if (world.isClient) {
return; return;
} }

View file

@ -32,6 +32,7 @@ import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUsageContext; import net.minecraft.item.ItemUsageContext;
import net.minecraft.nbt.NbtOps; import net.minecraft.nbt.NbtOps;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.LiteralText; import net.minecraft.text.LiteralText;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText; import net.minecraft.text.TranslatableText;
@ -66,20 +67,20 @@ public class FrequencyTransmitterItem extends Item {
GlobalPos.CODEC.encodeStart(NbtOps.INSTANCE, globalPos).result() GlobalPos.CODEC.encodeStart(NbtOps.INSTANCE, globalPos).result()
.ifPresent(tag -> stack.getOrCreateNbt().put("pos", tag)); .ifPresent(tag -> stack.getOrCreateNbt().put("pos", tag));
if (!world.isClient) { if (context.getPlayer() instanceof ServerPlayerEntity serverPlayerEntity) {
ChatUtils.sendNoSpamMessage(serverPlayerEntity, MessageIDs.freqTransmitterID, new TranslatableText("techreborn.message.setTo")
ChatUtils.sendNoSpamMessages(MessageIDs.freqTransmitterID, new TranslatableText("techreborn.message.setTo") .append(new LiteralText(" X:").formatted(Formatting.GRAY))
.append(new LiteralText(" X:").formatted(Formatting.GRAY)) .append(new LiteralText(String.valueOf(pos.getX())).formatted(Formatting.GOLD))
.append(new LiteralText(String.valueOf(pos.getX())).formatted(Formatting.GOLD)) .append(new LiteralText(" Y:").formatted(Formatting.GRAY))
.append(new LiteralText(" Y:").formatted(Formatting.GRAY)) .append(new LiteralText(String.valueOf(pos.getY())).formatted(Formatting.GOLD))
.append(new LiteralText(String.valueOf(pos.getY())).formatted(Formatting.GOLD)) .append(new LiteralText(" Z:").formatted(Formatting.GRAY))
.append(new LiteralText(" Z:").formatted(Formatting.GRAY)) .append(new LiteralText(String.valueOf(pos.getZ())).formatted(Formatting.GOLD))
.append(new LiteralText(String.valueOf(pos.getZ())).formatted(Formatting.GOLD)) .append(" ")
.append(" ") .append(new TranslatableText("techreborn.message.in").formatted(Formatting.GRAY))
.append(new TranslatableText("techreborn.message.in").formatted(Formatting.GRAY)) .append(" ")
.append(" ") .append(new LiteralText(getDimName(globalPos.getDimension()).toString()).formatted(Formatting.GOLD)));
.append(new LiteralText(getDimName(globalPos.getDimension()).toString()).formatted(Formatting.GOLD)));
} }
return ActionResult.SUCCESS; return ActionResult.SUCCESS;
} }
@ -96,16 +97,16 @@ public class FrequencyTransmitterItem extends Item {
ItemStack stack = player.getStackInHand(hand); ItemStack stack = player.getStackInHand(hand);
if (player.isSneaking()) { if (player.isSneaking()) {
stack.setNbt(null); stack.setNbt(null);
if (!world.isClient) {
ChatUtils.sendNoSpamMessages(MessageIDs.freqTransmitterID, if (player instanceof ServerPlayerEntity serverPlayerEntity) {
new TranslatableText("techreborn.message.coordsHaveBeen") ChatUtils.sendNoSpamMessage(serverPlayerEntity, MessageIDs.freqTransmitterID,
.formatted(Formatting.GRAY) new TranslatableText("techreborn.message.coordsHaveBeen")
.append(" ") .formatted(Formatting.GRAY)
.append( .append(" ")
new TranslatableText("techreborn.message.cleared") .append(
.formatted(Formatting.GOLD) new TranslatableText("techreborn.message.cleared")
) .formatted(Formatting.GOLD)
)
); );
} }
} }

View file

@ -90,7 +90,7 @@ public class AdvancedJackhammerItem extends JackhammerItem implements MultiBlock
public TypedActionResult<ItemStack> use(final World world, final PlayerEntity player, final Hand hand) { public TypedActionResult<ItemStack> use(final World world, final PlayerEntity player, final Hand hand) {
final ItemStack stack = player.getStackInHand(hand); final ItemStack stack = player.getStackInHand(hand);
if (player.isSneaking()) { 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.SUCCESS, stack);
} }
return new TypedActionResult<>(ActionResult.PASS, stack); return new TypedActionResult<>(ActionResult.PASS, stack);
@ -98,7 +98,7 @@ public class AdvancedJackhammerItem extends JackhammerItem implements MultiBlock
@Override @Override
public void inventoryTick(ItemStack stack, World world, Entity entity, int slot, boolean selected) { 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) @Environment(EnvType.CLIENT)

View file

@ -108,7 +108,7 @@ public class IndustrialChainsawItem extends ChainsawItem {
public TypedActionResult<ItemStack> use(final World world, final PlayerEntity player, final Hand hand) { public TypedActionResult<ItemStack> use(final World world, final PlayerEntity player, final Hand hand) {
final ItemStack stack = player.getStackInHand(hand); final ItemStack stack = player.getStackInHand(hand);
if (player.isSneaking()) { 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.SUCCESS, stack);
} }
return new TypedActionResult<>(ActionResult.PASS, stack); return new TypedActionResult<>(ActionResult.PASS, stack);
@ -116,7 +116,7 @@ public class IndustrialChainsawItem extends ChainsawItem {
@Override @Override
public void usageTick(World world, LivingEntity entity, ItemStack stack, int i) { 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) @Environment(EnvType.CLIENT)

View file

@ -103,7 +103,7 @@ public class IndustrialDrillItem extends DrillItem {
public TypedActionResult<ItemStack> use(final World world, final PlayerEntity player, final Hand hand) { public TypedActionResult<ItemStack> use(final World world, final PlayerEntity player, final Hand hand) {
final ItemStack stack = player.getStackInHand(hand); final ItemStack stack = player.getStackInHand(hand);
if (player.isSneaking()) { 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.SUCCESS, stack);
} }
return new TypedActionResult<>(ActionResult.PASS, stack); return new TypedActionResult<>(ActionResult.PASS, stack);
@ -111,7 +111,7 @@ public class IndustrialDrillItem extends DrillItem {
@Override @Override
public void usageTick(World world, LivingEntity entity, ItemStack stack, int i) { 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) @Environment(EnvType.CLIENT)

View file

@ -32,6 +32,7 @@ import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity; import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.LiteralText; import net.minecraft.text.LiteralText;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText; 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 // Cycle Inactive, Active 3*3 and Active 5*5
private void switchAOE(ItemStack stack, int cost, boolean isClient, int messageId) { private void switchAOE(ItemStack stack, int cost, int messageId, Entity entity) {
ItemUtils.checkActive(stack, cost, isClient, messageId); ItemUtils.checkActive(stack, cost, messageId, entity);
if (!ItemUtils.isActive(stack)) { if (!ItemUtils.isActive(stack)) {
ItemUtils.switchActive(stack, cost, isClient, messageId); ItemUtils.switchActive(stack, cost, messageId, entity);
stack.getOrCreateNbt().putBoolean("AOE5", false); stack.getOrCreateNbt().putBoolean("AOE5", false);
if (isClient) { if (entity instanceof ServerPlayerEntity serverPlayerEntity) {
ChatUtils.sendNoSpamMessages(messageId, new TranslatableText("techreborn.message.setTo").formatted(Formatting.GRAY).append(" ").append(new LiteralText("3*3").formatted(Formatting.GOLD))); ChatUtils.sendNoSpamMessage(serverPlayerEntity, messageId, new TranslatableText("techreborn.message.setTo").formatted(Formatting.GRAY).append(" ").append(new LiteralText("3*3").formatted(Formatting.GOLD)));
} }
} else { } else {
if (isAOE5(stack)) { if (isAOE5(stack)) {
ItemUtils.switchActive(stack, cost, isClient, messageId); ItemUtils.switchActive(stack, cost, messageId, entity);
stack.getOrCreateNbt().putBoolean("AOE5", false); stack.getOrCreateNbt().putBoolean("AOE5", false);
} else { } else {
stack.getOrCreateNbt().putBoolean("AOE5", true); stack.getOrCreateNbt().putBoolean("AOE5", true);
if (isClient) { if (entity instanceof ServerPlayerEntity serverPlayerEntity) {
ChatUtils.sendNoSpamMessages(messageId, new TranslatableText("techreborn.message.setTo").formatted(Formatting.GRAY).append(" ").append(new LiteralText("5*5").formatted(Formatting.GOLD))); 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<ItemStack> use(final World world, final PlayerEntity player, final Hand hand) { public TypedActionResult<ItemStack> use(final World world, final PlayerEntity player, final Hand hand) {
final ItemStack stack = player.getStackInHand(hand); final ItemStack stack = player.getStackInHand(hand);
if (player.isSneaking()) { 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.SUCCESS, stack);
} }
return new TypedActionResult<>(ActionResult.PASS, stack); return new TypedActionResult<>(ActionResult.PASS, stack);
@ -142,7 +143,7 @@ public class IndustrialJackhammerItem extends JackhammerItem implements MultiBlo
@Override @Override
public void inventoryTick(ItemStack stack, World world, Entity entity, int slot, boolean selected) { 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) @Environment(EnvType.CLIENT)

View file

@ -81,14 +81,14 @@ public class NanosaberItem extends SwordItem implements RcEnergyItem, ItemStackM
// Item // Item
@Override @Override
public void inventoryTick(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) { 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 @Override
public TypedActionResult<ItemStack> use(final World world, final PlayerEntity player, final Hand hand) { public TypedActionResult<ItemStack> use(final World world, final PlayerEntity player, final Hand hand) {
final ItemStack stack = player.getStackInHand(hand); final ItemStack stack = player.getStackInHand(hand);
if (player.isSneaking()) { 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.SUCCESS, stack);
} }
return new TypedActionResult<>(ActionResult.PASS, stack); return new TypedActionResult<>(ActionResult.PASS, stack);