add custom anvil gui text prompt

This commit is contained in:
jascotty2 2019-09-15 13:33:24 -05:00
parent 07348bf5b8
commit 252c17bf60
5 changed files with 147 additions and 20 deletions

View File

@ -97,6 +97,7 @@
<version>1.14.4</version>
</dependency-->
<!-- Need to include all NMS modules here -->
<!-- Note when adding a new module: include the class in NmsManager -->
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>SongodaCore-NMS-API</artifactId>

View File

@ -0,0 +1,87 @@
package com.songoda.core.gui;
import com.songoda.core.compatibility.CompatibleMaterial;
import com.songoda.core.nms.CoreNMS;
import com.songoda.core.nms.CustomAnvil;
import com.songoda.core.nms.NmsManager;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
/**
* Anvil GUI for text prompts
*
* @since 2019-09-15
* @author jascotty2
*/
public class AnvilGui extends Gui {
final Player player;
CustomAnvil anvil;
public AnvilGui(Player player) {
this.player = player;
}
public AnvilGui(Player player, Gui parent) {
super(parent);
this.player = player;
}
public void open() {
anvil.open();
}
public AnvilGui setInput(ItemStack item) {
return (AnvilGui) this.setItem(0, item);
}
public ItemStack getInput() {
return this.getItem(0);
}
public AnvilGui setOutput(ItemStack item) {
return (AnvilGui) this.setItem(2, item);
}
public ItemStack getOutput() {
return this.getItem(2);
}
public String getInputText() {
return anvil != null ? anvil.getRenameText() : null;
}
@NotNull
@Override
protected Inventory generateInventory(@NotNull GuiManager manager) {
this.guiManager = manager;
createInventory();
ItemStack item;
if ((item = cellItems.get(0)) != null) {
inventory.setItem(0, item);
} else if ((item = cellItems.get(1)) != null) {
inventory.setItem(1, item);
} else if (!acceptsItems) {
inventory.setItem(0, GuiUtils.createButtonItem(CompatibleMaterial.PAPER, " ", " "));
}
if ((item = cellItems.get(2)) != null) {
inventory.setItem(2, item);
}
return inventory;
}
@Override
protected void createInventory() {
CoreNMS nms = NmsManager.getNMS();
if (nms != null) {
anvil = nms.createAnvil(player, new GuiHolder(guiManager, this));
anvil.setCustomTitle(title);
anvil.setLevelCost(0);
inventory = anvil.getInventory();
}
}
}

View File

@ -78,20 +78,34 @@ public class GuiManager {
} else if (!initialized) {
init();
}
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
if (gui instanceof AnvilGui) {
// bukkit throws a fit now if you try to set anvil stuff asyncronously
Gui openInv = openInventories.get(player);
if (openInv != null) {
openInv.open = false;
}
Inventory inv = gui.getOrCreateInventory(this);
Bukkit.getScheduler().runTask(plugin, () -> {
player.openInventory(inv);
gui.onOpen(this, player);
synchronized(lock) {
openInventories.put(player, gui);
gui.getOrCreateInventory(this);
((AnvilGui) gui).open();
gui.onOpen(this, player);
synchronized (lock) {
openInventories.put(player, gui);
}
} else {
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
Gui openInv = openInventories.get(player);
if (openInv != null) {
openInv.open = false;
}
Inventory inv = gui.getOrCreateInventory(this);
Bukkit.getScheduler().runTask(plugin, () -> {
player.openInventory(inv);
gui.onOpen(this, player);
synchronized(lock) {
openInventories.put(player, gui);
}
});
});
});
}
}
public void showPopup(Player player, String message) {
@ -195,7 +209,11 @@ public class GuiManager {
if (openInv.getHolder() != null && openInv.getHolder() instanceof GuiHolder
&& ((GuiHolder) openInv.getHolder()).manager.uuid.equals(manager.uuid)) {
Gui gui = ((GuiHolder) openInv.getHolder()).getGUI();
if(!gui.open) {
if (gui instanceof AnvilGui) {
gui.inventory.clear();
gui.inventory = null;
}
if (!gui.open) {
return;
}
final Player player = (Player) event.getPlayer();

View File

@ -31,9 +31,8 @@ class PopupMessage {
private final NamespacedKey key;
private final TextComponent title;
CompatibleMaterial icon;
int iconAmount = 1; // experimental, untested
TriggerType trigger = TriggerType.IMPOSSIBLE;
FrameType frame = FrameType.GOAL;
FrameType frame = FrameType.GOAL; // TASK is the default
BackgroundType background = BackgroundType.ADVENTURE;
PopupMessage(Plugin source, CompatibleMaterial icon, String title) {
@ -58,9 +57,6 @@ class PopupMessage {
if (this.icon.usesData()) {
displayIcon.addProperty("data", this.icon.getData());
}
if (this.iconAmount > 1) {
displayIcon.addProperty("amount", this.iconAmount); // not entirely sure if this works
}
advDisplay.add("icon", displayIcon);
}
advDisplay.add("title", gson.fromJson(ComponentSerializer.toString(this.title), JsonElement.class));

View File

@ -11,13 +11,38 @@ public class NmsManager {
private final static CoreNMS nms = _getNMS();
private static CoreNMS _getNMS() {
CoreNMS result = null;
try {
result = (CoreNMS) Class.forName("com.songoda.core.nms." + serverPackageVersion + ".NMS").newInstance();
} catch (Exception ex) {
Logger.getLogger(NmsManager.class.getName()).log(Level.SEVERE, "Failed to load NMS for this server version", ex);
// try {
// return (CoreNMS) Class.forName("com.songoda.core.nms." + serverPackageVersion + ".NMS").newInstance();
// } catch (Exception ex) {
// Logger.getLogger(NmsManager.class.getName()).log(Level.SEVERE, "Failed to load NMS for this server version", ex);
// }
// this block was only added to keep minimizeJar happy
switch (serverPackageVersion) {
case "v1_8_R1":
return new com.songoda.core.nms.v1_8_R1.NMS();
case "v1_8_R2":
return new com.songoda.core.nms.v1_8_R2.NMS();
case "v1_8_R3":
return new com.songoda.core.nms.v1_8_R3.NMS();
case "v1_9_R1":
return new com.songoda.core.nms.v1_9_R1.NMS();
case "v1_9_R2":
return new com.songoda.core.nms.v1_9_R2.NMS();
case "v1_10_R1":
return new com.songoda.core.nms.v1_10_R1.NMS();
case "v1_11_R1":
return new com.songoda.core.nms.v1_11_R1.NMS();
case "v1_12_R1":
return new com.songoda.core.nms.v1_12_R1.NMS();
case "v1_13_R1":
return new com.songoda.core.nms.v1_13_R1.NMS();
case "v1_13_R2":
return new com.songoda.core.nms.v1_13_R2.NMS();
case "v1_14_R1":
return new com.songoda.core.nms.v1_14_R1.NMS();
}
return result;
Logger.getLogger(NmsManager.class.getName()).log(Level.SEVERE, "Failed to load NMS for this server version: version {0} not found", serverPackageVersion);
return null;
}
public static CoreNMS getNMS() {