the lens sees the bees

This commit is contained in:
yrsegal@gmail.com 2022-07-25 20:11:06 -04:00
parent 4ed6f299e7
commit 508dde11ef
5 changed files with 87 additions and 22 deletions

View file

@ -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<LocalPlayer, Pair<BlockPos, Integer>> comparatorData = new WeakHashMap<>();
private static final Map<LocalPlayer, Pair<BlockPos, Integer>> 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.
*

View file

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

View file

@ -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<ServerPlayer, Pair<BlockPos, Integer>> comparatorDataMap = new WeakHashMap<>();
private static final Map<ServerPlayer, Pair<BlockPos, Integer>> 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));
}
}

View file

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

View file

@ -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",