summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2024-11-20 18:51:13 +0100
committerLinnea Gräf <nea@nea.moe>2024-11-20 18:51:13 +0100
commit14f272f5db13d56de538b71df79f2dccbec0475f (patch)
treee27491c39e8363ebacdf48f80fff94167d125a33 /src
parent241682c95b56f47cbd09f79dd77c6ad8ee4489c5 (diff)
downloadfunny-teleporters-14f272f5db13d56de538b71df79f2dccbec0475f.tar.gz
funny-teleporters-14f272f5db13d56de538b71df79f2dccbec0475f.tar.bz2
funny-teleporters-14f272f5db13d56de538b71df79f2dccbec0475f.zip
Add inventory
Diffstat (limited to 'src')
-rw-r--r--src/main/java/moe/nea/funnyteleporters/ColouredChestBlock.java15
-rw-r--r--src/main/java/moe/nea/funnyteleporters/ColouredChestBlockEntity.java114
-rw-r--r--src/main/java/moe/nea/funnyteleporters/ColouredChestConfigGUI.java91
3 files changed, 209 insertions, 11 deletions
diff --git a/src/main/java/moe/nea/funnyteleporters/ColouredChestBlock.java b/src/main/java/moe/nea/funnyteleporters/ColouredChestBlock.java
index eb68f95..cf756e0 100644
--- a/src/main/java/moe/nea/funnyteleporters/ColouredChestBlock.java
+++ b/src/main/java/moe/nea/funnyteleporters/ColouredChestBlock.java
@@ -2,15 +2,11 @@ package moe.nea.funnyteleporters;
import com.mojang.serialization.MapCodec;
import eu.pb4.polymer.core.api.block.PolymerBlock;
-import net.minecraft.block.AbstractBlock;
import net.minecraft.block.AbstractChestBlock;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
-import net.minecraft.block.ChestBlock;
import net.minecraft.block.DoubleBlockProperties;
-import net.minecraft.block.EnderChestBlock;
-import net.minecraft.block.FacingBlock;
import net.minecraft.block.HorizontalFacingBlock;
import net.minecraft.block.Waterloggable;
import net.minecraft.block.entity.BlockEntity;
@@ -20,10 +16,10 @@ import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.fluid.FluidState;
import net.minecraft.fluid.Fluids;
import net.minecraft.item.ItemPlacementContext;
+import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.Properties;
import net.minecraft.state.property.Property;
-import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.BlockMirror;
import net.minecraft.util.BlockRotation;
@@ -33,6 +29,8 @@ import net.minecraft.util.math.Direction;
import net.minecraft.world.World;
import net.minecraft.world.WorldAccess;
+import java.util.Objects;
+
public class ColouredChestBlock extends AbstractChestBlock<ColouredChestBlockEntity> implements PolymerBlock, Waterloggable {
static Property<Direction> FACING = HorizontalFacingBlock.FACING;
static Property<Boolean> WATERLOGGED = Properties.WATERLOGGED;
@@ -62,9 +60,10 @@ public class ColouredChestBlock extends AbstractChestBlock<ColouredChestBlockEnt
@Override
protected ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, BlockHitResult hit) {
- // TODO: create a ui or something to configure frequency
- player.sendMessage(Text.literal("Ender Chest geöffnet" + player));
- return super.onUse(state, world, pos, player, hit);
+ if (player instanceof ServerPlayerEntity serverPlayer) {
+ ((ColouredChestBlockEntity) Objects.requireNonNull(world.getBlockEntity(pos))).openScreen(serverPlayer);
+ }
+ return ActionResult.SUCCESS_NO_ITEM_USED;
}
@Override
diff --git a/src/main/java/moe/nea/funnyteleporters/ColouredChestBlockEntity.java b/src/main/java/moe/nea/funnyteleporters/ColouredChestBlockEntity.java
index e26aea1..ba09c5e 100644
--- a/src/main/java/moe/nea/funnyteleporters/ColouredChestBlockEntity.java
+++ b/src/main/java/moe/nea/funnyteleporters/ColouredChestBlockEntity.java
@@ -1,12 +1,36 @@
package moe.nea.funnyteleporters;
-import net.minecraft.block.Block;
+import com.mojang.serialization.Codec;
+import com.mojang.serialization.DataResult;
+import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.block.BlockState;
+import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;
-import net.minecraft.block.entity.ChestBlockEntity;
+import net.minecraft.entity.player.PlayerEntity;
+import net.minecraft.inventory.Inventories;
+import net.minecraft.inventory.Inventory;
+import net.minecraft.item.ItemStack;
+import net.minecraft.nbt.NbtCompound;
+import net.minecraft.nbt.NbtOps;
+import net.minecraft.registry.RegistryWrapper;
+import net.minecraft.server.network.ServerPlayerEntity;
+import net.minecraft.util.DyeColor;
+import net.minecraft.util.collection.DefaultedList;
import net.minecraft.util.math.BlockPos;
-public class ColouredChestBlockEntity extends ChestBlockEntity {
+import java.util.List;
+
+public class ColouredChestBlockEntity extends BlockEntity implements Inventory {
+ private DefaultedList<ItemStack> inventory = DefaultedList.ofSize(size(), ItemStack.EMPTY);
+
+ public record ExtraData(
+ List<DyeColor> frequency
+ ) {
+ public static Codec<ExtraData> CODEC = RecordCodecBuilder.create(instance -> instance.group(
+ DyeColor.CODEC.listOf().validate(it -> it.size() == 3 ? DataResult.success(it) : (it.size() > 3 ? DataResult.error(() -> "List " + it + "too long", it.subList(0, 3)) : DataResult.error(() -> "List " + it + " too short"))).fieldOf("frequency").forGetter(ExtraData::frequency)
+ ).apply(instance, ExtraData::new));
+ }
+
public ColouredChestBlockEntity(BlockPos blockPos, BlockState blockState) {
this(FunnyRegistry.COLOURED_CHEST_ENTITY, blockPos, blockState);
}
@@ -14,4 +38,88 @@ public class ColouredChestBlockEntity extends ChestBlockEntity {
protected ColouredChestBlockEntity(BlockEntityType<?> blockEntityType, BlockPos blockPos, BlockState blockState) {
super(blockEntityType, blockPos, blockState);
}
+
+ @Override
+ protected void writeNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLookup) {
+ super.writeNbt(nbt, registryLookup);
+ Inventories.writeNbt(nbt, inventory, registryLookup);
+ var result = ExtraData.CODEC.encodeStart(NbtOps.INSTANCE, extra);
+ if (result.hasResultOrPartial()) {
+ nbt.put("extra", result.getPartialOrThrow());
+ }
+ }
+
+ ExtraData extra = new ExtraData(List.of(DyeColor.BLUE, DyeColor.BLUE, DyeColor.BLUE));
+
+ public int size() {
+ return 27;
+ }
+
+ @Override
+ public boolean isEmpty() {
+ for (ItemStack itemStack : inventory) {
+ if (!itemStack.isEmpty()) return false;
+ }
+ return true;
+ }
+
+ @Override
+ public ItemStack getStack(int slot) {
+ if (0 <= slot && slot < size())
+ return inventory.get(slot);
+ return ItemStack.EMPTY;
+ }
+
+ @Override
+ public ItemStack removeStack(int slot, int amount) {
+ ItemStack itemStack = Inventories.splitStack(this.inventory, slot, amount);
+ if (!itemStack.isEmpty()) {
+ markDirty();
+ }
+
+ return itemStack;
+ }
+
+ @Override
+ public ItemStack removeStack(int slot) {
+ markDirty();
+ return Inventories.removeStack(inventory, slot);
+ }
+
+ @Override
+ public void setStack(int slot, ItemStack stack) {
+ if (0 <= slot && slot < size()) {
+ markDirty();
+ inventory.set(slot, stack);
+ }
+ }
+
+ @Override
+ public boolean canPlayerUse(PlayerEntity player) {
+ return true;
+ }
+
+ @Override
+ protected void readNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLookup) {
+ super.readNbt(nbt, registryLookup);
+ inventory = DefaultedList.ofSize(size(), ItemStack.EMPTY);
+ Inventories.readNbt(nbt, inventory, registryLookup);
+ if (nbt.contains("extra")) {
+ var extra = ExtraData.CODEC.decode(NbtOps.INSTANCE, nbt.get("extra"));
+ if (extra.hasResultOrPartial()) {
+ this.extra = extra.getPartialOrThrow().getFirst();
+ }
+ }
+ }
+
+ @Override
+ public void clear() {
+ markDirty();
+ inventory.clear();
+ }
+
+ public void openScreen(ServerPlayerEntity player) {
+ new ColouredChestConfigGUI(this, player).open();
+ }
}
+
diff --git a/src/main/java/moe/nea/funnyteleporters/ColouredChestConfigGUI.java b/src/main/java/moe/nea/funnyteleporters/ColouredChestConfigGUI.java
new file mode 100644
index 0000000..8c1aff8
--- /dev/null
+++ b/src/main/java/moe/nea/funnyteleporters/ColouredChestConfigGUI.java
@@ -0,0 +1,91 @@
+package moe.nea.funnyteleporters;
+
+import com.google.common.collect.ImmutableList;
+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.DyeItem;
+import net.minecraft.item.ItemStack;
+import net.minecraft.item.Items;
+import net.minecraft.screen.ScreenHandlerType;
+import net.minecraft.screen.slot.SlotActionType;
+import net.minecraft.server.network.ServerPlayerEntity;
+import net.minecraft.text.Style;
+import net.minecraft.text.Text;
+import net.minecraft.util.DyeColor;
+import net.minecraft.util.Formatting;
+import net.minecraft.util.math.MathHelper;
+
+import java.util.ArrayList;
+import java.util.Collections;
+
+public class ColouredChestConfigGUI extends SimpleGui {
+
+ private final ColouredChestBlockEntity blockEntity;
+
+ public ColouredChestConfigGUI(ColouredChestBlockEntity blockEntity, ServerPlayerEntity player) {
+ super(ScreenHandlerType.GENERIC_3X3, player, false);
+ setTitle(Text.literal("Funny Enderchest"));
+ this.blockEntity = blockEntity;
+ this.updateFromBlockEntity();
+ }
+
+ ItemStack createUpItem(int frequency, String direction) {
+ var is = new ItemStack(Items.ARROW);
+ is.set(DataComponentTypes.CUSTOM_NAME, Text.literal("Frequency " + frequency + " " + direction).setStyle(Style.EMPTY.withItalic(false).withColor(Formatting.AQUA)));
+ return is;
+ }
+
+ private ItemStack createFrequencyItem(int frequency, DyeColor dyeColor) {
+ var is = new ItemStack(DyeItem.byColor(dyeColor));
+ is.set(DataComponentTypes.CUSTOM_NAME, Text.literal("Frequency " + frequency + ": " + dyeColor.getId()).setStyle(Style.EMPTY.withItalic(false).withColor(dyeColor.getSignColor())));
+ return is;
+ }
+
+ @Override
+ public boolean onClick(int index, ClickType type, SlotActionType action, GuiElementInterface element) {
+ if (index == 0)
+ cycleColour(0, 1);
+ else if (index == 6)
+ cycleColour(0, -1);
+ else if (index == 1)
+ cycleColour(1, 1);
+ else if (index == 7)
+ cycleColour(1, -1);
+ else if (index == 2)
+ cycleColour(2, 1);
+ else if (index == 8)
+ cycleColour(2, -1);
+ else return super.onClick(index, type, action, element);
+ return false;
+ }
+
+ void cycleColour(int frequencyIndex, int direction) {
+ var newList = new ArrayList<>(blockEntity.extra.frequency());
+ newList.set(frequencyIndex, cycleColour(newList.get(frequencyIndex), direction));
+ blockEntity.extra = new ColouredChestBlockEntity.ExtraData(Collections.unmodifiableList(newList));
+ updateFromBlockEntity();
+ blockEntity.markDirty();
+ }
+
+ private DyeColor cycleColour(DyeColor dyeColor, int direction) {
+ return DyeColor.values()[MathHelper.floorMod(dyeColor.ordinal() + direction, DyeColor.values().length)];
+ }
+
+ public void updateFromBlockEntity() {
+ setSlot(0, createUpItem(0, "up"));
+ setSlot(3, createFrequencyItem(0, blockEntity.extra.frequency().get(0)));
+ setSlot(6, createUpItem(0, "down"));
+
+ setSlot(1, createUpItem(1, "up"));
+ setSlot(4, createFrequencyItem(1, blockEntity.extra.frequency().get(1)));
+ setSlot(7, createUpItem(1, "down"));
+
+ setSlot(2, createUpItem(2, "up"));
+ setSlot(5, createFrequencyItem(2, blockEntity.extra.frequency().get(2)));
+ setSlot(8, createUpItem(2, "down"));
+ }
+
+
+}