Skip to content

Commit

Permalink
feat: improve wire behaviour
Browse files Browse the repository at this point in the history
- allow removing specific wires by using the wire item again on both endpoints
- add a tooltip specifying if the wire is currently linked to an endpoint
- allow to unlink the wire item
- make wire show its connection point in-world
  • Loading branch information
klikli-dev committed Aug 19, 2024
1 parent 4c0ad0c commit 031cfe9
Show file tree
Hide file tree
Showing 12 changed files with 207 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// 1.21.1 2024-08-18T11:12:57.321193 Languages: en_us
d4bf9645b2f50d984c242c57413b237136206f89 assets/theurgy/lang/en_us.json
// 1.21.1 2024-08-19T12:28:56.1332157 Languages: en_us
e2fce6be6f36a4edf044502cca8f6cc9c723ed2b assets/theurgy/lang/en_us.json
5 changes: 3 additions & 2 deletions src/generated/resources/assets/theurgy/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@
"book.theurgy.the_hermetica.logistics.mercurial_wand.usage.title": "Usage",
"book.theurgy.the_hermetica.logistics.mercurial_wire.description": "Item-Over-Wire Transport",
"book.theurgy.the_hermetica.logistics.mercurial_wire.name": "Mercurial Wire",
"book.theurgy.the_hermetica.logistics.mercurial_wire.removal.text": "To remove a wire, break one of the blocks the wire is connected to.\n",
"book.theurgy.the_hermetica.logistics.mercurial_wire.removal.text": "To remove a wire, break one of the blocks the wire is connected to.\n\\\n\\\nAlternatively, click both blocks connected by the wire with the wire item once more to remove just the one wire.\n",
"book.theurgy.the_hermetica.logistics.mercurial_wire.removal.title": "Removing Wires",
"book.theurgy.the_hermetica.logistics.mercurial_wire.usage.text": "Right-click one block, then right-click another block to connect them with the wire.\n\\\n\\\nOnly mercurial logistics blocks, such as inserters, extractors or connection nodes, can be connected with wires.\n",
"book.theurgy.the_hermetica.logistics.mercurial_wire.usage.title": "Usage",
Expand Down Expand Up @@ -2395,8 +2395,9 @@
"item.theurgy.attribute_filter.tooltip.usage": "§aRight-Click§r§7 the air to open the filter GUI and add items.\n§aRight-Click§r§7 a logistics inserter or extractor to apply the filter.\n§aRight-Click§r§7 a filtered block with an empty hand to remove the filter.\n",
"item.theurgy.copper_wire": "Mercurial Copper Wire",
"item.theurgy.copper_wire.tooltip": "A piece of copper wire capable of transferring matter in its mercurial form.",
"item.theurgy.copper_wire.tooltip.dynamic": "Linked to %1$s (Highlighted in yellow in the world).\n\n§aRight-Click§r§7 another connector block to create wire connection.\n§aShift-Right-Click§r§7 the air or a non-connector block to unlink.\n§aRight-Click§r§7 another connector block already wired to %1$s to remove the wire.\n",
"item.theurgy.copper_wire.tooltip.extended": "Can be used to connect different parts of Mercurial Logistics Networks.",
"item.theurgy.copper_wire.tooltip.usage": "Right-click one connector, then right-click another connector to connect them with the wire.\n",
"item.theurgy.copper_wire.tooltip.usage": "Right-click one connector, then right-click another connector to connect them with the wire.\nRepeat the process to disconnect them.\n",
"item.theurgy.crystallized_lava": "Crystallized Lava",
"item.theurgy.crystallized_lava.tooltip": "Lava in solid form for easy transportation, and alchemical processing.",
"item.theurgy.crystallized_water": "Crystallized Water",
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/com/klikli_dev/theurgy/Theurgy.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.klikli_dev.theurgy.content.item.derivative.AlchemicalDerivativeItem;
import com.klikli_dev.theurgy.content.item.sulfur.AlchemicalSulfurItem;
import com.klikli_dev.theurgy.content.item.derivative.render.AlchemicalDerivativeBEWLR;
import com.klikli_dev.theurgy.content.item.wire.WireItem;
import com.klikli_dev.theurgy.content.render.*;
import com.klikli_dev.theurgy.content.render.itemhud.ItemHUD;
import com.klikli_dev.theurgy.content.render.outliner.Outliner;
Expand All @@ -54,6 +55,7 @@
import net.minecraft.core.RegistryAccess;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.phys.Vec3;
import net.neoforged.api.distmarker.Dist;
Expand Down Expand Up @@ -203,9 +205,14 @@ public static void onClientTick(ClientTickEvent.Post event) {
if (Minecraft.getInstance().level == null || Minecraft.getInstance().player == null)
return;

Player player = Minecraft.getInstance().player;

Outliner.get().tick();
BlockRegistry.CALORIC_FLUX_EMITTER.get().selectionBehaviour().tick(Minecraft.getInstance().player);
BlockRegistry.SULFURIC_FLUX_EMITTER.get().selectionBehaviour().tick(Minecraft.getInstance().player);
BlockRegistry.CALORIC_FLUX_EMITTER.get().selectionBehaviour().tick(player);
BlockRegistry.SULFURIC_FLUX_EMITTER.get().selectionBehaviour().tick(player);


WireItem.onClientTick(player);
}

