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 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..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,8 @@ 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; import net.minecraft.block.BlockState; import net.minecraft.block.entity.BlockEntity; @@ -12,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 { @@ -33,21 +31,23 @@ 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()) { 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..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 @@ -1,43 +1,80 @@ 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.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; +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; +import java.util.List; public class HolographicConnectorItem extends Item { - public static final Object2ObjectArrayMap CACHE = new Object2ObjectArrayMap<>(); - 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) { 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 +98,88 @@ 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); } } 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) { 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",