diff options
author | Linnea Gräf <nea@nea.moe> | 2024-11-26 15:55:40 +0100 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-11-26 15:55:40 +0100 |
commit | 894994bf9990f13896501333d54c299f5a6091ff (patch) | |
tree | 397da40982f41d6c081ea18d7b15dc2092e0f308 /src/main/java | |
parent | c2442a97e93491432d40d0a9ee579584781f76b3 (diff) | |
download | funny-teleporters-894994bf9990f13896501333d54c299f5a6091ff.tar.gz funny-teleporters-894994bf9990f13896501333d54c299f5a6091ff.tar.bz2 funny-teleporters-894994bf9990f13896501333d54c299f5a6091ff.zip |
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/moe/nea/funnyteleporters/TeleporterDestination.java | 10 | ||||
-rw-r--r-- | src/main/java/moe/nea/funnyteleporters/TeleporterWand.java | 34 |
2 files changed, 40 insertions, 4 deletions
diff --git a/src/main/java/moe/nea/funnyteleporters/TeleporterDestination.java b/src/main/java/moe/nea/funnyteleporters/TeleporterDestination.java index 482bc49..44e6d46 100644 --- a/src/main/java/moe/nea/funnyteleporters/TeleporterDestination.java +++ b/src/main/java/moe/nea/funnyteleporters/TeleporterDestination.java @@ -8,15 +8,16 @@ import net.minecraft.registry.Registries; import net.minecraft.registry.RegistryKey; import net.minecraft.registry.RegistryKeys; import net.minecraft.registry.entry.RegistryEntry; -import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.world.ServerWorld; +import net.minecraft.text.MutableText; +import net.minecraft.text.Text; import net.minecraft.util.Identifier; -import net.minecraft.util.dynamic.Codecs; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import org.jetbrains.annotations.NotNull; import java.util.Comparator; +import java.util.List; import java.util.Optional; import java.util.Set; @@ -33,6 +34,11 @@ public record TeleporterDestination( return COMPARATOR.compare(this, o); } + public List<Text> formatName(String prefix) { // TODO: use this more often + return List.of(Text.literal("%s x: %d, y: %d, z: %d".formatted(prefix, blockPos().getX(), blockPos().getY(), blockPos().getZ())), + Text.literal("In Dimension: %s".formatted(target().getValue()))); + } + public void teleport(Entity subject) { var sourceWorld = subject.getWorld(); var destinationWorld = getDestinationWorld((ServerWorld) sourceWorld); diff --git a/src/main/java/moe/nea/funnyteleporters/TeleporterWand.java b/src/main/java/moe/nea/funnyteleporters/TeleporterWand.java index fa379b4..6510123 100644 --- a/src/main/java/moe/nea/funnyteleporters/TeleporterWand.java +++ b/src/main/java/moe/nea/funnyteleporters/TeleporterWand.java @@ -1,15 +1,25 @@ package moe.nea.funnyteleporters; import eu.pb4.polymer.core.api.item.PolymerItem; +import net.minecraft.component.DataComponentTypes; +import net.minecraft.component.type.LoreComponent; +import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.item.ItemUsageContext; import net.minecraft.item.Items; +import net.minecraft.item.tooltip.TooltipType; +import net.minecraft.registry.RegistryWrapper; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; import net.minecraft.util.ActionResult; +import net.minecraft.util.Hand; +import net.minecraft.util.TypedActionResult; +import net.minecraft.world.World; import org.jetbrains.annotations.Nullable; +import java.util.List; + public class TeleporterWand extends Item implements PolymerItem { public TeleporterWand(Settings settings) { super(settings); @@ -21,6 +31,27 @@ public class TeleporterWand extends Item implements PolymerItem { } @Override + public ItemStack getPolymerItemStack(ItemStack original, TooltipType tooltipType, RegistryWrapper.WrapperLookup lookup, @Nullable ServerPlayerEntity player) { + var stack = PolymerItem.super.getPolymerItemStack(original, tooltipType, lookup, player); + var dest = original.get(FunnyRegistry.TELEPORTER_DESTINATION); + if (dest != null) { + stack.set(DataComponentTypes.LORE, new LoreComponent(dest.formatName("Linked to"))); + } + return stack; + } + + @Override + public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) { + var stack = user.getStackInHand(hand); + if (user.isSneaking()) { + if (stack.remove(FunnyRegistry.TELEPORTER_DESTINATION) != null) { + user.sendMessage(Text.literal("Cleared saved destination.")); + } + } + return TypedActionResult.success(stack, world.isClient()); + } + + @Override public ActionResult useOnBlock(ItemUsageContext context) { var destination = context.getStack().get(FunnyRegistry.TELEPORTER_DESTINATION); var block = context.getWorld().getBlockState(context.getBlockPos()).getBlock(); @@ -34,9 +65,8 @@ public class TeleporterWand extends Item implements PolymerItem { if (context.getPlayer() != null && context.getPlayer().isSneaking()) { context.getStack().set(FunnyRegistry.TELEPORTER_DESTINATION, new TeleporterDestination(context.getWorld().getRegistryKey(), context.getBlockPos())); context.getPlayer().sendMessage(Text.literal("Saved teleport destination to wand.")); - } else { + } else if (destination != null){ var be = FunnyRegistry.TELEPORTER.getBE(context.getWorld(), context.getBlockPos()); - // TODO: check for empty destination be.destination = destination; be.markDirty(); if (context.getPlayer() != null) { |