Skip to content

Commit e431451

Browse files
committed
Added a way to send enderchest content to the client
1 parent 9b64d4b commit e431451

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed

bukkit-1.17/src/main/java/net/badlion/bukkitapi/BukkitBadlionPlugin.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import net.badlion.bukkitapi.command.SurvivalCommand;
44
import net.badlion.bukkitapi.gui.SurvivalGui;
5+
import net.badlion.bukkitapi.listener.EnderchestListener;
56
import net.badlion.bukkitapi.survival.SurvivalManager;
67

78
import java.util.Objects;
@@ -20,6 +21,8 @@ public void onEnable() {
2021

2122
this.survivalGui = new SurvivalGui(this);
2223

24+
this.getServer().getPluginManager().registerEvents(new EnderchestListener(this), this);
25+
2326
Objects.requireNonNull(this.getCommand("survivalapi")).setExecutor(new SurvivalCommand(this));
2427
}
2528

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package net.badlion.bukkitapi.listener;
2+
3+
import com.google.gson.JsonObject;
4+
import com.google.gson.JsonPrimitive;
5+
import net.badlion.bukkitapi.BukkitBadlionPlugin;
6+
import net.badlion.modapicommon.mods.ModType;
7+
import net.badlion.modapicommon.survival.SurvivalFeature;
8+
import org.bukkit.entity.Player;
9+
import org.bukkit.event.EventHandler;
10+
import org.bukkit.event.Listener;
11+
import org.bukkit.event.inventory.InventoryClickEvent;
12+
import org.bukkit.event.inventory.InventoryType;
13+
import org.bukkit.event.player.PlayerJoinEvent;
14+
import org.bukkit.inventory.Inventory;
15+
16+
import java.io.ByteArrayOutputStream;
17+
import java.io.OutputStream;
18+
import java.util.Base64;
19+
import java.util.logging.Level;
20+
21+
public class EnderchestListener implements Listener {
22+
private final BukkitBadlionPlugin apiBukkit;
23+
24+
public EnderchestListener(BukkitBadlionPlugin apiBukkit) {
25+
this.apiBukkit = apiBukkit;
26+
}
27+
28+
@EventHandler
29+
public void onJoin(PlayerJoinEvent event) {
30+
this.sendEnderchest(event.getPlayer());
31+
}
32+
33+
@EventHandler
34+
public void onInventoryClick(InventoryClickEvent event) {
35+
if (event.getView().getTopInventory().getType() == InventoryType.ENDER_CHEST && event.getWhoClicked() instanceof Player) {
36+
this.sendEnderchest((Player) event.getWhoClicked());
37+
}
38+
}
39+
40+
public void sendEnderchest(Player player) {
41+
if (!this.apiBukkit.getBadlionApi().getSurvivalManager().getEnabledFeatures().contains(SurvivalFeature.SHOW_ENDERCHEST_INVENTORY)) {
42+
return;
43+
}
44+
45+
this.apiBukkit.getServer().getScheduler().runTaskLater(this.apiBukkit, () -> {
46+
try {
47+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
48+
49+
Inventory bukkitEnderChest = player.getEnderChest();
50+
Object enderchest = bukkitEnderChest.getClass().getDeclaredMethod("getInventory").invoke(bukkitEnderChest);
51+
Object listTag = enderchest.getClass().getDeclaredMethod("g").invoke(enderchest);
52+
53+
Class<?> compoundTagClass = Class.forName("net.minecraft.nbt.NBTTagCompound");
54+
Object compoundTag = compoundTagClass.getConstructor().newInstance();
55+
56+
compoundTagClass.getDeclaredMethod("a", String.class, Class.forName("net.minecraft.nbt.NBTBase")).invoke(compoundTag, "EnderChest", listTag);
57+
58+
Class.forName("net.minecraft.nbt.NBTCompressedStreamTools").getDeclaredMethod("a", compoundTagClass, OutputStream.class).invoke(null, compoundTag, outputStream);
59+
60+
JsonObject jsonObject = new JsonObject();
61+
62+
jsonObject.add("type", new JsonPrimitive("enderchest"));
63+
jsonObject.add("value", new JsonPrimitive(Base64.getEncoder().encodeToString(outputStream.toByteArray())));
64+
65+
this.apiBukkit.getBadlionApi().getPluginMessageSender().sendModData(player.getUniqueId(), ModType.SURVIVAL, jsonObject);
66+
} catch (Exception ex) {
67+
this.apiBukkit.getLogger().log(Level.SEVERE, "Failed to send enderchest to player " + player.getUniqueId() + ": " + ex.getMessage(), ex);
68+
}
69+
}, 1L);
70+
}
71+
}

bukkit-1.17/src/main/java/net/badlion/bukkitapi/survival/SurvivalManager.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package net.badlion.bukkitapi.survival;
22

33
import com.google.gson.JsonElement;
4+
import com.google.gson.JsonObject;
5+
import com.google.gson.JsonPrimitive;
46
import net.badlion.bukkitapi.BukkitBadlionPlugin;
57
import net.badlion.modapicommon.AbstractBadlionApi;
68
import net.badlion.modapicommon.mods.ModType;
@@ -106,7 +108,11 @@ private void sendData(Player player, JsonElement data) {
106108
}
107109

108110
private JsonElement generateData() {
109-
return AbstractBadlionApi.GSON_NON_PRETTY.toJsonTree(this.config);
111+
JsonObject jsonObject = AbstractBadlionApi.GSON_NON_PRETTY.toJsonTree(this.config).getAsJsonObject();
112+
113+
jsonObject.add("type", new JsonPrimitive("config"));
114+
115+
return jsonObject;
110116
}
111117

112118
private void scheduleUpdate() {

modapi-common/src/main/java/net/badlion/modapicommon/survival/SurvivalFeature.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ public enum SurvivalFeature {
2929
"Show shulker inventory",
3030
"..."
3131
),
32+
SHOW_ENDERCHEST_INVENTORY(
33+
"ENDER_CHEST",
34+
"Show enderchest inventory",
35+
"..."
36+
),
3237
SEARCH_BAR(
3338
"SPYGLASS",
3439
"Inventory search bar",

0 commit comments

Comments
 (0)