the lens sees the bees
This commit is contained in:
parent
4ed6f299e7
commit
508dde11ef
5 changed files with 87 additions and 22 deletions
|
@ -11,6 +11,7 @@ import net.minecraft.core.Direction;
|
||||||
import net.minecraft.network.chat.Component;
|
import net.minecraft.network.chat.Component;
|
||||||
import net.minecraft.resources.ResourceLocation;
|
import net.minecraft.resources.ResourceLocation;
|
||||||
import net.minecraft.world.item.ItemStack;
|
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.Block;
|
||||||
import net.minecraft.world.level.block.Blocks;
|
import net.minecraft.world.level.block.Blocks;
|
||||||
import net.minecraft.world.level.block.state.BlockState;
|
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
|
// 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>> 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;
|
LocalPlayer player = Minecraft.getInstance().player;
|
||||||
if (player != null) {
|
if (player != null) {
|
||||||
if (pos == null || value == -1) {
|
if (pos == null || comparator == -1) {
|
||||||
comparatorData.remove(player);
|
comparatorData.remove(player);
|
||||||
} else {
|
} 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();
|
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.
|
* Add the block to display things when the player is holding a lens and looking at it.
|
||||||
*
|
*
|
||||||
|
|
|
@ -190,6 +190,17 @@ public class RegisterClientStuff {
|
||||||
new TextComponent(String.valueOf(state.getValue(RepeaterBlock.DELAY)))
|
new TextComponent(String.valueOf(state.getValue(RepeaterBlock.DELAY)))
|
||||||
.withStyle(ChatFormatting.YELLOW))));
|
.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(
|
ScryingLensOverlayRegistry.addPredicateDisplayer(
|
||||||
(state, pos, observer, world, direction) -> state.isSignalSource() && !state.is(
|
(state, pos, observer, world, direction) -> state.isSignalSource() && !state.is(
|
||||||
Blocks.COMPARATOR),
|
Blocks.COMPARATOR),
|
||||||
|
|
|
@ -19,8 +19,10 @@ import net.minecraft.world.item.ArmorItem;
|
||||||
import net.minecraft.world.item.Item;
|
import net.minecraft.world.item.Item;
|
||||||
import net.minecraft.world.item.ItemStack;
|
import net.minecraft.world.item.ItemStack;
|
||||||
import net.minecraft.world.item.Wearable;
|
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.Blocks;
|
||||||
import net.minecraft.world.level.block.DispenserBlock;
|
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.level.block.state.properties.BlockStateProperties;
|
||||||
import net.minecraft.world.phys.BlockHitResult;
|
import net.minecraft.world.phys.BlockHitResult;
|
||||||
import net.minecraft.world.phys.HitResult;
|
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>> comparatorDataMap = new WeakHashMap<>();
|
||||||
|
private static final Map<ServerPlayer, Pair<BlockPos, Integer>> beeDataMap = new WeakHashMap<>();
|
||||||
|
|
||||||
private static void sendComparatorDataToClient(ServerPlayer player) {
|
private static void sendComparatorDataToClient(ServerPlayer player) {
|
||||||
double reachAttribute = IXplatAbstractions.INSTANCE.getReachDistance(player);
|
double reachAttribute = IXplatAbstractions.INSTANCE.getReachDistance(player);
|
||||||
|
@ -94,29 +97,40 @@ public class ItemLens extends Item implements Wearable {
|
||||||
if (hitResult.getType() == HitResult.Type.BLOCK) {
|
if (hitResult.getType() == HitResult.Type.BLOCK) {
|
||||||
var pos = ((BlockHitResult) hitResult).getBlockPos();
|
var pos = ((BlockHitResult) hitResult).getBlockPos();
|
||||||
var state = player.level.getBlockState(pos);
|
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)) {
|
if (state.is(Blocks.COMPARATOR)) {
|
||||||
syncComparatorValue(player, pos,
|
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()) {
|
} else if (state.hasAnalogOutputSignal()) {
|
||||||
syncComparatorValue(player, pos, state.getAnalogOutputSignal(player.level, pos));
|
syncComparatorValue(player, pos, state.getAnalogOutputSignal(player.level, pos), bee);
|
||||||
} else {
|
} else {
|
||||||
syncComparatorValue(player, null, -1);
|
syncComparatorValue(player, null, -1, bee);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
syncComparatorValue(player, null, -1);
|
syncComparatorValue(player, null, -1, -1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void syncComparatorValue(ServerPlayer player, BlockPos pos, int value) {
|
private static void syncComparatorValue(ServerPlayer player, BlockPos pos, int comparator, int bee) {
|
||||||
var previous = comparatorDataMap.get(player);
|
var previousComparator = comparatorDataMap.get(player);
|
||||||
if (value == -1) {
|
var previousBee = beeDataMap.get(player);
|
||||||
if (previous != null) {
|
if (comparator == -1 && bee == -1) {
|
||||||
|
if (previousComparator != null || previousBee != null) {
|
||||||
comparatorDataMap.remove(player);
|
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())) {
|
} else if (previousComparator == null || !pos.equals(previousComparator.getFirst()) || comparator != previousComparator.getSecond() ||
|
||||||
comparatorDataMap.put(player, new Pair<>(pos, value));
|
previousBee == null || !pos.equals(previousBee.getFirst()) || bee != previousBee.getSecond()) {
|
||||||
IXplatAbstractions.INSTANCE.sendPacketToPlayer(player, new MsgUpdateComparatorVisualsAck(pos, value));
|
comparatorDataMap.put(player, new Pair<>(pos, comparator));
|
||||||
|
beeDataMap.put(player, new Pair<>(pos, bee));
|
||||||
|
IXplatAbstractions.INSTANCE.sendPacketToPlayer(player, new MsgUpdateComparatorVisualsAck(pos, comparator, bee));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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.
|
* 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");
|
public static final ResourceLocation ID = modLoc("cmp");
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -23,15 +23,17 @@ public record MsgUpdateComparatorVisualsAck(BlockPos pos, int value) implements
|
||||||
public static MsgUpdateComparatorVisualsAck deserialize(ByteBuf buffer) {
|
public static MsgUpdateComparatorVisualsAck deserialize(ByteBuf buffer) {
|
||||||
var buf = new FriendlyByteBuf(buffer);
|
var buf = new FriendlyByteBuf(buffer);
|
||||||
|
|
||||||
int value = buf.readInt();
|
int comparator = buf.readInt();
|
||||||
BlockPos pos = value == -1 ? null : buf.readBlockPos();
|
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) {
|
public void serialize(FriendlyByteBuf buf) {
|
||||||
buf.writeInt(this.value);
|
buf.writeInt(this.comparator);
|
||||||
if (this.value != -1) {
|
buf.writeInt(this.bee);
|
||||||
|
if (this.comparator != -1 || this.bee != -1) {
|
||||||
buf.writeBlockPos(this.pos);
|
buf.writeBlockPos(this.pos);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -40,7 +42,7 @@ public record MsgUpdateComparatorVisualsAck(BlockPos pos, int value) implements
|
||||||
Minecraft.getInstance().execute(new Runnable() {
|
Minecraft.getInstance().execute(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
ScryingLensOverlayRegistry.receiveComparatorValue(msg.pos(), msg.value());
|
ScryingLensOverlayRegistry.receiveComparatorAndBeeValue(msg.pos(), msg.comparator(), msg.bee());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -130,6 +130,8 @@
|
||||||
"hexcasting.tooltip.lens.akashic.bookshelf.location": "Record at %s",
|
"hexcasting.tooltip.lens.akashic.bookshelf.location": "Record at %s",
|
||||||
"hexcasting.tooltip.lens.akashic.record.count": "%s iotas stored",
|
"hexcasting.tooltip.lens.akashic.record.count": "%s iotas stored",
|
||||||
"hexcasting.tooltip.lens.akashic.record.count.single": "%s iota 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.min_level": "Level %s or higher",
|
||||||
"hexcasting.tooltip.brainsweep.level": "Level %s",
|
"hexcasting.tooltip.brainsweep.level": "Level %s",
|
||||||
"hexcasting.tooltip.brainsweep.product": "Mindless Body",
|
"hexcasting.tooltip.brainsweep.product": "Mindless Body",
|
||||||
|
|
Loading…
Reference in a new issue