diff options
Diffstat (limited to 'src/main/java/moe/nea/funnyteleporters')
16 files changed, 528 insertions, 33 deletions
diff --git a/src/main/java/moe/nea/funnyteleporters/ColouredChestViewScreen.java b/src/main/java/moe/nea/funnyteleporters/ColouredChestViewScreen.java index 21d8c8a..4e1a769 100644 --- a/src/main/java/moe/nea/funnyteleporters/ColouredChestViewScreen.java +++ b/src/main/java/moe/nea/funnyteleporters/ColouredChestViewScreen.java @@ -3,7 +3,6 @@ package moe.nea.funnyteleporters; import eu.pb4.sgui.api.ClickType; import eu.pb4.sgui.api.elements.GuiElementInterface; import eu.pb4.sgui.api.gui.SimpleGui; -import net.minecraft.component.DataComponentTypes; import net.minecraft.item.ItemStack; import net.minecraft.item.Items; import net.minecraft.screen.ScreenHandlerType; @@ -12,7 +11,6 @@ import net.minecraft.screen.slot.SlotActionType; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; import net.minecraft.util.DyeColor; -import net.minecraft.util.Unit; import java.util.stream.Collectors; @@ -36,10 +34,7 @@ public class ColouredChestViewScreen extends SimpleGui { void setSlots() { for (int i = 1; i < 9; i++) { - var s = new ItemStack(Items.BLACK_STAINED_GLASS_PANE); - s.set(DataComponentTypes.CUSTOM_NAME, Text.empty()); - s.set(DataComponentTypes.HIDE_TOOLTIP, Unit.INSTANCE); - setSlot(i, s); + setSlot(i, Utils.createBlankBlack()); } setSlot(0, new ItemStack(Items.ARROW)); for (int i = 0; i < 27; i++) { diff --git a/src/main/java/moe/nea/funnyteleporters/FunnyDropTableProvider.java b/src/main/java/moe/nea/funnyteleporters/FunnyDropTableProvider.java index cb27621..b93424d 100644 --- a/src/main/java/moe/nea/funnyteleporters/FunnyDropTableProvider.java +++ b/src/main/java/moe/nea/funnyteleporters/FunnyDropTableProvider.java @@ -16,5 +16,6 @@ public class FunnyDropTableProvider extends FabricBlockLootTableProvider { addDrop(FunnyRegistry.COLOURED_CHEST); addDrop(FunnyRegistry.ENDER_PEARL_BLOCK); addDrop(FunnyRegistry.TELEPORTER); + addDrop(FunnyRegistry.TELEPORTER_NEXUS); } } diff --git a/src/main/java/moe/nea/funnyteleporters/FunnyRecipeProvider.java b/src/main/java/moe/nea/funnyteleporters/FunnyRecipeProvider.java index a3023a7..d59eea0 100644 --- a/src/main/java/moe/nea/funnyteleporters/FunnyRecipeProvider.java +++ b/src/main/java/moe/nea/funnyteleporters/FunnyRecipeProvider.java @@ -51,5 +51,15 @@ public class FunnyRecipeProvider extends FabricRecipeProvider { .input('s', Items.STICK) .criterion(hasItem(Items.ENDER_PEARL), conditionsFromItem(Items.ENDER_PEARL)) .offerTo(recipeExporter); + + ShapedRecipeJsonBuilder.create(RecipeCategory.TRANSPORTATION, FunnyRegistry.TELEPORTER_NEXUS) + .pattern(" e ") + .pattern("ptp") + .pattern("ttt") + .input('e', Items.ENCHANTING_TABLE) + .input('p', FunnyRegistry.ENDER_PEARL_BLOCK) + .input('t', FunnyRegistry.TELEPORTER) + .criterion(hasItem(FunnyRegistry.TELEPORTER), conditionsFromItem(FunnyRegistry.TELEPORTER)) + .offerTo(recipeExporter); } } diff --git a/src/main/java/moe/nea/funnyteleporters/FunnyRegistry.java b/src/main/java/moe/nea/funnyteleporters/FunnyRegistry.java index c42f4e4..2da8233 100644 --- a/src/main/java/moe/nea/funnyteleporters/FunnyRegistry.java +++ b/src/main/java/moe/nea/funnyteleporters/FunnyRegistry.java @@ -4,6 +4,7 @@ import com.mojang.serialization.Codec; import eu.pb4.polymer.core.api.block.PolymerBlockUtils; import eu.pb4.polymer.core.api.block.SimplePolymerBlock; import eu.pb4.polymer.core.api.item.PolymerBlockItem; +import eu.pb4.polymer.rsm.api.RegistrySyncUtils; import net.minecraft.block.AbstractBlock; import net.minecraft.block.Block; import net.minecraft.block.Blocks; @@ -25,8 +26,10 @@ public class FunnyRegistry { .strength(10F), Blocks.GREEN_WOOL), Items.GREEN_WOOL); public static TeleporterBlock TELEPORTER = registerBlock("teleporter", new TeleporterBlock(AbstractBlock.Settings.create() - .pistonBehavior(PistonBehavior.BLOCK) - .strength(50F, 1200F)), Items.SEA_LANTERN); + .pistonBehavior(PistonBehavior.BLOCK) + .strength(50F, 1200F)), Items.SEA_LANTERN); + + public static TeleporterNexus TELEPORTER_NEXUS = registerBlock("teleporter_nexus", new TeleporterNexus(AbstractBlock.Settings.create().pistonBehavior(PistonBehavior.BLOCK).strength(100F, 1200F)), Items.ENCHANTING_TABLE); public static ComponentType<TeleporterDestination> TELEPORTER_DESTINATION = registerComponentType("teleporter_destination", TeleporterDestination.CODEC); @@ -34,9 +37,12 @@ public class FunnyRegistry { public static BlockEntityType<ColouredChestBlockEntity> COLOURED_CHEST_ENTITY = registerBlockEntity("coloured_chest", BlockEntityType.Builder.create(ColouredChestBlockEntity::new, COLOURED_CHEST)); public static BlockEntityType<TeleporterBlockEntity> TELEPORTER_ENTITY = registerBlockEntity("teleporter", BlockEntityType.Builder.create(TeleporterBlockEntity::new, TELEPORTER)); public static TeleporterWand TELEPORTER_WAND = registerItem("teleporter_wand", new TeleporterWand(new Item.Settings().maxCount(1))); + public static BlockEntityType<TeleporterNexusBlockEntity> TELEPORTER_NEXUS_ENTITY = registerBlockEntity("teleporter_nexus", BlockEntityType.Builder.create(TeleporterNexusBlockEntity::new, TELEPORTER_NEXUS)); private static <T> ComponentType<T> registerComponentType(String name, Codec<T> codec) { - return Registry.register(Registries.DATA_COMPONENT_TYPE, FunnyTeleporters.id(name), ComponentType.<T>builder().codec(codec).build()); + var comp = Registry.register(Registries.DATA_COMPONENT_TYPE, FunnyTeleporters.id(name), ComponentType.<T>builder().codec(codec).build()); + RegistrySyncUtils.setServerEntry(Registries.DATA_COMPONENT_TYPE, comp); + return comp; } private static <T extends BlockEntity> BlockEntityType<T> registerBlockEntity(String name, BlockEntityType.Builder<T> builder) { diff --git a/src/main/java/moe/nea/funnyteleporters/FunnyTagGenerator.java b/src/main/java/moe/nea/funnyteleporters/FunnyTagGenerator.java index a43babd..a51e311 100644 --- a/src/main/java/moe/nea/funnyteleporters/FunnyTagGenerator.java +++ b/src/main/java/moe/nea/funnyteleporters/FunnyTagGenerator.java @@ -17,7 +17,8 @@ public class FunnyTagGenerator extends FabricTagProvider.BlockTagProvider { protected void configure(RegistryWrapper.WrapperLookup wrapperLookup) { getTagBuilder(BlockTags.PICKAXE_MINEABLE) .add(Registries.BLOCK.getId(FunnyRegistry.COLOURED_CHEST)) - .add(Registries.BLOCK.getId(FunnyRegistry.TELEPORTER)); + .add(Registries.BLOCK.getId(FunnyRegistry.TELEPORTER)) + .add(Registries.BLOCK.getId(FunnyRegistry.TELEPORTER_NEXUS)); getTagBuilder(BlockTags.HOE_MINEABLE) .add(Registries.BLOCK.getId(FunnyRegistry.ENDER_PEARL_BLOCK)); } diff --git a/src/main/java/moe/nea/funnyteleporters/FunnyTranslationProvider.java b/src/main/java/moe/nea/funnyteleporters/FunnyTranslationProvider.java index e24a819..4cdc995 100644 --- a/src/main/java/moe/nea/funnyteleporters/FunnyTranslationProvider.java +++ b/src/main/java/moe/nea/funnyteleporters/FunnyTranslationProvider.java @@ -15,6 +15,7 @@ public class FunnyTranslationProvider extends FabricLanguageProvider { public void generateTranslations(RegistryWrapper.WrapperLookup wrapperLookup, TranslationBuilder translationBuilder) { translationBuilder.add(FunnyRegistry.ENDER_PEARL_BLOCK, "Block of Ender Pearl"); translationBuilder.add(FunnyRegistry.TELEPORTER, "Teleporter"); + translationBuilder.add(FunnyRegistry.TELEPORTER_NEXUS, "Teleporter Nexus"); translationBuilder.add(FunnyRegistry.COLOURED_CHEST, "Colored Chest"); translationBuilder.add(FunnyRegistry.TELEPORTER_WAND, "Teleporter Wand"); } diff --git a/src/main/java/moe/nea/funnyteleporters/TeleporterBlock.java b/src/main/java/moe/nea/funnyteleporters/TeleporterBlock.java index a12037e..45d88e8 100644 --- a/src/main/java/moe/nea/funnyteleporters/TeleporterBlock.java +++ b/src/main/java/moe/nea/funnyteleporters/TeleporterBlock.java @@ -6,6 +6,7 @@ import net.minecraft.block.Block; import net.minecraft.block.BlockEntityProvider; import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; +import net.minecraft.block.RespawnAnchorBlock; import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.BlockEntityTicker; import net.minecraft.block.entity.BlockEntityType; @@ -22,7 +23,7 @@ public class TeleporterBlock extends Block implements PolymerBlock, BlockEntityP @Override public BlockState getPolymerBlockState(BlockState blockState) { - return Blocks.SEA_LANTERN.getDefaultState(); + return Blocks.RESPAWN_ANCHOR.getDefaultState().with(RespawnAnchorBlock.CHARGES, RespawnAnchorBlock.MAX_CHARGES); } public TeleporterBlockEntity getBE(World world, BlockPos pos) { diff --git a/src/main/java/moe/nea/funnyteleporters/TeleporterBlockEntity.java b/src/main/java/moe/nea/funnyteleporters/TeleporterBlockEntity.java index 1c23576..2cc452c 100644 --- a/src/main/java/moe/nea/funnyteleporters/TeleporterBlockEntity.java +++ b/src/main/java/moe/nea/funnyteleporters/TeleporterBlockEntity.java @@ -1,22 +1,20 @@ package moe.nea.funnyteleporters; -import com.mojang.datafixers.util.Pair; -import com.mojang.serialization.DataResult; import net.minecraft.block.BlockState; import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.BlockEntityType; import net.minecraft.entity.Entity; import net.minecraft.nbt.NbtCompound; -import net.minecraft.nbt.NbtElement; import net.minecraft.nbt.NbtOps; import net.minecraft.registry.RegistryWrapper; -import net.minecraft.server.world.ServerWorld; import net.minecraft.util.TypeFilter; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Box; import net.minecraft.world.World; import org.jetbrains.annotations.Nullable; +import java.lang.ref.ReferenceQueue; +import java.lang.ref.WeakReference; import java.util.HashSet; import java.util.Set; @@ -37,7 +35,8 @@ public class TeleporterBlockEntity extends BlockEntity { var up = blockPos.up(); var newEntities = new HashSet<>(world.getEntitiesByType(TypeFilter.instanceOf(Entity.class), Box.enclosing(up, up), entity -> true)); for (Entity entity : newEntities) { - if (incomingEntities.remove(entity)) { + var ref = new Ref(entity); + if (incomingEntities.remove(ref)) { trackedEntities.remove(entity); continue; } @@ -46,6 +45,11 @@ public class TeleporterBlockEntity extends BlockEntity { } } trackedEntities = newEntities; + while (true) { + var ref = (Ref) refQueue.poll(); + if (ref == null) break; + incomingEntities.remove(ref); + } } @Override @@ -67,21 +71,39 @@ public class TeleporterBlockEntity extends BlockEntity { } } + class Ref extends WeakReference<Entity> { + public Ref(Entity referent) { + super(referent, refQueue); + hash = System.identityHashCode(referent); + } + + int hash; + + @Override + public int hashCode() { + return hash; + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof Ref ref) { + return ref.hash == this.hash && ref.get() == this.get(); + } + return false; + } + } + + private final ReferenceQueue<Entity> refQueue = new ReferenceQueue<>(); Set<Entity> trackedEntities = new HashSet<>(); - Set<Entity> incomingEntities = new HashSet<>(); + Set<Ref> incomingEntities = new HashSet<>(); @Nullable TeleporterDestination destination; void performTeleport(Entity subject) { if (destination == null) return; - var sourceWorld = subject.getWorld(); - var destinationWorld = destination.getDestinationWorld((ServerWorld) sourceWorld); - if (destinationWorld == null) return; - var destinationBE = destinationWorld.getBlockEntity(destination.blockPos()); - if (!(destinationBE instanceof TeleporterBlockEntity be)) { - return; - } - be.incomingEntities.add(subject); - var destPos = destination.blockPos().up().toBottomCenterPos(); - subject.teleport(destinationWorld, destPos.x, destPos.y, destPos.z, Set.of(), subject.getYaw(), subject.getPitch()); + destination.teleport(subject); + } + + public void addIncoming(Entity subject) { + incomingEntities.add(new Ref(subject)); } } diff --git a/src/main/java/moe/nea/funnyteleporters/TeleporterDestination.java b/src/main/java/moe/nea/funnyteleporters/TeleporterDestination.java index d47ae1f..44e6d46 100644 --- a/src/main/java/moe/nea/funnyteleporters/TeleporterDestination.java +++ b/src/main/java/moe/nea/funnyteleporters/TeleporterDestination.java @@ -2,16 +2,64 @@ package moe.nea.funnyteleporters; import com.mojang.serialization.Codec; import com.mojang.serialization.codecs.RecordCodecBuilder; +import net.minecraft.entity.Entity; +import net.minecraft.item.Item; +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.world.ServerWorld; +import net.minecraft.text.MutableText; +import net.minecraft.text.Text; +import net.minecraft.util.Identifier; 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; public record TeleporterDestination( RegistryKey<World> target, BlockPos blockPos -) { +) implements Comparable<TeleporterDestination> { + public static Comparator<TeleporterDestination> COMPARATOR = + Comparator.<TeleporterDestination, Identifier>comparing(it -> it.target().getValue()) + .thenComparing(TeleporterDestination::blockPos); + + @Override + public int compareTo(@NotNull TeleporterDestination o) { + 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); + if (destinationWorld == null) return; + var destinationBE = destinationWorld.getBlockEntity(blockPos()); + if (!(destinationBE instanceof TeleporterBlockEntity be)) { + return; + } + be.addIncoming(subject); + var destPos = blockPos().up().toBottomCenterPos(); + subject.teleport(destinationWorld, destPos.x, destPos.y, destPos.z, Set.of(), subject.getYaw(), subject.getPitch()); + } + + public record Labeled(RegistryEntry<Item> item, TeleporterDestination destination, Optional<String> name) { + public static Codec<Labeled> CODEC = RecordCodecBuilder.create(instance -> instance.group( + Registries.ITEM.getEntryCodec().fieldOf("item").forGetter(Labeled::item), + TeleporterDestination.CODEC.fieldOf("destination").forGetter(Labeled::destination), + Codec.STRING.optionalFieldOf("name").forGetter(Labeled::name) + ).apply(instance, Labeled::new)); + } + public ServerWorld getDestinationWorld(ServerWorld world) { return world.getServer().getWorld(target); } diff --git a/src/main/java/moe/nea/funnyteleporters/TeleporterNexus.java b/src/main/java/moe/nea/funnyteleporters/TeleporterNexus.java new file mode 100644 index 0000000..26c5382 --- /dev/null +++ b/src/main/java/moe/nea/funnyteleporters/TeleporterNexus.java @@ -0,0 +1,42 @@ +package moe.nea.funnyteleporters; + +import eu.pb4.polymer.core.api.block.PolymerBlock; +import net.minecraft.block.Block; +import net.minecraft.block.BlockEntityProvider; +import net.minecraft.block.BlockState; +import net.minecraft.block.Blocks; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.util.ActionResult; +import net.minecraft.util.hit.BlockHitResult; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; +import org.jetbrains.annotations.Nullable; + +import java.util.Objects; + +public class TeleporterNexus extends Block implements PolymerBlock, BlockEntityProvider { + public TeleporterNexus(Settings settings) { + super(settings); + } + + @Override + public @Nullable TeleporterNexusBlockEntity createBlockEntity(BlockPos pos, BlockState state) { + return new TeleporterNexusBlockEntity(pos, state); + } + + public TeleporterNexusBlockEntity getBE(World world, BlockPos pos) { + return (TeleporterNexusBlockEntity) Objects.requireNonNull(world.getBlockEntity(pos)); + } + + @Override + public BlockState getPolymerBlockState(BlockState blockState) { + return Blocks.ENCHANTING_TABLE.getDefaultState(); + } + + @Override + protected ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, BlockHitResult hit) { + new TeleporterNexusScreen(getBE(world, pos), (ServerPlayerEntity) player).open(); + return ActionResult.SUCCESS_NO_ITEM_USED; + } +} diff --git a/src/main/java/moe/nea/funnyteleporters/TeleporterNexusBlockEntity.java b/src/main/java/moe/nea/funnyteleporters/TeleporterNexusBlockEntity.java new file mode 100644 index 0000000..dbc8150 --- /dev/null +++ b/src/main/java/moe/nea/funnyteleporters/TeleporterNexusBlockEntity.java @@ -0,0 +1,96 @@ +package moe.nea.funnyteleporters; + +import com.mojang.datafixers.util.Pair; +import com.mojang.serialization.Codec; +import net.minecraft.block.BlockState; +import net.minecraft.block.entity.BlockEntity; +import net.minecraft.block.entity.BlockEntityType; +import net.minecraft.item.Item; +import net.minecraft.item.ItemConvertible; +import net.minecraft.item.Items; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.nbt.NbtOps; +import net.minecraft.registry.Registries; +import net.minecraft.registry.RegistryWrapper; +import net.minecraft.registry.entry.RegistryEntry; +import net.minecraft.util.math.BlockPos; + +import java.util.Optional; +import java.util.TreeMap; + +public class TeleporterNexusBlockEntity extends BlockEntity { + public TeleporterNexusBlockEntity(BlockPos pos, BlockState state) {this(FunnyRegistry.TELEPORTER_NEXUS_ENTITY, pos, state);} + + protected TeleporterNexusBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) { + super(type, pos, state); + } + + public Optional<String> getName(TeleporterDestination dest) { + var label = destinations.get(dest); + return label != null ? (label.label) : Optional.empty(); + } + + public ItemConvertible getIcon(TeleporterDestination dest) { + var label = destinations.get(dest); + return (label != null ? label.item : getDefaultItem()).value(); + } + + public record Label( + RegistryEntry<Item> item, + Optional<String> label + ) { + } + + public TreeMap<TeleporterDestination, Label> destinations = new TreeMap<>(); + private static final Codec<TreeMap<TeleporterDestination, Label>> DESTINATION_CODEC = + TeleporterDestination.Labeled.CODEC + .listOf() + .xmap( + pairs -> { + var hash = new TreeMap<TeleporterDestination, Label>(); + for (TeleporterDestination.Labeled pair : pairs) { + hash.put(pair.destination(), new Label(pair.item(), pair.name())); + } + return hash; + }, + map -> map.entrySet().stream().map(it -> new TeleporterDestination.Labeled(it.getValue().item(), it.getKey(), it.getValue().label())).toList() + ); + + static RegistryEntry<Item> getDefaultItem() { + return Registries.ITEM.getEntry(Items.ENDER_PEARL); + } + + @Override + protected void readNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLookup) { + super.readNbt(nbt, registryLookup); + var result = DESTINATION_CODEC.decode(registryLookup.getOps(NbtOps.INSTANCE), nbt.get("destinations")); + result.resultOrPartial().map(Pair::getFirst).ifPresent(it -> destinations = it); + } + + @Override + protected void writeNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLookup) { + super.writeNbt(nbt, registryLookup); + nbt.put("destinations", DESTINATION_CODEC.encodeStart(registryLookup.getOps(NbtOps.INSTANCE), destinations).getPartialOrThrow()); + } + + public void addDestination(TeleporterDestination destination) { + destinations.putIfAbsent(destination, new Label(getDefaultItem(), Optional.empty())); + markDirty(); + } + + public void removeDestination(TeleporterDestination destination) { + destinations.remove(destination); + markDirty(); + } + + public void setIcon(TeleporterDestination dest, Item item) { + var entry = Registries.ITEM.getEntry(item); + destinations.compute(dest, (key, old) -> old == null ? new Label(entry, Optional.empty()) : new Label(entry, old.label())); + markDirty(); + } + + public void setName(TeleporterDestination dest, Optional<String> string) { + destinations.compute(dest, (key, old) -> old == null ? new Label(getDefaultItem(), string) : new Label(old.item(), string)); + markDirty(); + } +} diff --git a/src/main/java/moe/nea/funnyteleporters/TeleporterNexusEditorScreen.java b/src/main/java/moe/nea/funnyteleporters/TeleporterNexusEditorScreen.java new file mode 100644 index 0000000..7fe55a6 --- /dev/null +++ b/src/main/java/moe/nea/funnyteleporters/TeleporterNexusEditorScreen.java @@ -0,0 +1,86 @@ +package moe.nea.funnyteleporters; + +import eu.pb4.sgui.api.ClickType; +import eu.pb4.sgui.api.elements.GuiElementBuilder; +import eu.pb4.sgui.api.gui.SimpleGui; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.item.Items; +import net.minecraft.registry.Registries; +import net.minecraft.screen.ScreenHandlerType; +import net.minecraft.screen.slot.SlotActionType; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.text.Text; +import net.minecraft.util.Formatting; + +public class TeleporterNexusEditorScreen extends SimpleGui { + final TeleporterNexusBlockEntity blockEntity; + final TeleporterDestination dest; + + public TeleporterNexusEditorScreen(TeleporterNexusBlockEntity blockEntity, TeleporterDestination dest, ServerPlayerEntity player) { + super(ScreenHandlerType.GENERIC_9X3, player, false); + this.blockEntity = blockEntity; + this.dest = dest; + setSlots(); + } + + void setSlots() { + setTitle(Text.literal("Set Icon for " + blockEntity.getName(dest).orElse("teleporter"))); + for (int i = 0; i < width * height; i++) { + setSlot(i, Utils.createBlankBlack()); + } + setSlot(4 + 9, + GuiElementBuilder.from(new ItemStack(blockEntity.getIcon(dest))) + .setName(Text.literal("Click item in your inventory to set icon."))); + setSlot(2 + 9, + GuiElementBuilder.from(new ItemStack(Items.OAK_SIGN)) + .setName(Text.literal("Click here to change name")) + .addLoreLine(Text.literal("Click here to set a name for the teleport destination") + .setStyle(Utils.colouredLoreStyle(Formatting.AQUA))) + .setCallback(this::handleNameChange)); + setSlot(9 + 6, + GuiElementBuilder.from(new ItemStack(Items.BARRIER)) + .setName(Text.literal("Delete").withColor(0xFFFF0000)) + .addLoreLine(Text.literal("Shift-Click here to remove this location") + .setStyle(Utils.colouredLoreStyle(Formatting.RED))) + .addLoreLine(Text.literal("from the teleport nexus") + .setStyle(Utils.colouredLoreStyle(Formatting.RED))) + .setCallback(this::handleDelete)); + } + + void handleNameChange() { + new TeleporterNexusNameEditorScreen(this).open(); + } + + void handleDelete(ClickType clickType) { + if (clickType == ClickType.MOUSE_LEFT_SHIFT) { + blockEntity.removeDestination(dest); + back(); + } else { + player.sendMessage(Text.literal("You need to shift click to delete locations.")); + } + } + + @Override + public void onClose() { + back(); + } + + void back() { + new TeleporterNexusScreen(blockEntity, player).open(); + } + + @Override + public boolean onAnyClick(int index, ClickType type, SlotActionType action) { + var slot = getSlotRedirectOrPlayer(index); + if (slot == null) return true; + var stack = slot.getStack(); + if (!stack.isEmpty()) { + blockEntity.setIcon(dest, stack.getItem()); + setSlots(); + if (screenHandler != null) + screenHandler.syncState(); + } + return false; + } +} diff --git a/src/main/java/moe/nea/funnyteleporters/TeleporterNexusNameEditorScreen.java b/src/main/java/moe/nea/funnyteleporters/TeleporterNexusNameEditorScreen.java new file mode 100644 index 0000000..f4d33ea --- /dev/null +++ b/src/main/java/moe/nea/funnyteleporters/TeleporterNexusNameEditorScreen.java @@ -0,0 +1,29 @@ +package moe.nea.funnyteleporters; + +import eu.pb4.sgui.api.elements.GuiElementBuilder; +import eu.pb4.sgui.api.gui.AnvilInputGui; +import net.minecraft.item.ItemStack; +import net.minecraft.item.Items; +import net.minecraft.text.Text; + +import java.util.Optional; + +public class TeleporterNexusNameEditorScreen extends AnvilInputGui { + private final TeleporterNexusEditorScreen previous; + + public TeleporterNexusNameEditorScreen(TeleporterNexusEditorScreen previous) { + super(previous.getPlayer(), false); + this.previous = previous; + setTitle(Text.literal("Edit Name")); + setSlot(2, GuiElementBuilder.from(new ItemStack(Items.OAK_SIGN)) + .setName(Text.literal("Click here to save")) + .setCallback(this::save)); + setDefaultInputValue(previous.blockEntity.getName(previous.dest).orElse("")); + } + + void save() { + previous.blockEntity.setName(previous.dest, Optional.of(getInput())); + previous.setSlots(); + previous.open(); + } +} diff --git a/src/main/java/moe/nea/funnyteleporters/TeleporterNexusScreen.java b/src/main/java/moe/nea/funnyteleporters/TeleporterNexusScreen.java new file mode 100644 index 0000000..4a9088e --- /dev/null +++ b/src/main/java/moe/nea/funnyteleporters/TeleporterNexusScreen.java @@ -0,0 +1,85 @@ +package moe.nea.funnyteleporters; + +import eu.pb4.sgui.api.ClickType; +import eu.pb4.sgui.api.elements.GuiElementBuilder; +import eu.pb4.sgui.api.elements.GuiElementBuilderInterface; +import eu.pb4.sgui.api.gui.SimpleGui; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.item.Items; +import net.minecraft.registry.entry.RegistryEntry; +import net.minecraft.screen.ScreenHandlerType; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.text.Style; +import net.minecraft.text.Text; +import net.minecraft.util.Formatting; + +import java.util.Map; + +public class TeleporterNexusScreen extends SimpleGui { + private final TeleporterNexusBlockEntity blockEntity; + + public TeleporterNexusScreen(TeleporterNexusBlockEntity blockEntity, ServerPlayerEntity player) { + super(ScreenHandlerType.GENERIC_9X6, player, false); + this.blockEntity = blockEntity; + setSlots(); + setTitle(Text.literal("Teleport Nexus")); + } + + int scroll = 0; + + void setSlots() { + for (int i = 0; i < 9; i++) { + setSlot(i, Utils.createBlankBlack()); + setSlot(i + (height - 1) * width, Utils.createBlankBlack()); + } + if (scroll > 0) { + setSlot(0, GuiElementBuilder.from(new ItemStack(Items.ARROW)) + .setName(Text.literal("Previous page")) + .setCallback(() -> scroll(-1))); + } + var it = blockEntity.destinations.entrySet().iterator(); + Utils.skipIt(it, scroll * width * (height - 2)); + for (int i = 1; i < height - 1 && it.hasNext(); i++) { + for (int j = 0; j < width && it.hasNext(); j++) { + int index = i * width + j; + var entry = it.next(); + setSlot(index, getHandlerForEntry(entry)); + } + } + if (it.hasNext()) { + setSlot(1, GuiElementBuilder.from(new ItemStack(Items.ARROW)) + .setName(Text.literal("Next page")) + .setCallback(() -> scroll(1))); + } + } + + void scroll(int offset) { + scroll = Math.max(0, scroll + offset); + setSlots(); + } + + void handleClick(ClickType clickType, TeleporterDestination destination) { + if (clickType == ClickType.MOUSE_RIGHT) { + new TeleporterNexusEditorScreen(blockEntity, destination, player).open(); + return; + } + destination.teleport(player); + close(); + } + + private GuiElementBuilderInterface<?> getHandlerForEntry(Map.Entry<TeleporterDestination, TeleporterNexusBlockEntity.Label> entry) { + var dest = entry.getKey(); + return GuiElementBuilder.from(new ItemStack(entry.getValue().item())) + .setName(Text.literal("Teleport to" + entry.getValue().label().map(it -> " " + it).orElse(""))) + .hideDefaultTooltip() + .addLoreLine(Text.literal(String.format("x: %d, y: %d, z: %d", dest.blockPos().getX(), dest.blockPos().getY(), dest.blockPos().getZ())) + .setStyle(Style.EMPTY.withItalic(false).withColor(Formatting.AQUA))) + .addLoreLine(Text.literal("in " + dest.target().getValue()) + .setStyle(Style.EMPTY.withItalic(false).withColor(Formatting.AQUA))) + .addLoreLine(Text.empty()) + .addLoreLine(Text.literal("Left-Click to teleport.").setStyle(Style.EMPTY.withItalic(false).withColor(Formatting.GRAY))) + .addLoreLine(Text.literal("Right-Click to edit item.").setStyle(Style.EMPTY.withItalic(false).withColor(Formatting.GRAY))) + .setCallback(clickType -> handleClick(clickType, dest)); + } +} diff --git a/src/main/java/moe/nea/funnyteleporters/TeleporterWand.java b/src/main/java/moe/nea/funnyteleporters/TeleporterWand.java index 96ed941..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,18 +31,46 @@ 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) { - if (context.getWorld().getBlockState(context.getBlockPos()).getBlock() == FunnyRegistry.TELEPORTER) { + var destination = context.getStack().get(FunnyRegistry.TELEPORTER_DESTINATION); + var block = context.getWorld().getBlockState(context.getBlockPos()).getBlock(); + if (block == FunnyRegistry.TELEPORTER_NEXUS && destination != null) { + var nexus = FunnyRegistry.TELEPORTER_NEXUS.getBE(context.getWorld(), context.getBlockPos()); + if (context.getPlayer() != null) + context.getPlayer().sendMessage(Text.literal("Saved destination from wand into nexus.")); + nexus.addDestination(destination); + } + if (block == FunnyRegistry.TELEPORTER) { 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 = context.getStack().get(FunnyRegistry.TELEPORTER_DESTINATION); + be.destination = destination; be.markDirty(); if (context.getPlayer() != null) { - context.getPlayer().sendMessage(Text.literal("Set new target destination to wand.")); + context.getPlayer().sendMessage(Text.literal("Set new target destination from wand.")); } } return ActionResult.SUCCESS_NO_ITEM_USED; diff --git a/src/main/java/moe/nea/funnyteleporters/Utils.java b/src/main/java/moe/nea/funnyteleporters/Utils.java new file mode 100644 index 0000000..b0a1649 --- /dev/null +++ b/src/main/java/moe/nea/funnyteleporters/Utils.java @@ -0,0 +1,34 @@ +package moe.nea.funnyteleporters; + +import net.minecraft.component.DataComponentTypes; +import net.minecraft.item.ItemStack; +import net.minecraft.item.Items; +import net.minecraft.text.Style; +import net.minecraft.text.Text; +import net.minecraft.util.Formatting; +import net.minecraft.util.Unit; + +import java.util.Iterator; + +public class Utils { + public static ItemStack createBlankBlack() { + var s = new ItemStack(Items.BLACK_STAINED_GLASS_PANE); + s.set(DataComponentTypes.CUSTOM_NAME, Text.empty()); + s.set(DataComponentTypes.HIDE_TOOLTIP, Unit.INSTANCE); + return s; + } + + public static void skipIt(Iterator<?> it, int skipCount) { + for (; skipCount > 0 && it.hasNext(); skipCount--) { + it.next(); + } + } + + public static Style emptyLoreStyle() { + return Style.EMPTY.withItalic(false); + } + + public static Style colouredLoreStyle(Formatting formatting) { + return emptyLoreStyle().withColor(formatting); + } +} |