Added the ability to use whole items as display items.

This commit is contained in:
Brianna 2021-03-10 10:23:48 -06:00
parent 1032cde38e
commit 2bb9b64090
13 changed files with 89 additions and 84 deletions

View File

@ -46,6 +46,7 @@ import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.PluginManager;
import java.util.ArrayList;
@ -75,8 +76,6 @@ public class UltimateKits extends SongodaPlugin {
private CrateManager crateManager;
private CategoryManager categoryManager;
private ItemSerializer itemSerializer;
private DatabaseConnector databaseConnector;
private DataMigrationManager dataMigrationManager;
private DataManager dataManager;
@ -95,12 +94,6 @@ public class UltimateKits extends SongodaPlugin {
@Override
public void onPluginLoad() {
INSTANCE = this;
try {
this.itemSerializer = new ItemSerializer();
} catch (NoSuchMethodException | SecurityException | ClassNotFoundException e) {
console.sendMessage(ChatColor.RED + "Could not load the serialization class! Please report this error.");
e.printStackTrace();
}
}
@Override
@ -227,13 +220,22 @@ public class UltimateKits extends SongodaPlugin {
ConfigurationSection section = kitConfig.getConfigurationSection("Kits." + kitName);
if (section == null) continue;
String itemString = section.getString("displayItem");
ItemStack item = null;
if (itemString != null) {
if (itemString.contains("{"))
item = ItemSerializer.deserializeItemStackFromJson(itemString);
else
item = CompatibleMaterial.getMaterial(itemString).getItem();
}
kitManager.addKit(new Kit(kitName)
.setTitle(section.getString("title"))
.setDelay(section.getLong("delay"))
.setLink(section.getString("link"))
.setDisplayItem(section.contains("displayItem")
? CompatibleMaterial.getMaterial(section.getString("displayItem"), CompatibleMaterial.DIAMOND_HELMET)
: null)
.setDisplayItem(item)
.setCategory(categoryManager.getCategory(section.getString("category")))
.setHidden(section.getBoolean("hidden"))
.setPrice(section.getDouble("price"))
@ -314,7 +316,7 @@ public class UltimateKits extends SongodaPlugin {
public void onPluginDisable() {
saveKits(false);
dataFile.save();
this.dataManager.bulkUpdateBlockData(this.getKitManager().getKitLocations());
dataManager.bulkUpdateBlockData(this.getKitManager().getKitLocations());
kitManager.clearKits();
HologramManager.removeAllHolograms();
}
@ -326,9 +328,9 @@ public class UltimateKits extends SongodaPlugin {
@Override
public void onConfigReload() {
this.setLocale(Settings.LANGUGE_MODE.getString(), true);
setLocale(Settings.LANGUGE_MODE.getString(), true);
this.dataManager.bulkUpdateBlockData(this.getKitManager().getKitLocations());
dataManager.bulkUpdateBlockData(this.getKitManager().getKitLocations());
kitConfig.load();
categoryConfig.load();
keyFile.load();
@ -488,7 +490,7 @@ public class UltimateKits extends SongodaPlugin {
if (kit.getCategory() != null)
kitConfig.set("Kits." + kit.getKey() + ".category", kit.getCategory().getKey());
if (kit.getDisplayItem() != null)
kitConfig.set("Kits." + kit.getKey() + ".displayItem", kit.getDisplayItem().toString());
kitConfig.set("Kits." + kit.getKey() + ".displayItem", ItemSerializer.serializeItemStackToJson(kit.getDisplayItem()));
else
kitConfig.set("Kits." + kit.getKey() + ".displayItem", null);
@ -587,15 +589,6 @@ public class UltimateKits extends SongodaPlugin {
return guiManager;
}
/**
* Grab instance of the item serializer
*
* @return instance of ItemSerializer
*/
public ItemSerializer getItemSerializer() {
return this.itemSerializer;
}
public DisplayItemHandler getDisplayItemHandler() {
return displayItemHandler;
}

View File

@ -3,6 +3,7 @@ package com.songoda.ultimatekits.conversion.hooks;
import com.songoda.core.compatibility.ServerVersion;
import com.songoda.ultimatekits.UltimateKits;
import com.songoda.ultimatekits.conversion.Hook;
import com.songoda.ultimatekits.utils.ItemSerializer;
import org.bukkit.inventory.ItemStack;
import java.util.HashSet;
@ -16,7 +17,7 @@ public class DefaultHook implements Hook {
for (Kits kit : Kits.values()) {
if (!kit.name().equalsIgnoreCase(kitName)) continue;
for (String string : kit.items) {
items.add(UltimateKits.getInstance().getItemSerializer().deserializeItemStackFromJson(string));
items.add(ItemSerializer.deserializeItemStackFromJson(string));
}
}

View File

@ -29,7 +29,7 @@ public class ConfirmBuyGui extends Gui {
Methods.fillGlass(this);
// Kit information
setItem(0, 4, GuiUtils.createButtonItem(kit.getDisplayItem() != null ? kit.getDisplayItem() : CompatibleMaterial.DIAMOND_HELMET,
setItem(0, 4, GuiUtils.createButtonItem(kit.getDisplayItem() != null ? kit.getDisplayItem() : CompatibleMaterial.DIAMOND_HELMET.getItem(),
ChatColor.RED + TextUtils.formatText(kit.getKey().toLowerCase(), true),
ChatColor.GREEN + Settings.CURRENCY_SYMBOL.getString() + Methods.formatEconomy(cost)));

View File

@ -71,7 +71,7 @@ public class KitDecorOptionsGui extends Gui {
});
// Item Display Override
setButton(1, 7, GuiUtils.createButtonItem(kit.getDisplayItem() != null ? kit.getDisplayItem() : CompatibleMaterial.BEACON,
setButton(1, 7, GuiUtils.createButtonItem(kit.getDisplayItem() != null ? kit.getDisplayItem() : CompatibleMaterial.BEACON.getItem(),
plugin.getLocale().getMessage("interface.kitdecor.displayone").getMessage(),
plugin.getLocale().getMessage("interface.kitdecor.displayonelore")
.processPlaceholder("enabled", kitBlockData.isItemOverride() ? enableLore : disableLore)

View File

@ -1,6 +1,5 @@
package com.songoda.ultimatekits.gui;
import com.songoda.core.commands.AbstractCommand;
import com.songoda.core.compatibility.CompatibleMaterial;
import com.songoda.core.compatibility.CompatibleSound;
import com.songoda.core.gui.AnvilGui;
@ -54,7 +53,7 @@ public class KitEditorGui extends DoubleGui {
setOnClose((event) -> {
restoreItemsInstance();
this.saveKit(player, inventory, false);
saveKit(player, inventory, false);
CompatibleSound.ENTITY_VILLAGER_YES.play(player);
});
@ -262,7 +261,7 @@ public class KitEditorGui extends DoubleGui {
.processPlaceholder("command", msg).getMessage())
.sendPrefixedMessage(player);
this.inventory.addItem(parseStack);
inventory.addItem(parseStack);
Bukkit.getScheduler().runTask(plugin, event.player::closeInventory);
}).setOnClose(() -> {
event.manager.showGUI(event.player, this);

View File

@ -55,6 +55,7 @@ public class KitGeneralOptionsGui extends Gui {
} catch (NumberFormatException e) {
}
plugin.getLocale().getMessage("interface.kitoptions.delaynonumber").processPlaceholder("input", msg).sendPrefixedMessage(player);
plugin.saveKits(false);
});
guiManager.showGUI(event.player, gui);
});
@ -79,6 +80,7 @@ public class KitGeneralOptionsGui extends Gui {
return;
}
plugin.getLocale().getMessage("interface.kitoptions.notacategory").processPlaceholder("input", msg).sendPrefixedMessage(player);
plugin.saveKits(false);
});
guiManager.showGUI(event.player, gui);
} else if (event.clickType == ClickType.RIGHT) {
@ -105,6 +107,7 @@ public class KitGeneralOptionsGui extends Gui {
plugin.getLocale().getMessage("interface.kitoptions.destroycancel").sendPrefixedMessage(player);
}
aevent.player.closeInventory();
plugin.saveKits(false);
});
guiManager.showGUI(event.player, gui);
});

View File

@ -82,11 +82,12 @@ public class KitGuiOptionsGui extends Gui {
plugin.saveKits(false);
});
setButton(1, 4, GuiUtils.createButtonItem(kit.getDisplayItem() != null ? kit.getDisplayItem() : CompatibleMaterial.BEACON,
setButton(1, 4, GuiUtils.createButtonItem(kit.getDisplayItem() != null ? kit.getDisplayItem() : CompatibleMaterial.BEACON.getItem(),
plugin.getLocale().getMessage("interface.kitguioptions.item").getMessage(),
plugin.getLocale().getMessage("interface.kitguioptions.itemlore")
.processPlaceholder("onoff",
kit.getDisplayItem() != null ? plugin.getLocale().getMessage("interface.kitguioptions.itemon").processPlaceholder("item", kit.getDisplayItem().toString()).getMessage()
kit.getDisplayItem() != null ? plugin.getLocale().getMessage("interface.kitguioptions.itemon")
.processPlaceholder("item", kit.getDisplayItem().toString()).getMessage()
: plugin.getLocale().getMessage("interface.kitguioptions.itemoff").getMessage()
).getMessage().split("\\|")),
ClickType.LEFT,

View File

@ -144,7 +144,7 @@ public class KitSelectorGui extends Gui {
.processPlaceholder("kit", TextUtils.formatText(kitItem, true)).getMessage();
setButton(row, col, GuiUtils.createButtonItem(
kit.getDisplayItem() != null ? kit.getDisplayItem() : CompatibleMaterial.ENCHANTED_BOOK, kitTitle,
kit.getDisplayItem() != null ? kit.getDisplayItem() : CompatibleMaterial.ENCHANTED_BOOK.getItem(), kitTitle,
getKitLore(kit)),
event -> {
if (event.clickType == ClickType.MIDDLE && player.hasPermission("ultimatekits.admin")) {

View File

@ -74,7 +74,7 @@ public class DisplayItemHandler {
ItemStack is = list.get(inum - 1);
if (kitBlockData.isItemOverride()) {
if (kit.getDisplayItem() != null)
is = kit.getDisplayItem().getItem();
is = kit.getDisplayItem();
}
is.setAmount(1);
ItemMeta meta = is.getItemMeta();

View File

@ -42,7 +42,7 @@ import java.util.Objects;
public class Kit implements Cloneable {
private String key;
private String name;
private String name;
private Category category = null;
private static UltimateKits plugin;
@ -50,7 +50,7 @@ public class Kit implements Cloneable {
private String link, title = null;
private long delay = 0L;
private boolean hidden = false;
private CompatibleMaterial displayItem = null;
private ItemStack displayItem = null;
private List<KitItem> contents = new ArrayList<>();
private KitAnimation kitAnimation = KitAnimation.NONE;
@ -281,7 +281,7 @@ public class Kit implements Cloneable {
}
public boolean giveKit(Player player) {
return giveKit(player, getContents().size(), -1);
return giveKit(player, contents.size(), -1);
}
private boolean giveKit(Player player, Key key) {
@ -294,13 +294,13 @@ public class Kit implements Cloneable {
private boolean giveKit(Player player, int itemAmount, int kitAmount) {
List<KitItem> innerContents = new ArrayList<>(getContents());
int kitSize = innerContents.size();
// Amount of items from the kit to give to the player.
if (kitAnimation == KitAnimation.ROULETTE)
itemAmount = 1; //TODO how about kitAmount > 1? generateRandomItem() will only give 1 random item instead of kitAmount
int itemGiveAmount = itemAmount > 0 ? itemAmount : kitSize;
if (kitAmount > 0) itemGiveAmount = itemGiveAmount * kitAmount;
System.out.println("itemAmount" + ": " + itemAmount);
int itemGiveAmount = kitAmount > 0 ? itemAmount * kitAmount : kitAmount;
System.out.println(itemGiveAmount + " : " + kitAmount + " : " + itemAmount);
if (Settings.NO_REDEEM_WHEN_FULL.getBoolean() && !hasRoom(player, itemGiveAmount)) {
plugin.getLocale().getMessage("event.claim.full").sendPrefixedMessage(player);
@ -310,20 +310,21 @@ public class Kit implements Cloneable {
if (Settings.SOUNDS_ENABLED.getBoolean() && kitAnimation == KitAnimation.NONE)
CompatibleSound.ENTITY_PLAYER_LEVELUP.play(player, 0.6F, 15.0F);
return generateRandomItem(innerContents, itemGiveAmount, player);
return generateRandomItem(innerContents, itemGiveAmount, 0, player);
}
private boolean generateRandomItem(List<KitItem> innerContents, int itemGiveAmount, Player player) {
private boolean generateRandomItem(List<KitItem> innerContents, int itemGiveAmount, int itemGivenAmount, Player player) {
if (innerContents.size() != itemGiveAmount || kitAnimation != KitAnimation.NONE)
Collections.shuffle(innerContents);
for (KitItem item : new ArrayList<>(innerContents)) {
if (itemGiveAmount == 0) break;
if (itemGiveAmount <= 0 && itemGivenAmount != 0) break;
double ch = item.getChance() == 0 ? 100 : item.getChance();
double rand = Math.random() * 100;
System.out.println("We tryin here [" + ch + ":" + rand + "]");
itemGiveAmount--;
if (rand < ch || ch == 100) {
itemGivenAmount++;
if (kitAnimation != KitAnimation.NONE) {
// TODO: this is a very bad way to solve this problem.
// Giving the player kit rewards really should be done outside of the Kit class.
@ -347,9 +348,8 @@ public class Kit implements Cloneable {
}
}
if (itemGiveAmount > 0 && !innerContents.isEmpty()) {
return generateRandomItem(innerContents, itemGiveAmount, player);
}
if ((itemGiveAmount > 0 || itemGivenAmount == 0) && !innerContents.isEmpty())
return generateRandomItem(innerContents, itemGiveAmount, itemGivenAmount, player);
player.updateInventory();
return true;
@ -447,16 +447,12 @@ public class Kit implements Cloneable {
return name;
}
public CompatibleMaterial getDisplayItem() {
public ItemStack getDisplayItem() {
return displayItem;
}
public void setDisplayItem(ItemStack item) {
this.displayItem = item != null ? CompatibleMaterial.getMaterial(item) : null;
}
public Kit setDisplayItem(CompatibleMaterial material) {
this.displayItem = material;
public Kit setDisplayItem(ItemStack item) {
this.displayItem = item;
return this;
}

View File

@ -9,6 +9,7 @@ import com.songoda.ultimatekits.kit.type.KitContentCommand;
import com.songoda.ultimatekits.kit.type.KitContentEconomy;
import com.songoda.ultimatekits.kit.type.KitContentItem;
import com.songoda.ultimatekits.settings.Settings;
import com.songoda.ultimatekits.utils.ItemSerializer;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
@ -52,7 +53,7 @@ public class KitItem implements Cloneable {
this.content = new KitContentCommand(line.substring(1));
this.type = KitItemType.COMMAND;
} else {
ItemStack itemStack = item == null ? UltimateKits.getInstance().getItemSerializer().deserializeItemStackFromJson(line) : item;
ItemStack itemStack = item == null ? ItemSerializer.deserializeItemStackFromJson(line) : item;
this.content = itemStack != null ? new KitContentItem(itemStack) : null;
this.type = KitItemType.ITEM;
}

View File

@ -1,6 +1,7 @@
package com.songoda.ultimatekits.kit.type;
import com.songoda.ultimatekits.UltimateKits;
import com.songoda.ultimatekits.utils.ItemSerializer;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
@ -25,7 +26,7 @@ public class KitContentItem implements KitContent {
@Override
public String getSerialized() {
if (serialized != null) return serialized;
serialized = UltimateKits.getInstance().getItemSerializer().serializeItemStackToJson(itemStack);
serialized = ItemSerializer.serializeItemStackToJson(itemStack);
return serialized;
}

View File

@ -11,23 +11,23 @@ public class ItemSerializer {
// classes needed for reflections
private Class<?> classMojangsonParser = Class.forName(formatNMS("net.minecraft.server.NMS.MojangsonParser"));
private Class<?> classItemStack = Class.forName(formatNMS("net.minecraft.server.NMS.ItemStack"));
private Class<?> classCraftItemStack = Class.forName(formatNMS("org.bukkit.craftbukkit.NMS.inventory.CraftItemStack"));
private Class<?> classNBTTagCompound = Class.forName(formatNMS("net.minecraft.server.NMS.NBTTagCompound"));
private Class<?> classBukkitItemStack = Class.forName("org.bukkit.inventory.ItemStack");
private static Class<?> classMojangsonParser;
private static Class<?> classItemStack;
private static Class<?> classCraftItemStack;
private static Class<?> classNBTTagCompound;
private static Class<?> classBukkitItemStack;
private Constructor<?> constructorItemStack;
private static Constructor<?> constructorItemStack;
// reflected methods
private Method methodParseString;
private Method methodCreateStack;
private Method methodToItemStack;
private Method methodTobItemStack;
private Method methodTocItemStack;
private Method methodSaveTagToStack;
private Method methodToString;
private static Method methodParseString;
private static Method methodCreateStack;
private static Method methodToItemStack;
private static Method methodTobItemStack;
private static Method methodTocItemStack;
private static Method methodSaveTagToStack;
private static Method methodToString;
/**
* Initializes all reflection methods
@ -36,19 +36,29 @@ public class ItemSerializer {
* @throws SecurityException
* @throws ClassNotFoundException
*/
public ItemSerializer() throws NoSuchMethodException, SecurityException, ClassNotFoundException {
methodParseString = classMojangsonParser.getMethod("parse", String.class);
if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_13))
methodToItemStack = classItemStack.getMethod("a", classNBTTagCompound);
else if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_11))
constructorItemStack = classItemStack.getConstructor(classNBTTagCompound);
else
methodCreateStack = classItemStack.getMethod("createStack", classNBTTagCompound);
methodTobItemStack = classCraftItemStack.getMethod("asBukkitCopy", classItemStack);
static {
try {
classMojangsonParser = Class.forName(formatNMS("net.minecraft.server.NMS.MojangsonParser"));
classItemStack = Class.forName(formatNMS("net.minecraft.server.NMS.ItemStack"));
classCraftItemStack = Class.forName(formatNMS("org.bukkit.craftbukkit.NMS.inventory.CraftItemStack"));
classNBTTagCompound = Class.forName(formatNMS("net.minecraft.server.NMS.NBTTagCompound"));
classBukkitItemStack = Class.forName("org.bukkit.inventory.ItemStack");
methodParseString = classMojangsonParser.getMethod("parse", String.class);
methodTocItemStack = classCraftItemStack.getDeclaredMethod("asNMSCopy", classBukkitItemStack);
methodSaveTagToStack = classItemStack.getMethod("save", classNBTTagCompound);
methodToString = classNBTTagCompound.getMethod("toString");
if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_13))
methodToItemStack = classItemStack.getMethod("a", classNBTTagCompound);
else if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_11))
constructorItemStack = classItemStack.getConstructor(classNBTTagCompound);
else
methodCreateStack = classItemStack.getMethod("createStack", classNBTTagCompound);
methodTobItemStack = classCraftItemStack.getMethod("asBukkitCopy", classItemStack);
methodTocItemStack = classCraftItemStack.getDeclaredMethod("asNMSCopy", classBukkitItemStack);
methodSaveTagToStack = classItemStack.getMethod("save", classNBTTagCompound);
methodToString = classNBTTagCompound.getMethod("toString");
} catch (NoSuchMethodException | ClassNotFoundException e) {
e.getStackTrace();
}
}
/**
@ -57,7 +67,7 @@ public class ItemSerializer {
* @param s the string to format, must contain NMS.
* @return formatted string
*/
private String formatNMS(String s) {
private static String formatNMS(String s) {
String packageName = Bukkit.getServer().getClass().getPackage().getName();
String nmsVersion = packageName.substring(packageName.lastIndexOf('.') + 1);
return s.replace("NMS", nmsVersion);
@ -69,7 +79,7 @@ public class ItemSerializer {
* @param jsonString the JSON String to parse
* @return the deserialized ItemStack
*/
public ItemStack deserializeItemStackFromJson(String jsonString) {
public static ItemStack deserializeItemStackFromJson(String jsonString) {
try {
Object nbtTagCompound = methodParseString.invoke(null, jsonString);
Object citemStack;
@ -94,7 +104,7 @@ public class ItemSerializer {
* @param itemStack the ItemStack to parse
* @return condensed JSON String
*/
public String serializeItemStackToJson(ItemStack itemStack) {
public static String serializeItemStackToJson(ItemStack itemStack) {
try {
Object citemStack = methodTocItemStack.invoke(null, itemStack);
Object nbtTagCompoundObject = classNBTTagCompound.newInstance();