From 508dde11ef15bf5cb163c6127e8bc945d80c90fa Mon Sep 17 00:00:00 2001 From: "yrsegal@gmail.com" Date: Mon, 25 Jul 2022 20:11:06 -0400 Subject: [PATCH] the lens sees the bees --- .../client/ScryingLensOverlayRegistry.java | 42 +++++++++++++++++-- .../client/RegisterClientStuff.java | 11 +++++ .../hexcasting/common/items/ItemLens.java | 38 +++++++++++------ .../MsgUpdateComparatorVisualsAck.java | 16 +++---- .../assets/hexcasting/lang/en_us.json | 2 + 5 files changed, 87 insertions(+), 22 deletions(-) diff --git a/Common/src/main/java/at/petrak/hexcasting/api/client/ScryingLensOverlayRegistry.java b/Common/src/main/java/at/petrak/hexcasting/api/client/ScryingLensOverlayRegistry.java index 6c111641..6ff8fd7c 100644 --- a/Common/src/main/java/at/petrak/hexcasting/api/client/ScryingLensOverlayRegistry.java +++ b/Common/src/main/java/at/petrak/hexcasting/api/client/ScryingLensOverlayRegistry.java @@ -11,6 +11,7 @@ import net.minecraft.core.Direction; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.block.BeehiveBlock; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.state.BlockState; @@ -37,14 +38,21 @@ public final class ScryingLensOverlayRegistry { // implemented as a map to allow for weak dereferencing private static final Map> comparatorData = new WeakHashMap<>(); + private static final Map> beeData = new WeakHashMap<>(); - public static void receiveComparatorValue(BlockPos pos, int value) { + public static void receiveComparatorAndBeeValue(BlockPos pos, int comparator, int bee) { LocalPlayer player = Minecraft.getInstance().player; if (player != null) { - if (pos == null || value == -1) { + if (pos == null || comparator == -1) { comparatorData.remove(player); } else { - comparatorData.put(player, new Pair<>(pos, value)); + comparatorData.put(player, new Pair<>(pos, comparator)); + } + + if (pos == null || bee == -1) { + beeData.remove(player); + } else { + beeData.put(player, new Pair<>(pos, bee)); } } } @@ -78,6 +86,34 @@ public final class ScryingLensOverlayRegistry { return comparatorValue.getSecond(); } + public static int getBeeValue() { + var mc = Minecraft.getInstance(); + var player = mc.player; + var level = mc.level; + var result = mc.hitResult; + + if (player == null || level == null || result == null || result.getType() != HitResult.Type.BLOCK) { + return -1; + } + + var beeValue = beeData.get(player); + if (beeValue == null) { + return -1; + } + + var pos = ((BlockHitResult) result).getBlockPos(); + if (!pos.equals(beeValue.getFirst())) { + return -1; + } + + var state = mc.level.getBlockState(pos); + if (!(state.getBlock() instanceof BeehiveBlock)) { + return -1; + } + + return beeValue.getSecond(); + } + /** * Add the block to display things when the player is holding a lens and looking at it. * diff --git a/Common/src/main/java/at/petrak/hexcasting/client/RegisterClientStuff.java b/Common/src/main/java/at/petrak/hexcasting/client/RegisterClientStuff.java index 77f0ce01..855600f0 100644 --- a/Common/src/main/java/at/petrak/hexcasting/client/RegisterClientStuff.java +++ b/Common/src/main/java/at/petrak/hexcasting/client/RegisterClientStuff.java @@ -190,6 +190,17 @@ public class RegisterClientStuff { new TextComponent(String.valueOf(state.getValue(RepeaterBlock.DELAY))) .withStyle(ChatFormatting.YELLOW)))); + ScryingLensOverlayRegistry.addPredicateDisplayer( + (state, pos, observer, world, direction) -> state.getBlock() instanceof BeehiveBlock, + (lines, state, pos, observer, world, direction) -> { + int count = ScryingLensOverlayRegistry.getBeeValue(); + lines.add(new Pair<>(new ItemStack(Items.BEE_NEST), count == -1 ? new TextComponent("") : + new TranslatableComponent( + "hexcasting.tooltip.lens.bee" + (count == 1 ? ".single" : ""), + count + ))); + }); + ScryingLensOverlayRegistry.addPredicateDisplayer( (state, pos, observer, world, direction) -> state.isSignalSource() && !state.is( Blocks.COMPARATOR), diff --git a/Common/src/main/java/at/petrak/hexcasting/common/items/ItemLens.java b/Common/src/main/java/at/petrak/hexcasting/common/items/ItemLens.java index 3869e489..9bc36f03 100644 --- a/Common/src/main/java/at/petrak/hexcasting/common/items/ItemLens.java +++ b/Common/src/main/java/at/petrak/hexcasting/common/items/ItemLens.java @@ -19,8 +19,10 @@ import net.minecraft.world.item.ArmorItem; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Wearable; +import net.minecraft.world.level.block.BeehiveBlock; import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.DispenserBlock; +import net.minecraft.world.level.block.entity.BeehiveBlockEntity; import net.minecraft.world.level.block.state.properties.BlockStateProperties; import net.minecraft.world.phys.BlockHitResult; import net.minecraft.world.phys.HitResult; @@ -86,6 +88,7 @@ public class ItemLens extends Item implements Wearable { } private static final Map> comparatorDataMap = new WeakHashMap<>(); + private static final Map> beeDataMap = new WeakHashMap<>(); private static void sendComparatorDataToClient(ServerPlayer player) { double reachAttribute = IXplatAbstractions.INSTANCE.getReachDistance(player); @@ -94,29 +97,40 @@ public class ItemLens extends Item implements Wearable { if (hitResult.getType() == HitResult.Type.BLOCK) { var pos = ((BlockHitResult) hitResult).getBlockPos(); var state = player.level.getBlockState(pos); + + int bee = -1; + + if (state.getBlock() instanceof BeehiveBlock && player.level.getBlockEntity(pos) instanceof BeehiveBlockEntity bees) { + bee = bees.getOccupantCount(); + } + if (state.is(Blocks.COMPARATOR)) { syncComparatorValue(player, pos, - state.getDirectSignal(player.level, pos, state.getValue(BlockStateProperties.HORIZONTAL_FACING))); + state.getDirectSignal(player.level, pos, state.getValue(BlockStateProperties.HORIZONTAL_FACING)), bee); } else if (state.hasAnalogOutputSignal()) { - syncComparatorValue(player, pos, state.getAnalogOutputSignal(player.level, pos)); + syncComparatorValue(player, pos, state.getAnalogOutputSignal(player.level, pos), bee); } else { - syncComparatorValue(player, null, -1); + syncComparatorValue(player, null, -1, bee); } } else { - syncComparatorValue(player, null, -1); + syncComparatorValue(player, null, -1, -1); } } - private static void syncComparatorValue(ServerPlayer player, BlockPos pos, int value) { - var previous = comparatorDataMap.get(player); - if (value == -1) { - if (previous != null) { + private static void syncComparatorValue(ServerPlayer player, BlockPos pos, int comparator, int bee) { + var previousComparator = comparatorDataMap.get(player); + var previousBee = beeDataMap.get(player); + if (comparator == -1 && bee == -1) { + if (previousComparator != null || previousBee != null) { comparatorDataMap.remove(player); - IXplatAbstractions.INSTANCE.sendPacketToPlayer(player, new MsgUpdateComparatorVisualsAck(null, -1)); + beeDataMap.remove(player); + IXplatAbstractions.INSTANCE.sendPacketToPlayer(player, new MsgUpdateComparatorVisualsAck(null, -1, -1)); } - } else if (previous == null || (!pos.equals(previous.getFirst()) || value != previous.getSecond())) { - comparatorDataMap.put(player, new Pair<>(pos, value)); - IXplatAbstractions.INSTANCE.sendPacketToPlayer(player, new MsgUpdateComparatorVisualsAck(pos, value)); + } else if (previousComparator == null || !pos.equals(previousComparator.getFirst()) || comparator != previousComparator.getSecond() || + previousBee == null || !pos.equals(previousBee.getFirst()) || bee != previousBee.getSecond()) { + comparatorDataMap.put(player, new Pair<>(pos, comparator)); + beeDataMap.put(player, new Pair<>(pos, bee)); + IXplatAbstractions.INSTANCE.sendPacketToPlayer(player, new MsgUpdateComparatorVisualsAck(pos, comparator, bee)); } } diff --git a/Common/src/main/java/at/petrak/hexcasting/common/network/MsgUpdateComparatorVisualsAck.java b/Common/src/main/java/at/petrak/hexcasting/common/network/MsgUpdateComparatorVisualsAck.java index 2d534b50..31835cc0 100644 --- a/Common/src/main/java/at/petrak/hexcasting/common/network/MsgUpdateComparatorVisualsAck.java +++ b/Common/src/main/java/at/petrak/hexcasting/common/network/MsgUpdateComparatorVisualsAck.java @@ -12,7 +12,7 @@ import static at.petrak.hexcasting.api.HexAPI.modLoc; /** * Sent server->client when a player is looking at a block through a lens whose comparator value is not the same as what they last saw. */ -public record MsgUpdateComparatorVisualsAck(BlockPos pos, int value) implements IMessage { +public record MsgUpdateComparatorVisualsAck(BlockPos pos, int comparator, int bee) implements IMessage { public static final ResourceLocation ID = modLoc("cmp"); @Override @@ -23,15 +23,17 @@ public record MsgUpdateComparatorVisualsAck(BlockPos pos, int value) implements public static MsgUpdateComparatorVisualsAck deserialize(ByteBuf buffer) { var buf = new FriendlyByteBuf(buffer); - int value = buf.readInt(); - BlockPos pos = value == -1 ? null : buf.readBlockPos(); + int comparator = buf.readInt(); + int bee = buf.readInt(); + BlockPos pos = comparator == -1 && bee == -1 ? null : buf.readBlockPos(); - return new MsgUpdateComparatorVisualsAck(pos, value); + return new MsgUpdateComparatorVisualsAck(pos, comparator, bee); } public void serialize(FriendlyByteBuf buf) { - buf.writeInt(this.value); - if (this.value != -1) { + buf.writeInt(this.comparator); + buf.writeInt(this.bee); + if (this.comparator != -1 || this.bee != -1) { buf.writeBlockPos(this.pos); } } @@ -40,7 +42,7 @@ public record MsgUpdateComparatorVisualsAck(BlockPos pos, int value) implements Minecraft.getInstance().execute(new Runnable() { @Override public void run() { - ScryingLensOverlayRegistry.receiveComparatorValue(msg.pos(), msg.value()); + ScryingLensOverlayRegistry.receiveComparatorAndBeeValue(msg.pos(), msg.comparator(), msg.bee()); } }); } diff --git a/Common/src/main/resources/assets/hexcasting/lang/en_us.json b/Common/src/main/resources/assets/hexcasting/lang/en_us.json index 6e0e1b0d..50cbab60 100644 --- a/Common/src/main/resources/assets/hexcasting/lang/en_us.json +++ b/Common/src/main/resources/assets/hexcasting/lang/en_us.json @@ -130,6 +130,8 @@ "hexcasting.tooltip.lens.akashic.bookshelf.location": "Record at %s", "hexcasting.tooltip.lens.akashic.record.count": "%s iotas stored", "hexcasting.tooltip.lens.akashic.record.count.single": "%s iota stored", + "hexcasting.tooltip.lens.bee": "%s bees", + "hexcasting.tooltip.lens.bee.single": "%s bee", "hexcasting.tooltip.brainsweep.min_level": "Level %s or higher", "hexcasting.tooltip.brainsweep.level": "Level %s", "hexcasting.tooltip.brainsweep.product": "Mindless Body",