From e90b1cc186bc625578733e8f44450d6e09e0abc9 Mon Sep 17 00:00:00 2001 From: shedaniel Date: Wed, 1 Jul 2020 15:12:20 +0800 Subject: [PATCH 1/4] We use nbt instead of a map --- .../HolographicBridgeProjectorBlock.java | 1 + .../common/item/HolographicConnectorItem.java | 135 +++++++++++++----- 2 files changed, 100 insertions(+), 36 deletions(-) diff --git a/src/main/java/com/github/chainmailstudios/astromine/common/block/HolographicBridgeProjectorBlock.java b/src/main/java/com/github/chainmailstudios/astromine/common/block/HolographicBridgeProjectorBlock.java index 4956d4a69..e44f9ba86 100644 --- a/src/main/java/com/github/chainmailstudios/astromine/common/block/HolographicBridgeProjectorBlock.java +++ b/src/main/java/com/github/chainmailstudios/astromine/common/block/HolographicBridgeProjectorBlock.java @@ -48,6 +48,7 @@ public ActionResult onUse(BlockState state, World world, BlockPos position, Play if (!player.isCreative()) { stack.decrement(1); } + return ActionResult.SUCCESS; } } diff --git a/src/main/java/com/github/chainmailstudios/astromine/common/item/HolographicConnectorItem.java b/src/main/java/com/github/chainmailstudios/astromine/common/item/HolographicConnectorItem.java index 1a18fb4a2..2b638da54 100644 --- a/src/main/java/com/github/chainmailstudios/astromine/common/item/HolographicConnectorItem.java +++ b/src/main/java/com/github/chainmailstudios/astromine/common/item/HolographicConnectorItem.java @@ -1,24 +1,27 @@ package com.github.chainmailstudios.astromine.common.item; +import com.github.chainmailstudios.astromine.common.block.HolographicBridgeProjectorBlock; +import com.github.chainmailstudios.astromine.common.block.entity.HolographicBridgeProjectorBlockEntity; +import com.github.chainmailstudios.astromine.registry.client.AstromineSounds; import net.minecraft.block.HorizontalFacingBlock; +import net.minecraft.block.entity.BlockEntity; import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; import net.minecraft.item.ItemUsageContext; +import net.minecraft.nbt.CompoundTag; import net.minecraft.sound.SoundCategory; import net.minecraft.text.TranslatableText; import net.minecraft.util.ActionResult; import net.minecraft.util.Formatting; +import net.minecraft.util.Identifier; +import net.minecraft.util.Pair; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Direction; +import net.minecraft.util.registry.Registry; +import net.minecraft.util.registry.RegistryKey; import net.minecraft.world.World; -import com.github.chainmailstudios.astromine.common.block.HolographicBridgeProjectorBlock; -import com.github.chainmailstudios.astromine.common.block.entity.HolographicBridgeProjectorBlockEntity; -import com.github.chainmailstudios.astromine.registry.client.AstromineSounds; -import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap; - public class HolographicConnectorItem extends Item { - public static final Object2ObjectArrayMap CACHE = new Object2ObjectArrayMap<>(); - public HolographicConnectorItem(Settings settings) { super(settings); } @@ -28,16 +31,32 @@ public ActionResult useOnBlock(ItemUsageContext context) { World world = context.getWorld(); BlockPos position = context.getBlockPos(); + + if (context.shouldCancelInteraction()) return super.useOnBlock(context); if (world.getBlockState(position).getBlock() instanceof HolographicBridgeProjectorBlock) { HolographicBridgeProjectorBlockEntity entity = (HolographicBridgeProjectorBlockEntity) world.getBlockEntity(position); - if (CACHE.getOrDefault(world, null) == null) { - CACHE.put(world, entity); - context.getPlayer().sendMessage(new TranslatableText("text.astromine.message.holographic_connector_select", toShortString(entity.getPos())).formatted(Formatting.BLUE), true); - world.playSound(context.getPlayer(), context.getBlockPos(), AstromineSounds.HOLOGRAPHIC_CONNECTOR_CLICK, SoundCategory.PLAYERS, 0.5f, 0.33f); + Pair, BlockPos> pair = readBlock(context.getStack()); + if (pair == null || !pair.getLeft().getValue().equals(world.getRegistryKey().getValue())) { + if (!world.isClient) { + context.getPlayer().setStackInHand(context.getHand(), selectBlock(context.getStack(), entity.getWorld().getRegistryKey(), entity.getPos())); + } else { + context.getPlayer().sendMessage(new TranslatableText("text.astromine.message.holographic_connector_select", toShortString(entity.getPos())).formatted(Formatting.BLUE), true); + world.playSound(context.getPlayer(), context.getBlockPos(), AstromineSounds.HOLOGRAPHIC_CONNECTOR_CLICK, SoundCategory.PLAYERS, 0.5f, 0.33f); + } } else { - HolographicBridgeProjectorBlockEntity parent = (HolographicBridgeProjectorBlockEntity) CACHE.get(world); + BlockEntity blockEntity = world.getBlockEntity(pair.getRight()); + if (!(blockEntity instanceof HolographicBridgeProjectorBlockEntity)) { + if (!world.isClient) { + context.getPlayer().setStackInHand(context.getHand(), selectBlock(context.getStack(), entity.getWorld().getRegistryKey(), entity.getPos())); + } else { + context.getPlayer().sendMessage(new TranslatableText("text.astromine.message.holographic_connector_select", toShortString(entity.getPos())).formatted(Formatting.BLUE), true); + world.playSound(context.getPlayer(), context.getBlockPos(), AstromineSounds.HOLOGRAPHIC_CONNECTOR_CLICK, SoundCategory.PLAYERS, 0.5f, 0.33f); + } + return ActionResult.SUCCESS; + } + HolographicBridgeProjectorBlockEntity parent = (HolographicBridgeProjectorBlockEntity) blockEntity; BlockPos nP = entity.getPos(); BlockPos oP = parent.getPos(); @@ -61,45 +80,89 @@ public ActionResult useOnBlock(ItemUsageContext context) { } if ((parent.getPos().getX() != entity.getPos().getX() && parent.getPos().getZ() != entity.getPos().getZ()) || oP.getSquaredDistance(nP) > 65536) { - CACHE.put(world, null); - context.getPlayer().sendMessage(new TranslatableText("text.astromine.message.holographic_connection_failed", toShortString(parent.getPos()), toShortString(entity.getPos())).formatted(Formatting.RED), true); - world.playSound(context.getPlayer(), context.getBlockPos(), AstromineSounds.HOLOGRAPHIC_CONNECTOR_CLICK, SoundCategory.PLAYERS, 0.5f, 0.33f); - return ActionResult.FAIL; + if (!world.isClient) { + context.getPlayer().setStackInHand(context.getHand(), unselect(context.getStack())); + } else { + context.getPlayer().sendMessage(new TranslatableText("text.astromine.message.holographic_connection_failed", toShortString(parent.getPos()), toShortString(entity.getPos())).formatted(Formatting.RED), true); + world.playSound(context.getPlayer(), context.getBlockPos(), AstromineSounds.HOLOGRAPHIC_CONNECTOR_CLICK, SoundCategory.PLAYERS, 0.5f, 0.33f); + } + return ActionResult.SUCCESS; } else if (parent.getCachedState().get(HorizontalFacingBlock.FACING).getOpposite() != entity.getCachedState().get(HorizontalFacingBlock.FACING)) { - CACHE.put(world, null); - context.getPlayer().sendMessage(new TranslatableText("text.astromine.message.holographic_connection_failed", toShortString(parent.getPos()), toShortString(entity.getPos())).formatted(Formatting.RED), true); - world.playSound(context.getPlayer(), context.getBlockPos(), AstromineSounds.HOLOGRAPHIC_CONNECTOR_CLICK, SoundCategory.PLAYERS, 0.5f, 0.33f); - return ActionResult.FAIL; + if (!world.isClient) { + context.getPlayer().setStackInHand(context.getHand(), unselect(context.getStack())); + } else { + context.getPlayer().sendMessage(new TranslatableText("text.astromine.message.holographic_connection_failed", toShortString(parent.getPos()), toShortString(entity.getPos())).formatted(Formatting.RED), true); + world.playSound(context.getPlayer(), context.getBlockPos(), AstromineSounds.HOLOGRAPHIC_CONNECTOR_CLICK, SoundCategory.PLAYERS, 0.5f, 0.33f); + } + return ActionResult.SUCCESS; } - parent.setChild(entity); - entity.setParent(parent); - - if (parent.getParent() == entity.getParent()) { - parent.setParent(null); - } - - parent.direction = d; - - parent.buildBridge(); - if (world.isClient) { context.getPlayer().sendMessage(new TranslatableText("text.astromine.message.holographic_connection_successful", toShortString(parent.getPos()), toShortString(entity.getPos())).formatted(Formatting.GREEN), true); world.playSound(context.getPlayer(), context.getBlockPos(), AstromineSounds.HOLOGRAPHIC_CONNECTOR_CLICK, SoundCategory.PLAYERS, 0.5f, 0.33f); + } else { + parent.setChild(entity); + entity.setParent(parent); + + if (parent.getParent() == entity.getParent()) { + parent.setParent(null); + } + + parent.direction = d; + parent.buildBridge(); + parent.sync(); + context.getPlayer().setStackInHand(context.getHand(), unselect(context.getStack())); } - - CACHE.put(world, null); } + return ActionResult.SUCCESS; } else { if (world.isClient) { context.getPlayer().sendMessage(new TranslatableText("text.astromine.message.holographic_connection_clear").formatted(Formatting.YELLOW), true); world.playSound(context.getPlayer(), context.getBlockPos(), AstromineSounds.HOLOGRAPHIC_CONNECTOR_CLICK, SoundCategory.PLAYERS, 0.5f, 0.33f); + } else { + context.getPlayer().setStackInHand(context.getHand(), unselect(context.getStack())); } - - CACHE.put(world, null); + return ActionResult.SUCCESS; } + } + + private ItemStack unselect(ItemStack stack) { + stack = stack.copy(); + CompoundTag tag = stack.getOrCreateTag(); + tag.remove("SelectedConnectorBlock"); + return stack; + } + + private ItemStack selectBlock(ItemStack stack, RegistryKey registryKey, BlockPos pos) { + stack = stack.copy(); + CompoundTag tag = stack.getOrCreateTag(); + tag.remove("SelectedConnectorBlock"); + tag.put("SelectedConnectorBlock", writePos(registryKey, pos)); + return stack; + } + + private Pair, BlockPos> readBlock(ItemStack stack) { + CompoundTag tag = stack.getTag(); + if (tag == null) return null; + if (!tag.contains("SelectedConnectorBlock")) return null; + return readPos(tag.getCompound("SelectedConnectorBlock")); + } + + private CompoundTag writePos(RegistryKey registryKey, BlockPos pos) { + CompoundTag tag = new CompoundTag(); + tag.putString("World", registryKey.getValue().toString()); + tag.putInt("X", pos.getX()); + tag.putInt("Y", pos.getY()); + tag.putInt("Z", pos.getZ()); + return tag; + } - return super.useOnBlock(context); + private Pair, BlockPos> readPos(CompoundTag tag) { + RegistryKey registryKey = RegistryKey.of(Registry.DIMENSION, Identifier.tryParse(tag.getString("World"))); + int x = tag.getInt("X"); + int y = tag.getInt("Y"); + int z = tag.getInt("Z"); + return new Pair<>(registryKey, new BlockPos(x, y, z)); } public String toShortString(BlockPos pos) { From f868dceb700e76feadd3d57c37ae8fc1972a585c Mon Sep 17 00:00:00 2001 From: shedaniel Date: Wed, 1 Jul 2020 15:21:49 +0800 Subject: [PATCH 2/4] Well let's hope --- .../HolographicBridgeProjectorBlock.java | 2 ++ .../common/item/HolographicConnectorItem.java | 25 ++++++++++++++++--- .../assets/astromine/lang/en_us.json | 1 + 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/github/chainmailstudios/astromine/common/block/HolographicBridgeProjectorBlock.java b/src/main/java/com/github/chainmailstudios/astromine/common/block/HolographicBridgeProjectorBlock.java index e44f9ba86..8fef48e63 100644 --- a/src/main/java/com/github/chainmailstudios/astromine/common/block/HolographicBridgeProjectorBlock.java +++ b/src/main/java/com/github/chainmailstudios/astromine/common/block/HolographicBridgeProjectorBlock.java @@ -1,5 +1,7 @@ package com.github.chainmailstudios.astromine.common.block; +import com.github.chainmailstudios.astromine.common.block.base.DefaultedHorizontalFacingBlockWithEntity; +import com.github.chainmailstudios.astromine.common.block.entity.HolographicBridgeProjectorBlockEntity; import net.minecraft.block.AbstractBlock; import net.minecraft.block.BlockState; import net.minecraft.block.entity.BlockEntity; diff --git a/src/main/java/com/github/chainmailstudios/astromine/common/item/HolographicConnectorItem.java b/src/main/java/com/github/chainmailstudios/astromine/common/item/HolographicConnectorItem.java index 2b638da54..2359cc904 100644 --- a/src/main/java/com/github/chainmailstudios/astromine/common/item/HolographicConnectorItem.java +++ b/src/main/java/com/github/chainmailstudios/astromine/common/item/HolographicConnectorItem.java @@ -3,13 +3,17 @@ import com.github.chainmailstudios.astromine.common.block.HolographicBridgeProjectorBlock; import com.github.chainmailstudios.astromine.common.block.entity.HolographicBridgeProjectorBlockEntity; import com.github.chainmailstudios.astromine.registry.client.AstromineSounds; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; import net.minecraft.block.HorizontalFacingBlock; import net.minecraft.block.entity.BlockEntity; +import net.minecraft.client.item.TooltipContext; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.item.ItemUsageContext; import net.minecraft.nbt.CompoundTag; import net.minecraft.sound.SoundCategory; +import net.minecraft.text.Text; import net.minecraft.text.TranslatableText; import net.minecraft.util.ActionResult; import net.minecraft.util.Formatting; @@ -21,17 +25,31 @@ import net.minecraft.util.registry.RegistryKey; import net.minecraft.world.World; +import java.util.List; + public class HolographicConnectorItem extends Item { public HolographicConnectorItem(Settings settings) { super(settings); } + @Environment(EnvType.CLIENT) + @Override + public void appendTooltip(ItemStack stack, World world, List tooltip, TooltipContext context) { + super.appendTooltip(stack, world, tooltip, context); + + Pair, BlockPos> pair = readBlock(stack); + if (pair != null) { + tooltip.add(Text.method_30163(null)); + tooltip.add(new TranslatableText("text.astromine.selected.dimension.pos", pair.getLeft().getValue(), pair.getRight().getX(), pair.getRight().getY(), pair.getRight().getZ()).formatted(Formatting.GRAY)); + } + } + @Override public ActionResult useOnBlock(ItemUsageContext context) { World world = context.getWorld(); BlockPos position = context.getBlockPos(); - + if (context.shouldCancelInteraction()) return super.useOnBlock(context); if (world.getBlockState(position).getBlock() instanceof HolographicBridgeProjectorBlock) { @@ -107,14 +125,13 @@ public ActionResult useOnBlock(ItemUsageContext context) { if (parent.getParent() == entity.getParent()) { parent.setParent(null); } - + parent.direction = d; parent.buildBridge(); parent.sync(); context.getPlayer().setStackInHand(context.getHand(), unselect(context.getStack())); } } - return ActionResult.SUCCESS; } else { if (world.isClient) { context.getPlayer().sendMessage(new TranslatableText("text.astromine.message.holographic_connection_clear").formatted(Formatting.YELLOW), true); @@ -122,8 +139,8 @@ public ActionResult useOnBlock(ItemUsageContext context) { } else { context.getPlayer().setStackInHand(context.getHand(), unselect(context.getStack())); } - return ActionResult.SUCCESS; } + return ActionResult.SUCCESS; } private ItemStack unselect(ItemStack stack) { diff --git a/src/main/resources/assets/astromine/lang/en_us.json b/src/main/resources/assets/astromine/lang/en_us.json index 6f2217ca4..3d508f875 100644 --- a/src/main/resources/assets/astromine/lang/en_us.json +++ b/src/main/resources/assets/astromine/lang/en_us.json @@ -210,6 +210,7 @@ "category.astromine.consuming.energy": "%s consumed", "category.astromine.fluid.generating.consumed": "%s (%s) consumed/tick", "category.astromine.fluid.generating.generated": "%s (%s) generated/tick", + "text.astromine.selected.dimension.pos": "Selected %s (%d, %d, %d)", "text.astromine.patchouli.astronautics.title": "Astronautics", "text.astromine.patchouli.astronautics.description": "From where one dreams, to where their dreams come true.", "text.astromine.patchouli.components.title": "Components", From d190e1b55bd24ab333142aeccaeeae25f00bb4e1 Mon Sep 17 00:00:00 2001 From: shedaniel Date: Wed, 1 Jul 2020 15:24:50 +0800 Subject: [PATCH 3/4] just make stuff only run on servers --- .../HolographicBridgeProjectorBlock.java | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/github/chainmailstudios/astromine/common/block/HolographicBridgeProjectorBlock.java b/src/main/java/com/github/chainmailstudios/astromine/common/block/HolographicBridgeProjectorBlock.java index 8fef48e63..1690f0ebc 100644 --- a/src/main/java/com/github/chainmailstudios/astromine/common/block/HolographicBridgeProjectorBlock.java +++ b/src/main/java/com/github/chainmailstudios/astromine/common/block/HolographicBridgeProjectorBlock.java @@ -1,5 +1,6 @@ package com.github.chainmailstudios.astromine.common.block; +import com.github.chainmailstudios.astromine.access.DyeColorAccess; import com.github.chainmailstudios.astromine.common.block.base.DefaultedHorizontalFacingBlockWithEntity; import com.github.chainmailstudios.astromine.common.block.entity.HolographicBridgeProjectorBlockEntity; import net.minecraft.block.AbstractBlock; @@ -14,11 +15,6 @@ import net.minecraft.util.math.BlockPos; import net.minecraft.world.BlockView; import net.minecraft.world.World; - -import com.github.chainmailstudios.astromine.access.DyeColorAccess; -import com.github.chainmailstudios.astromine.common.block.base.DefaultedHorizontalFacingBlockWithEntity; -import com.github.chainmailstudios.astromine.common.block.entity.HolographicBridgeProjectorBlockEntity; -import com.github.chainmailstudios.astromine.mixin.DyeColorMixin; import spinnery.widget.api.Color; public class HolographicBridgeProjectorBlock extends DefaultedHorizontalFacingBlockWithEntity { @@ -35,16 +31,17 @@ public ActionResult onUse(BlockState state, World world, BlockPos position, Play HolographicBridgeProjectorBlockEntity entity = (HolographicBridgeProjectorBlockEntity) world.getBlockEntity(position); - if (entity != null) { - entity.color = Color.of(0x7e000000 >> 2 | ((DyeColorAccess)(Object)dye.getColor()).getColor()); - if(!world.isClient()) entity.sync(); - if(entity.hasChild()) { - entity.getChild().color = Color.of(0x7e000000 >> 2 | ((DyeColorAccess)(Object)dye.getColor()).getColor()); - if(!world.isClient()) entity.getChild().sync(); + if (!world.isClient() && entity != null) { + entity.color = Color.of(0x7e000000 | ((DyeColorAccess) (Object) dye.getColor()).getColor()); + entity.sync(); + + if (entity.hasChild()) { + entity.getChild().color = Color.of(0x7e000000 | ((DyeColorAccess) (Object) dye.getColor()).getColor()); + entity.getChild().sync(); } - if(entity.hasParent()) { - entity.getParent().color = Color.of(0x7e000000 >> 2 | ((DyeColorAccess)(Object)dye.getColor()).getColor()); - if(!world.isClient()) entity.getParent().sync(); + if (entity.hasParent()) { + entity.getParent().color = Color.of(0x7e000000 | ((DyeColorAccess) (Object) dye.getColor()).getColor()); + entity.getParent().sync(); } if (!player.isCreative()) { From 27f862450c3567836dd102ed7afd96d7c09f3031 Mon Sep 17 00:00:00 2001 From: shedaniel Date: Wed, 1 Jul 2020 15:31:33 +0800 Subject: [PATCH 4/4] Bump versions --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index 17a8d34fb..edac28322 100644 --- a/gradle.properties +++ b/gradle.properties @@ -11,7 +11,7 @@ api_version = 0.14.1+build.372-1.16 # Mod mod_name = astromine -mod_version = 1.0.21 +mod_version = 1.0.22 mod_group = com.github.chainmailstudios version_meta = fabric-1.16.1