Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement fake hopper inventory #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 1 addition & 35 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>com.nukkitx</groupId>
<artifactId>fakeinventories</artifactId>
<name>Fake Inventories</name>
<version>1.0.3-SNAPSHOT</version>
<version>1.0.4-SNAPSHOT</version>

<licenses>
<license>
Expand All @@ -17,40 +17,6 @@
</license>
</licenses>

<organization>
<name>NukkitX</name>
<url>https://github.com/FakeInventories</url>
</organization>

<issueManagement>
<system>GitHub</system>
<url>https://github.com/NukkitX/FakeInventories/issues</url>
</issueManagement>

<ciManagement>
<system>Jenkins</system>
<url>https://ci.nukkitx.com/job/NukkitX/job/FakeInventories</url>
</ciManagement>

<scm>
<connection>scm:git:https://github.com/NukkitX/FakeInventories.git</connection>
<developerConnection>scm:git:[email protected]:NukkitX/FakeInventories.git</developerConnection>
<url>https://github.com/NukkitX/FakeInventories</url>
</scm>

<distributionManagement>
<repository>
<id>releases</id>
<name>nukkitx-releases</name>
<url>https://repo.nukkitx.com/release</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>nukkitx-snapshots</name>
<url>https://repo.nukkitx.com/snapshot</url>
</snapshotRepository>
</distributionManagement>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<BlockVector3> 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");
}
}
}