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.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);
});
});
}
}

View file

@ -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);
});
}
}

View file

@ -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.
* <p>
* Original class link:
* https://github.com/SleepyTrousers/EnderCore/blob/master/src/main/java/com/enderio/core/common/util/ChatUtil.java
* </p>
*/
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);
}
}

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.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,18 +161,18 @@ 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")
if (player instanceof ServerPlayerEntity serverPlayerEntity) {
ChatUtils.sendNoSpamMessage(serverPlayerEntity, messageId, new TranslatableText("reborncore.message.energyError")
.formatted(Formatting.GRAY)
.append(" ")
.append(
@ -179,6 +181,7 @@ public class ItemUtils {
)
);
}
stack.getOrCreateNbt().putBoolean("isActive", false);
}
@ -187,18 +190,16 @@ 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")
if (entity instanceof ServerPlayerEntity serverPlayerEntity) {
ChatUtils.sendNoSpamMessage(serverPlayerEntity, messageId, new TranslatableText("reborncore.message.setTo")
.formatted(Formatting.GRAY)
.append(" ")
.append(
@ -209,8 +210,8 @@ public class ItemUtils {
}
} else {
stack.getOrCreateNbt().putBoolean("isActive", false);
if (isClient) {
ChatUtils.sendNoSpamMessages(messageId, new TranslatableText("reborncore.message.setTo")
if (entity instanceof ServerPlayerEntity serverPlayerEntity) {
ChatUtils.sendNoSpamMessage(serverPlayerEntity, messageId, new TranslatableText("reborncore.message.setTo")
.formatted(Formatting.GRAY)
.append(" ")
.append(

View file

@ -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,11 +62,13 @@ 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
@Override

View file

@ -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,8 +124,8 @@ public class PlayerDetectorBlock extends BlockMachineBase {
}
}
if (worldIn.isClient) {
ChatUtils.sendNoSpamMessages(MessageIDs.playerDetectorID,
if (playerIn instanceof ServerPlayerEntity serverPlayerEntity) {
ChatUtils.sendNoSpamMessage(serverPlayerEntity, MessageIDs.playerDetectorID,
new TranslatableText("techreborn.message.detects")
.formatted(Formatting.GRAY)
.append(" ")

View file

@ -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;
}

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) {
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;
}

View file

@ -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,9 +67,8 @@ 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")
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))
@ -80,6 +80,7 @@ public class FrequencyTransmitterItem extends Item {
.append(" ")
.append(new LiteralText(getDimName(globalPos.getDimension()).toString()).formatted(Formatting.GOLD)));
}
return ActionResult.SUCCESS;
}
@ -96,9 +97,9 @@ public class FrequencyTransmitterItem extends Item {
ItemStack stack = player.getStackInHand(hand);
if (player.isSneaking()) {
stack.setNbt(null);
if (!world.isClient) {
ChatUtils.sendNoSpamMessages(MessageIDs.freqTransmitterID,
if (player instanceof ServerPlayerEntity serverPlayerEntity) {
ChatUtils.sendNoSpamMessage(serverPlayerEntity, MessageIDs.freqTransmitterID,
new TranslatableText("techreborn.message.coordsHaveBeen")
.formatted(Formatting.GRAY)
.append(" ")

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) {
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)

View file

@ -108,7 +108,7 @@ public class IndustrialChainsawItem extends ChainsawItem {
public TypedActionResult<ItemStack> 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)

View file

@ -103,7 +103,7 @@ public class IndustrialDrillItem extends DrillItem {
public TypedActionResult<ItemStack> 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)

View file

@ -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<ItemStack> 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)

View file

@ -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<ItemStack> 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);