public static void onRenderLevelStage(RenderLevelStageEvent event) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/klikli_dev/theurgy/TheurgyConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public static class Tooltip {
public static final String SUFFIX = ".tooltip";
public static final String EXTENDED_SUFFIX = ".tooltip.extended";
public static final String USAGE_SUFFIX = ".tooltip.usage";
public static final String DYNMIC_SUFFIX = ".tooltip.dynamic";
private static final String PREFIX = "tooltip." + Theurgy.MODID + ".";
public static final String DIVINATION_ROD_LINKED_TO = PREFIX + ".divination_rod.linked_to";
public static final String DIVINATION_ROD_NO_LINK = PREFIX + ".divination_rod.no_link";
Expand Down
100 changes: 97 additions & 3 deletions src/main/java/com/klikli_dev/theurgy/content/item/wire/WireItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,114 @@

package com.klikli_dev.theurgy.content.item.wire;

import com.klikli_dev.theurgy.TheurgyConstants;
import com.klikli_dev.theurgy.content.behaviour.logistics.HasLeafNodeBehaviour;
import com.klikli_dev.theurgy.content.behaviour.logistics.HasWireEndPoint;
import com.klikli_dev.theurgy.content.render.Color;
import com.klikli_dev.theurgy.content.render.outliner.Outliner;
import com.klikli_dev.theurgy.logistics.Logistics;
import com.klikli_dev.theurgy.logistics.Wire;
import com.klikli_dev.theurgy.logistics.WireEndPoint;
import com.klikli_dev.theurgy.logistics.Wires;
import net.minecraft.ChatFormatting;
import net.minecraft.core.GlobalPos;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.shapes.Shapes;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Objects;

public class WireItem extends Item {

private final int maxRange;

public WireItem(Properties pProperties, int maxRange) {
super(pProperties);
this.maxRange = maxRange;
}

public static void onClientTick(Player player) {
var stack = player.getMainHandItem();
if (!(stack.getItem() instanceof WireItem))
return;

var wirePoint = WireEndPoint.load(stack);
if (wirePoint == null || wirePoint.level() != player.level().dimension())
return;

Outliner.get().showAABB(wirePoint,
Shapes.block().bounds().move(wirePoint.pos()), 1).colored(Color.YELLOW).lineWidth(1 / 32f);
}

@Override
public InteractionResult useOn(UseOnContext pContext) {
public @NotNull InteractionResult useOn(UseOnContext pContext) {
var stack = pContext.getItemInHand();

//TODO: wires do not exist on client wire cache (only render cache)
// so when we check for getWire we get null -> and thus we may return the wrong interactionresult and never run the serverside logkc

if (!this.isWireEndPoint(pContext)) {
if (Objects.requireNonNull(pContext.getPlayer()).isShiftKeyDown()) {
WireEndPoint.removeFrom(stack);
return InteractionResult.SUCCESS;
}
return InteractionResult.FAIL;
}

var wirePoint = WireEndPoint.load(stack);

if (wirePoint != null) {
return this.connectWire(pContext, wirePoint);
var existingWire = this.getWire(pContext, wirePoint);
if (existingWire != null) {
return this.disconnectWire(pContext, existingWire);
} else {
return this.connectWire(pContext, wirePoint);
}
} else {
return this.storeWireEndPoint(pContext);
}
}

@Override
public @NotNull InteractionResultHolder<ItemStack> use(@NotNull Level level, @NotNull Player player, @NotNull InteractionHand usedHand) {
if (usedHand == InteractionHand.MAIN_HAND && player.isShiftKeyDown()) {
WireEndPoint.removeFrom(player.getMainHandItem());
return InteractionResultHolder.sidedSuccess(player.getMainHandItem(), level.isClientSide());
}

return super.use(level, player, usedHand);
}

@Override
public void appendHoverText(@NotNull ItemStack stack, @NotNull TooltipContext context, @NotNull List<Component> tooltipComponents, @NotNull TooltipFlag tooltipFlag) {
super.appendHoverText(stack, context, tooltipComponents, tooltipFlag);

var wirePoint = WireEndPoint.load(stack);
if (wirePoint != null) {
tooltipComponents.add(Component.translatable(
stack.getDescriptionId() + TheurgyConstants.I18n.Tooltip.DYNMIC_SUFFIX,
Component.literal("[" + wirePoint.pos().toShortString() + "]").withStyle(ChatFormatting.GREEN)
).withStyle(ChatFormatting.GRAY));
}
}

protected Wire getWire(UseOnContext pContext, WireEndPoint wireEndPoint) {
if (wireEndPoint.level() != pContext.getLevel().dimension())
return null; //theoretically we could have a wire in place in the same spot in another dimension

return Wires.get(pContext.getLevel()).getWire(wireEndPoint.pos(), pContext.getClickedPos());
}

protected boolean isWireEndPoint(UseOnContext pContext) {
var level = pContext.getLevel();
var pos = pContext.getClickedPos();
Expand All @@ -60,7 +133,7 @@ protected InteractionResult connectWire(UseOnContext pContext, WireEndPoint wire
var stack = pContext.getItemInHand();
WireEndPoint.removeFrom(stack);

if (!pContext.getPlayer().getAbilities().instabuild) {
if (!Objects.requireNonNull(pContext.getPlayer()).getAbilities().instabuild) {
stack.shrink(1);
}

Expand All @@ -85,6 +158,27 @@ protected InteractionResult connectWire(UseOnContext pContext, WireEndPoint wire
return InteractionResult.SUCCESS;
}

protected InteractionResult disconnectWire(UseOnContext pContext, Wire wire) {
var stack = pContext.getItemInHand();

WireEndPoint.removeFrom(stack); //removing the wire should also remove the stored wire endpoint, otherwise our next click creates a new wire from the start of this old wire.

if (!Objects.requireNonNull(pContext.getPlayer()).getAbilities().instabuild) {
stack.grow(1); //return the wire item
}

var level = pContext.getLevel();

Wires.get(level).removeWire(wire);
if (!level.isClientSide) {
var posA = GlobalPos.of(level.dimension(), wire.from());
var posB = GlobalPos.of(level.dimension(), wire.to());
Logistics.get().remove(posA, posB);
}

return InteractionResult.SUCCESS;
}

protected InteractionResult storeWireEndPoint(UseOnContext pContext) {
var stack = pContext.getItemInHand();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ protected void generatePages() {
this.pageTitle("Removing Wires");
this.pageText("""
To remove a wire, break one of the blocks the wire is connected to.
\\
\\
Alternatively, click both blocks connected by the wire with the wire item once more to remove just the one wire.
"""
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1088,8 +1088,21 @@ private void addItems() {
"Can be used to connect different parts of Mercurial Logistics Networks.",
"""
Right-click one connector, then right-click another connector to connect them with the wire.
Repeat the process to disconnect them.
"""
);
this.addDynamicTooltip(ItemRegistry.COPPER_WIRE,
this.f("""
Linked to %1$s (Highlighted in yellow in the world).
{0} another connector block to create wire connection.
{1} the air or a non-connector block to unlink.
{0} another connector block already wired to %1$s to remove the wire.
""",
this.green("Right-Click"),
this.green("Shift-Right-Click")
)
);

this.addItem(ItemRegistry.MERCURIAL_WAND, "Mercurial Wand");
var wandUsage = this.f("""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,13 @@ default void addExtendedTooltip(Supplier<? extends Item> key, String extendedToo
default void addUsageTooltip(Supplier<? extends Item> key, String usageTooltip) {
this.addTooltip(key, null, null, usageTooltip);
}

default void addDynamicTooltip(Supplier<? extends Item> key, String tooltip) {
this.addDynamicTooltip(key, "", tooltip);
}

default void addDynamicTooltip(Supplier<? extends Item> key, String keySuffix, String tooltip) {
if (tooltip != null)
this.add(key.get().getDescriptionId() + TheurgyConstants.I18n.Tooltip.DYNMIC_SUFFIX + keySuffix, tooltip);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public void rebuildCaches() {
this.onUnloadExtractNode(node.asExtractor());
}
if (node.mode() == LeafNodeMode.INSERT) {
//no need to call unload here as it just notifies the extractors, which we reset anyay
//no need to call unload here as it just notifies the extractors, which we reset anyway
//this.onUnloadInsertNode(node.asInserter());
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/klikli_dev/theurgy/logistics/Wire.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@

package com.klikli_dev.theurgy.logistics;

import com.klikli_dev.modonomicon.networking.ClickCommandLinkMessage;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.resources.ResourceLocation;

public record Wire(BlockPos from, BlockPos to) {
public static final Codec<Wire> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Expand All @@ -27,6 +25,11 @@ public record Wire(BlockPos from, BlockPos to) {
Wire::new
);

public Wire(BlockPos from, BlockPos to) {
this.from = from.immutable();
this.to = to.immutable();
}

public static Wire load(CompoundTag tag) {
return new Wire(BlockPos.of(tag.getLong("from")), BlockPos.of(tag.getLong("to")));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.Nullable;

public record WireEndPoint(BlockPos pos, ResourceKey<Level> level) {

Expand All @@ -39,7 +40,7 @@ public static WireEndPoint load(CompoundTag tag) {
return new WireEndPoint(BlockPos.of(tag.getLong("pos")), ResourceKey.create(Registries.DIMENSION, ResourceLocation.parse(tag.getString("level"))));
}

public static WireEndPoint load(ItemStack stack) {
public static @Nullable WireEndPoint load(ItemStack stack) {
return stack.get(DataComponentRegistry.WIRE_END_POINT.get());
}

Expand Down
Loading

0 comments on commit 031cfe9

Please sign in to comment.