diff --git a/pom.xml b/pom.xml index c8b35f9..7697b09 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ com.nukkitx fakeinventories Fake Inventories - 1.0.3-SNAPSHOT + 1.0.4-SNAPSHOT @@ -17,40 +17,6 @@ - - NukkitX - https://github.com/FakeInventories - - - - GitHub - https://github.com/NukkitX/FakeInventories/issues - - - - Jenkins - https://ci.nukkitx.com/job/NukkitX/job/FakeInventories - - - - scm:git:https://github.com/NukkitX/FakeInventories.git - scm:git:git@github.com:NukkitX/FakeInventories.git - https://github.com/NukkitX/FakeInventories - - - - - releases - nukkitx-releases - https://repo.nukkitx.com/release - - - snapshots - nukkitx-snapshots - https://repo.nukkitx.com/snapshot - - - 1.8 1.8 diff --git a/src/main/java/com/nukkitx/fakeinventories/inventory/HopperFakeInventory.java b/src/main/java/com/nukkitx/fakeinventories/inventory/HopperFakeInventory.java new file mode 100644 index 0000000..e903617 --- /dev/null +++ b/src/main/java/com/nukkitx/fakeinventories/inventory/HopperFakeInventory.java @@ -0,0 +1,81 @@ +package com.nukkitx.fakeinventories.inventory; + +import cn.nukkit.Player; +import cn.nukkit.block.BlockID; +import cn.nukkit.blockentity.BlockEntity; +import cn.nukkit.inventory.InventoryHolder; +import cn.nukkit.inventory.InventoryType; +import cn.nukkit.level.GlobalBlockPalette; +import cn.nukkit.math.BlockVector3; +import cn.nukkit.nbt.NBTIO; +import cn.nukkit.nbt.tag.CompoundTag; +import cn.nukkit.network.protocol.BlockEntityDataPacket; +import cn.nukkit.network.protocol.UpdateBlockPacket; +import lombok.Getter; +import lombok.Setter; + +import java.io.IOException; +import java.nio.ByteOrder; +import java.util.Collections; +import java.util.List; + +public class HopperFakeInventory extends FakeInventory { + @Getter + @Setter + private String name; + + public HopperFakeInventory() { + this(null); + } + + public HopperFakeInventory(InventoryHolder holder) { + this(holder, null); + } + + public HopperFakeInventory(InventoryHolder holder, String title) { + super(InventoryType.HOPPER, holder, title); + } + + @Override + protected List onOpenBlock(Player who) { + BlockVector3 blockPosition = new BlockVector3((int) who.x, ((int) who.y) + 2, (int) who.z); + + placeHopper(who, blockPosition); + + return Collections.singletonList(blockPosition); + } + + protected void placeHopper(Player who, BlockVector3 pos) { + UpdateBlockPacket updateBlock = new UpdateBlockPacket(); + updateBlock.blockRuntimeId = GlobalBlockPalette.getOrCreateRuntimeId(BlockID.HOPPER_BLOCK, 0); + updateBlock.flags = UpdateBlockPacket.FLAG_ALL_PRIORITY; + updateBlock.x = pos.x; + updateBlock.y = pos.y; + updateBlock.z = pos.z; + + who.dataPacket(updateBlock); + + BlockEntityDataPacket blockEntityData = new BlockEntityDataPacket(); + blockEntityData.x = pos.x; + blockEntityData.y = pos.y; + blockEntityData.z = pos.z; + blockEntityData.namedTag = getNbt(pos, getName()); + + who.dataPacket(blockEntityData); + } + + private static byte[] getNbt(BlockVector3 pos, String name) { + CompoundTag tag = new CompoundTag() + .putString("id", BlockEntity.HOPPER) + .putInt("x", pos.x) + .putInt("y", pos.y) + .putInt("z", pos.z) + .putString("CustomName", name == null ? "Hopper" : name); + + try { + return NBTIO.write(tag, ByteOrder.LITTLE_ENDIAN, true); + } catch (IOException e) { + throw new RuntimeException("Unable to create NBT for hopper"); + } + } +}