Skip to content

Commit

Permalink
anvil/workbench title
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jun 15, 2022
1 parent c7d1610 commit 6d1dfe8
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryView;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.plugin.java.JavaPlugin;

Expand Down Expand Up @@ -134,6 +135,10 @@ public BiomeNMS getBiomeAt(Block block) {

public abstract String getTitle(Inventory inventory);

public void setInventoryTitle(InventoryView view, String title) {
throw new UnsupportedOperationException();
}

public abstract String stringForHover(HoverEvent hover);

public abstract ArrayList<String> containerListFlags(PersistentDataContainer container, String prefix);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.NMSVersion;
import com.denizenscript.denizen.objects.*;
import com.denizenscript.denizen.scripts.containers.core.InventoryScriptContainer;
import com.denizenscript.denizen.scripts.containers.core.InventoryScriptHelper;
import com.denizenscript.denizen.utilities.AdvancedTextImpl;
import com.denizenscript.denizen.utilities.Conversion;
Expand Down Expand Up @@ -264,6 +266,7 @@ else if (!scriptEntry.hasObject("flag_action")
public ObjectTag currentScriptInvHolder;
public Player currentScriptInvPlayer;
public Location currentScriptInvLocation;
public String currentScriptInvTitle;

@EventHandler(priority = EventPriority.LOWEST)
public void onOpen(InventoryOpenEvent event) {
Expand All @@ -276,17 +279,25 @@ public void onOpen(InventoryOpenEvent event) {
if (!event.getPlayer().getUniqueId().equals(currentScriptInvPlayer.getUniqueId())) {
return;
}
if (currentScriptInvTitle != null && NMSHandler.getVersion().isAtLeast(NMSVersion.v1_18)) {
NMSHandler.getInstance().setInventoryTitle(event.getView(), currentScriptInvTitle);
}
InventoryTag newTag = new InventoryTag(event.getInventory(), "script", currentScriptInvHolder);
InventoryTrackerSystem.trackTemporaryInventory(event.getInventory(), newTag);
}

public void doSpecialOpen(InventoryType type, Player player, InventoryTag destination) {
try {
if (destination.getIdType().equals("script")) {
currentScriptInvHolder = destination.getIdHolder();
currentScriptInvPlayer = player;
currentScriptInvLocation = player.getLocation();
currentScriptInvLocation.setY(-1000);
ScriptTag scriptTag = (ScriptTag) destination.getIdHolder();
if (scriptTag != null && scriptTag.getContainer() instanceof InventoryScriptContainer) {
InventoryScriptContainer script = (InventoryScriptContainer) scriptTag.getContainer();
currentScriptInvTitle = script.getString("title", null);
currentScriptInvHolder = destination.getIdHolder();
currentScriptInvPlayer = player;
currentScriptInvLocation = player.getLocation();
currentScriptInvLocation.setY(-1000);
}
}
InventoryView view;
if (type == InventoryType.ANVIL) {
Expand All @@ -305,6 +316,7 @@ else if (type == InventoryType.WORKBENCH) {
currentScriptInvHolder = null;
currentScriptInvPlayer = null;
currentScriptInvLocation = null;
currentScriptInvTitle = null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import net.minecraft.world.Container;
import net.minecraft.world.Nameable;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.biome.Biome;
import org.bukkit.Bukkit;
Expand All @@ -54,12 +55,14 @@
import org.bukkit.craftbukkit.v1_18_R2.entity.CraftPlayer;
import org.bukkit.craftbukkit.v1_18_R2.inventory.CraftInventory;
import org.bukkit.craftbukkit.v1_18_R2.inventory.CraftInventoryCustom;
import org.bukkit.craftbukkit.v1_18_R2.inventory.CraftInventoryView;
import org.bukkit.craftbukkit.v1_18_R2.inventory.CraftItemStack;
import org.bukkit.craftbukkit.v1_18_R2.persistence.CraftPersistentDataContainer;
import org.bukkit.craftbukkit.v1_18_R2.util.CraftChatMessage;
import org.bukkit.craftbukkit.v1_18_R2.util.CraftMagicNumbers;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryView;
import org.bukkit.persistence.PersistentDataContainer;
import org.spigotmc.AsyncCatcher;

Expand Down Expand Up @@ -202,6 +205,19 @@ else if (MINECRAFT_INVENTORY.isInstance(nms)) {
return "Chest";
}

public static MethodHandle AbstractContainerMenu_title_SETTER = ReflectionHelper.getFinalSetter(AbstractContainerMenu.class, "title");

@Override
public void setInventoryTitle(InventoryView view, String title) {
AbstractContainerMenu menu = ((CraftInventoryView) view).getHandle();
try {
AbstractContainerMenu_title_SETTER.invoke(menu, componentToNMS(FormattedTextHelper.parse(title, ChatColor.DARK_GRAY)));
}
catch (Throwable ex) {
Debug.echoError(ex);
}
}

public static final Class MINECRAFT_INVENTORY;
public static final Field INVENTORY_TITLE;
public static final Field ENTITY_BUKKITYENTITY = ReflectionHelper.getFields(Entity.class).get("bukkitEntity");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import net.minecraft.world.Container;
import net.minecraft.world.Nameable;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.biome.Biome;
import org.bukkit.Bukkit;
Expand All @@ -54,12 +55,14 @@
import org.bukkit.craftbukkit.v1_19_R1.entity.CraftPlayer;
import org.bukkit.craftbukkit.v1_19_R1.inventory.CraftInventory;
import org.bukkit.craftbukkit.v1_19_R1.inventory.CraftInventoryCustom;
import org.bukkit.craftbukkit.v1_19_R1.inventory.CraftInventoryView;
import org.bukkit.craftbukkit.v1_19_R1.inventory.CraftItemStack;
import org.bukkit.craftbukkit.v1_19_R1.persistence.CraftPersistentDataContainer;
import org.bukkit.craftbukkit.v1_19_R1.util.CraftChatMessage;
import org.bukkit.craftbukkit.v1_19_R1.util.CraftMagicNumbers;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryView;
import org.bukkit.persistence.PersistentDataContainer;
import org.spigotmc.AsyncCatcher;

Expand Down Expand Up @@ -202,6 +205,19 @@ else if (MINECRAFT_INVENTORY.isInstance(nms)) {
return "Chest";
}

public static MethodHandle AbstractContainerMenu_title_SETTER = ReflectionHelper.getFinalSetter(AbstractContainerMenu.class, "title");

@Override
public void setInventoryTitle(InventoryView view, String title) {
AbstractContainerMenu menu = ((CraftInventoryView) view).getHandle();
try {
AbstractContainerMenu_title_SETTER.invoke(menu, componentToNMS(FormattedTextHelper.parse(title, ChatColor.DARK_GRAY)));
}
catch (Throwable ex) {
Debug.echoError(ex);
}
}

public static final Class MINECRAFT_INVENTORY;
public static final Field INVENTORY_TITLE;
public static final Field ENTITY_BUKKITYENTITY = ReflectionHelper.getFields(Entity.class).get("bukkitEntity");
Expand Down

0 comments on commit 6d1dfe8

Please sign in to comment.