Remove NMS depend

This commit is contained in:
Ghost_chu committed 2018-07-30 17:09:49 +08:00
1 parent 2ab858b0c4
commit 96073a1ad4
8 files changed
+857 -859

No files matched your search

@@ -55,9 +55,9 @@ import org.maxgamer.quickshop.Shop.Shop;
import org.maxgamer.quickshop.Shop.ShopManager;
import org.maxgamer.quickshop.Shop.ShopType;
import org.maxgamer.quickshop.Util.CustomItemName;
import org.maxgamer.quickshop.Util.CustomPotionsName;
//import org.maxgamer.quickshop.Util.CustomPotionsName;
import org.maxgamer.quickshop.Util.MsgUtil;
import org.maxgamer.quickshop.Util.NMS;
//import org.maxgamer.quickshop.Util.NMS;
import org.maxgamer.quickshop.Util.Permissions;
import org.maxgamer.quickshop.Util.Util;
import org.maxgamer.quickshop.Watcher.ItemWatcher;
@@ -136,7 +136,7 @@ public class QuickShop extends JavaPlugin {
getLogger().info("Author:Ghost_chu");
getLogger().info("Original author:Netherfoam, Timtower, KaiNoMood");
getLogger().info("Let's us start load plugin");
NMS.init();
//NMS.init();
instance = this;
saveDefaultConfig(); // Creates the config folder and copies config.yml
@@ -218,52 +218,52 @@ public class QuickShop extends JavaPlugin {
}
}
ConfigurationSection cPotionSection = this.getConfig().getConfigurationSection("custom-potions-name");
if (cPotionSection!=null) {
CustomPotionsName.setSignFormat(new String[]{cPotionSection.getString("sign.format"), cPotionSection.getString("sign.variety.normal"), cPotionSection.getString("sign.variety.splash"), cPotionSection.getString("sign.variety.lingering")});
CustomPotionsName.setShopInfoFormat(new String[]{cPotionSection.getString("shopinfo.format"), cPotionSection.getString("shopinfo.variety.normal"), cPotionSection.getString("shopinfo.variety.splash"), cPotionSection.getString("shopinfo.variety.lingering")});
Map<PotionType,CustomPotionsName.Names> potionTypes = new HashMap<PotionType,CustomPotionsName.Names>();
for (String s : cPotionSection.getStringList("types")) {
try {
String[] a = s.split("[;]");
if (a.length!=3) {
throw new Exception("Invalid format (main args length must be 3)");
}
PotionType type = PotionType.valueOf(a[0].toUpperCase());
if (type==null) {
throw new Exception("Invalid PotionType "+a[0]);
}
potionTypes.put(type, new CustomPotionsName.Names(a[1], a[2]));
} catch (Exception e) {
Bukkit.getLogger().warning("Invalid potion item name definition {"+s+"} Error: "+e.getMessage());
}
}
CustomPotionsName.setPotionTypes(potionTypes);
Map<PotionEffectType,String> potionEffects = new HashMap<PotionEffectType,String>();
for (String s : cPotionSection.getStringList("effects")) {
try {
String[] a = s.split("[;]");
if (a.length!=2) {
throw new Exception("Invalid format (main args length must be 2)");
}
PotionEffectType type = PotionEffectType.getByName(a[0]);
if (type==null) {
throw new Exception("Invalid PotionEffectType "+a[0]);
}
potionEffects.put(type, a[1]);
} catch (Exception e) {
Bukkit.getLogger().warning("Invalid potion effect type name definition {"+s+"} Error: "+e.getMessage());
}
}
CustomPotionsName.setPotionEffects(potionEffects);
}
// ConfigurationSection cPotionSection = this.getConfig().getConfigurationSection("custom-potions-name");
// if (cPotionSection!=null) {
// CustomPotionsName.setSignFormat(new String[]{cPotionSection.getString("sign.format"), cPotionSection.getString("sign.variety.normal"), cPotionSection.getString("sign.variety.splash"), cPotionSection.getString("sign.variety.lingering")});
// CustomPotionsName.setShopInfoFormat(new String[]{cPotionSection.getString("shopinfo.format"), cPotionSection.getString("shopinfo.variety.normal"), cPotionSection.getString("shopinfo.variety.splash"), cPotionSection.getString("shopinfo.variety.lingering")});
//
// Map<PotionType,CustomPotionsName.Names> potionTypes = new HashMap<PotionType,CustomPotionsName.Names>();
// for (String s : cPotionSection.getStringList("types")) {
// try {
// String[] a = s.split("[;]");
// if (a.length!=3) {
// throw new Exception("Invalid format (main args length must be 3)");
// }
//
// PotionType type = PotionType.valueOf(a[0].toUpperCase());
// if (type==null) {
// throw new Exception("Invalid PotionType "+a[0]);
// }
//
// potionTypes.put(type, new CustomPotionsName.Names(a[1], a[2]));
// } catch (Exception e) {
// Bukkit.getLogger().warning("Invalid potion item name definition {"+s+"} Error: "+e.getMessage());
// }
// }
// CustomPotionsName.setPotionTypes(potionTypes);
//
// Map<PotionEffectType,String> potionEffects = new HashMap<PotionEffectType,String>();
// for (String s : cPotionSection.getStringList("effects")) {
// try {
// String[] a = s.split("[;]");
// if (a.length!=2) {
// throw new Exception("Invalid format (main args length must be 2)");
// }
//
// PotionEffectType type = PotionEffectType.getByName(a[0]);
// if (type==null) {
// throw new Exception("Invalid PotionEffectType "+a[0]);
// }
//
// potionEffects.put(type, a[1]);
// } catch (Exception e) {
// Bukkit.getLogger().warning("Invalid potion effect type name definition {"+s+"} Error: "+e.getMessage());
// }
// }
// CustomPotionsName.setPotionEffects(potionEffects);
// }
//
ConfigurationSection limitCfg = this.getConfig().getConfigurationSection("limits");
if (limitCfg != null) {
getLogger().info("Limit cfg found...");
@@ -1,5 +1,6 @@
package org.maxgamer.quickshop.Shop;
import org.bukkit.ChatColor;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
@@ -7,7 +8,8 @@ import org.bukkit.entity.Item;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.Vector;
import org.maxgamer.quickshop.QuickShop;
import org.maxgamer.quickshop.Util.NMS;
//import org.maxgamer.quickshop.Util.NMS;
import org.maxgamer.quickshop.Util.Util;
/**
* @author Netherfoam A display item, that spawns a block above the chest and
@@ -17,6 +19,7 @@ public class DisplayItem {
private Shop shop;
private ItemStack iStack;
private Item item;
private static int nextId = 0;
// private Location displayLoc;
/**
@@ -46,7 +49,8 @@ public class DisplayItem {
System.out.println("Spawned item. Safeguarding.");
}
try {
NMS.safeGuard(this.item);
this.safeGuard(this.item);
//NMS.safeGuard
} catch (Exception e) {
e.printStackTrace();
System.out.println("QuickShop version mismatch! This version of QuickShop is incompatible with this version of bukkit! Try update?");
@@ -60,7 +64,10 @@ public class DisplayItem {
remove();
spawn();
}
public void safeGuard(Item item) {
item.setPickupDelay(Integer.MAX_VALUE);
item.setCustomName(ChatColor.RED + "QuickShop " + Util.getName(iStack) + " " + nextId++);
}
/**
* Removes all items floating ontop of the chest that aren't the display
* item.
@@ -1,266 +0,0 @@
package org.maxgamer.quickshop.Util;
import java.util.Collection;
import java.util.Map;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.potion.PotionType;
public class CustomPotionsName {
private static Map<PotionType,Names> potionTypes;
private static Map<PotionEffectType,String> potionEffects;
private static String[] signFormat, shopInfoFormat;
private CustomPotionsName() {}
public static String getSignName(ItemStack potionItemStack) {
GenericPotionData potion = NMS.getPotionData(potionItemStack);
if (potion==null) {
return "InvalidPotion";
}
if (potion.isCustom() && potionItemStack.hasItemMeta() && potionItemStack.getItemMeta().hasDisplayName()) {
// return potion name for custom potions
return potionItemStack.getItemMeta().getDisplayName().replace(" ", "");
}
Names names = potionTypes.get(potion.getType());
String type;
if (names==null) {
if (potion.getType()==PotionType.WATER) {
return "WaterBottle";
}
type = Util.prettifyText(potion.getType().toString()).replace(" ", "")+"Potion";
} else {
type = names.getSign();
}
type+=(potion.getAmplifier()>1 ? potion.getAmplifier() : "")+(potion.getDuration()==-1 ? "+" : "");
String variety;
switch(potion.getCategory()) {
case NORMAL:
variety = getSignFormat()[1];
break;
case SPLASH:
variety = getSignFormat()[2];
break;
case LINGERING:
variety = getSignFormat()[3];
break;
default:
variety = "Invalid";
break;
}
return (getSignFormat()[0].replace("%variety", variety).replace("%type", type)).trim();
}
public static String getFullName(ItemStack potionItemStack) {
GenericPotionData potion = NMS.getPotionData(potionItemStack);
if (potion==null) {
return "InvalidPotion";
}
if (potion.isCustom() && potionItemStack.hasItemMeta() && potionItemStack.getItemMeta().hasDisplayName()) {
// return potion name for custom potions
return potionItemStack.getItemMeta().getDisplayName().replace(" ", "");
}
Names names = potionTypes.get(potion.getType());
String type;
if (names==null) {
if (potion.getType()==PotionType.WATER) {
return "Water Bottle";
}
type = Util.prettifyText(potion.getType().toString())+" Potion";
} else {
type = names.getFull();
}
type+=(potion.getAmplifier()>1 ? " "+potion.getAmplifier() : "")+(potion.getDuration()>0 ? " ("+((int)(potion.getDuration()/20))+"s)" : potion.getDuration()==-1 ? "+" : "");
String variety;
switch(potion.getCategory()) {
case NORMAL:
variety = getShopInfoFormat()[1];
break;
case SPLASH:
variety = getShopInfoFormat()[2];
break;
case LINGERING:
variety = getShopInfoFormat()[3];
break;
default:
variety = "Invalid";
break;
}
return (getShopInfoFormat()[0].replace("%variety", variety).replace("%type", type)).trim();
}
public static String getEffects(ItemStack potionItemStack) {
GenericPotionData potion = NMS.getPotionData(potionItemStack);
if (potion==null) {
return "InvalidPotion";
}
StringBuilder sb = new StringBuilder();
for (PotionEffect potionEffect : potion.getEffects()) {
PotionEffectType type = potionEffect.getType();
String name = potionEffects.get(type);
if (name==null) {
name = Util.prettifyText(type.getName());
}
sb.append(name.trim()+(potionEffect.getAmplifier()>1 ? " "+potionEffect.getAmplifier() : "")+(potionEffect.getDuration()>0 ? " ("+((int)(potionEffect.getDuration()/20))+"s)" : potionEffect.getDuration()==-1 ? "+" : "")+" | ");
}
if (sb.length()>0) {
sb.setLength(sb.length()-3);
}
return sb.toString();
}
public static class Names {
private String sign, full;
public Names(String sign, String full) {
this.sign = sign;
this.full = full;
}
public String getSign() {
return sign;
}
public String getFull() {
return full;
}
@Override
public String toString() {
return "Names [sign=" + sign + ", full=" + full + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((full == null) ? 0 : full.hashCode());
result = prime * result + ((sign == null) ? 0 : sign.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof Names)) {
return false;
}
Names other = (Names) obj;
if (full == null) {
if (other.full != null) {
return false;
}
} else if (!full.equals(other.full)) {
return false;
}
if (sign == null) {
if (other.sign != null) {
return false;
}
} else if (!sign.equals(other.sign)) {
return false;
}
return true;
}
}
public static class GenericPotionData {
private PotionType type;
private Collection<PotionEffect> effects;
private Category category;
private boolean custom;
private int amplifier, duration;
public GenericPotionData(PotionType type, Collection<PotionEffect> effects, Category category, boolean isCustom, int duration, int amplifier) {
this.type = type;
this.effects = effects;
this.category = category;
this.custom = isCustom;
this.duration = duration;
this.amplifier = amplifier;
}
public PotionType getType() {
return type;
}
public Collection<PotionEffect> getEffects() {
return effects;
}
public Category getCategory() {
return category;
}
public boolean isCustom() {
return custom;
}
public int getAmplifier() {
return amplifier;
}
public int getDuration() {
return duration;
}
public enum Category {
NORMAL, SPLASH, LINGERING;
}
}
public static String[] getSignFormat() {
return signFormat;
}
public static String[] getShopInfoFormat() {
return shopInfoFormat;
}
public static void setSignFormat(String[] signFormat) {
CustomPotionsName.signFormat = signFormat;
}
public static void setShopInfoFormat(String[] shopInfoFormat) {
CustomPotionsName.shopInfoFormat = shopInfoFormat;
}
public static Map<PotionType, Names> getPotionTypes() {
return potionTypes;
}
public static void setPotionTypes(Map<PotionType, Names> potionTypes) {
CustomPotionsName.potionTypes = potionTypes;
}
public static Map<PotionEffectType,String> getPotionEffects() {
return potionEffects;
}
public static void setPotionEffects(Map<PotionEffectType,String> potionEffects) {
CustomPotionsName.potionEffects = potionEffects;
}
}
@@ -0,0 +1,250 @@
//package org.maxgamer.quickshop.Util;
//
//import java.util.Collection;
//import java.util.Map;
//
//import org.bukkit.inventory.ItemStack;
//import org.bukkit.inventory.meta.PotionMeta;
//import org.bukkit.potion.Potion;
//import org.bukkit.potion.PotionData;
//import org.bukkit.potion.PotionEffect;
//import org.bukkit.potion.PotionEffectType;
//import org.bukkit.potion.PotionType;
//
//public class CustomPotionsNamesss {
// private static Map<PotionType,Names> potionTypes;
// private static Map<PotionEffectType,String> potionEffects;
// private static String[] signFormat, shopInfoFormat;
//
// private CustomPotionsNamesss() {}
//
// public static String getSignName(ItemStack potionItemStack) {
// org.bukkit.potion.Potion potion = null;
// potion.apply(potionItemStack);
// PotionMeta meta = null;
// if (potion==null) {
// return "InvalidPotion";
// }
//
// if (potionItemStack.hasItemMeta() && potionItemStack.getItemMeta().hasDisplayName()) {
// // return potion name for custom potions
// return potionItemStack.getItemMeta().getDisplayName().replace(" ", "");
// }
//
// Names names = potionTypes.get(potion.getType());
// String type;
// if (names==null) {
// if (potion.getType()==PotionType.WATER) {
// return "WaterBottle";
// }
// type = Util.prettifyText(potion.getType().toString()).replace(" ", "")+"Potion";
// } else {
// type = names.getSign();
// }
// Collection<PotionEffect> effect = potion.getEffects();
// for (PotionEffect potionEffect : effect) {
// type+=(potionEffect.getAmplifier() >1 ? potionEffect.getAmplifier() : "")+(potionEffect.getDuration()==-1 ? "+" : "");
// }
//
// String variety;
// variety = getSignFormat()[1];
// return (getSignFormat()[0].replace("%variety", variety).replace("%type", type)).trim();
// }
//
// public static String getFullName(ItemStack potionItemStack) {
// org.bukkit.potion.Potion potion = null;
// potion.apply(potionItemStack);
// PotionMeta meta = null;
// if (potion==null) {
// return "InvalidPotion";
// }
//
// if (potion.isCustom() && potionItemStack.hasItemMeta() && potionItemStack.getItemMeta().hasDisplayName()) {
// // return potion name for custom potions
// return potionItemStack.getItemMeta().getDisplayName().replace(" ", "");
// }
//
// Names names = potionTypes.get(potion.getType());
// String type;
// if (names==null) {
// if (potion.getType()==PotionType.WATER) {
// return "Water Bottle";
// }
// type = Util.prettifyText(potion.getType().toString())+" Potion";
// } else {
// type = names.getFull();
// }
// Collection<PotionEffect> effect = potion.getEffects();
//
// for (PotionEffect potionEffect : effect) {
// type+=(potionEffect.getAmplifier() >1 ? potionEffect.getAmplifier() : "")+(potionEffect.getDuration()==-1 ? "+" : "");
// }
//
// String variety = getSignFormat()[1];
//
// return (getShopInfoFormat()[0].replace("%variety", variety).replace("%type", type)).trim();
// }
//
// public static String getEffects(ItemStack potionItemStack) {
// GenericPotionData potion = NMS.getPotionData(potionItemStack);
// if (potion==null) {
// return "InvalidPotion";
// }
//
// StringBuilder sb = new StringBuilder();
//
// for (PotionEffect potionEffect : potion.getEffects()) {
// PotionEffectType type = potionEffect.getType();
// String name = potionEffects.get(type);
// if (name==null) {
// name = Util.prettifyText(type.getName());
// }
//
// sb.append(name.trim()+(potionEffect.getAmplifier()>1 ? " "+potionEffect.getAmplifier() : "")+(potionEffect.getDuration()>0 ? " ("+((int)(potionEffect.getDuration()/20))+"s)" : potionEffect.getDuration()==-1 ? "+" : "")+" | ");
// }
//
// if (sb.length()>0) {
// sb.setLength(sb.length()-3);
// }
//
// return sb.toString();
// }
//
// public static class Names {
// private String sign, full;
//
// public Names(String sign, String full) {
// this.sign = sign;
// this.full = full;
// }
//
// public String getSign() {
// return sign;
// }
//
// public String getFull() {
// return full;
// }
//
// @Override
// public String toString() {
// return "Names [sign=" + sign + ", full=" + full + "]";
// }
//
// @Override
// public int hashCode() {
// final int prime = 31;
// int result = 1;
// result = prime * result + ((full == null) ? 0 : full.hashCode());
// result = prime * result + ((sign == null) ? 0 : sign.hashCode());
// return result;
// }
//
// @Override
// public boolean equals(Object obj) {
// if (this == obj) {
// return true;
// }
// if (obj == null) {
// return false;
// }
// if (!(obj instanceof Names)) {
// return false;
// }
// Names other = (Names) obj;
// if (full == null) {
// if (other.full != null) {
// return false;
// }
// } else if (!full.equals(other.full)) {
// return false;
// }
// if (sign == null) {
// if (other.sign != null) {
// return false;
// }
// } else if (!sign.equals(other.sign)) {
// return false;
// }
// return true;
// }
//
// }
//
// public static class GenericPotionData {
// private PotionType type;
// private Collection<PotionEffect> effects;
// private Category category;
// private boolean custom;
// private int amplifier, duration;
//
// public GenericPotionData(PotionType type, Collection<PotionEffect> effects, Category category, boolean isCustom, int duration, int amplifier) {
// this.type = type;
// this.effects = effects;
// this.category = category;
// this.custom = isCustom;
// this.duration = duration;
// this.amplifier = amplifier;
// }
//
// public PotionType getType() {
// return type;
// }
//
// public Collection<PotionEffect> getEffects() {
// return effects;
// }
//
// public Category getCategory() {
// return category;
// }
//
// public boolean isCustom() {
// return custom;
// }
//
// public int getAmplifier() {
// return amplifier;
// }
//
// public int getDuration() {
// return duration;
// }
//
// public enum Category {
// NORMAL, SPLASH, LINGERING;
// }
// }
//
// public static String[] getSignFormat() {
// return signFormat;
// }
//
// public static String[] getShopInfoFormat() {
// return shopInfoFormat;
// }
//
// public static void setSignFormat(String[] signFormat) {
// CustomPotionsName.signFormat = signFormat;
// }
//
// public static void setShopInfoFormat(String[] shopInfoFormat) {
// CustomPotionsName.shopInfoFormat = shopInfoFormat;
// }
//
// public static Map<PotionType, Names> getPotionTypes() {
// return potionTypes;
// }
//
// public static void setPotionTypes(Map<PotionType, Names> potionTypes) {
// CustomPotionsName.potionTypes = potionTypes;
// }
//
// public static Map<PotionEffectType,String> getPotionEffects() {
// return potionEffects;
// }
//
// public static void setPotionEffects(Map<PotionEffectType,String> potionEffects) {
// CustomPotionsName.potionEffects = potionEffects;
// }
//}
@@ -1,285 +1,285 @@
package org.maxgamer.quickshop.Util;
import java.io.File;
import java.io.InputStreamReader;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import org.maxgamer.quickshop.QuickShop;
import org.maxgamer.quickshop.Shop.Shop;
public class MsgUtil {
private static QuickShop plugin;
private static YamlConfiguration messages;
private static HashMap<UUID, LinkedList<String>> player_messages = new HashMap<UUID, LinkedList<String>>();
static {
plugin = QuickShop.instance;
}
/**
* Loads all the messages from messages.yml
*/
public static void loadCfgMessages() {
// Load messages.yml
File messageFile = new File(plugin.getDataFolder(), "messages.yml");
if (!messageFile.exists()) {
plugin.getLogger().info("Creating messages.yml");
plugin.saveResource("messages.yml", true);
}
// Store it
messages = YamlConfiguration.loadConfiguration(messageFile);
messages.options().copyDefaults(true);
// Load default messages
YamlConfiguration defMessages = YamlConfiguration.loadConfiguration(new InputStreamReader(plugin.getResource("messages.yml")));
messages.setDefaults(defMessages);
// Parse colour codes
Util.parseColours(messages);
}
/**
* loads all player purchase messages from the database.
*/
public static void loadTransactionMessages() { //TODO Converted to UUID
player_messages.clear(); // Delete old messages
try {
ResultSet rs = plugin.getDB().getConnection().prepareStatement("SELECT * FROM messages").executeQuery();
while (rs.next()) {
UUID owner = UUID.fromString(rs.getString("owner"));
String message = rs.getString("message");
LinkedList<String> msgs = player_messages.get(owner);
if (msgs == null) {
msgs = new LinkedList<String>();
player_messages.put(owner, msgs);
}
msgs.add(message);
}
} catch (SQLException e) {
e.printStackTrace();
System.out.println("Could not load transaction messages from database. Skipping.");
}
}
/**
* @param player
* The name of the player to message
* @param message
* The message to send them Sends the given player a message if
* they're online. Else, if they're not online, queues it for
* them in the database.
*/
public static void send(UUID player, String message) { //TODO Converted to UUID
OfflinePlayer p = Bukkit.getOfflinePlayer(player);
if (p == null || !p.isOnline()) {
LinkedList<String> msgs = player_messages.get(player);
if (msgs == null) {
msgs = new LinkedList<String>();
player_messages.put(player, msgs);
}
msgs.add(message);
String q = "INSERT INTO messages (owner, message, time) VALUES (?, ?, ?)";
plugin.getDB().execute(q, player.toString(), message, System.currentTimeMillis());
} else {
p.getPlayer().sendMessage(message);
}
}
/**
* Deletes any messages that are older than a week in the database, to save
* on space.
*/
public static void clean() {
System.out.println("Cleaning purchase messages from database that are over a week old...");
// 604800,000 msec = 1 week.
long weekAgo = System.currentTimeMillis() - 604800000;
plugin.getDB().execute("DELETE FROM messages WHERE time < ?", weekAgo);
}
/**
* Empties the queue of messages a player has and sends them to the player.
*
* @param p
* The player to message
* @return true if success, false if the player is offline or null
*/
public static boolean flush(OfflinePlayer p) { //TODO Changed to UUID
if (p != null && p.isOnline()) {
UUID pName = p.getUniqueId();
LinkedList<String> msgs = player_messages.get(pName);
if (msgs != null) {
for (String msg : msgs) {
p.getPlayer().sendMessage(msg);
}
plugin.getDB().execute("DELETE FROM messages WHERE owner = ?", pName.toString());
msgs.clear();
}
return true;
}
return false;
}
public static void sendShopInfo(Player p, Shop shop) {
// Potentially faster with an array?
ItemStack items = shop.getItem();
p.sendMessage("");
p.sendMessage("");
p.sendMessage(ChatColor.DARK_PURPLE + "+---------------------------------------------------+");
p.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.shop-information"));
p.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.owner", shop.ownerName()));
p.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.item", shop.getDataName()));
if (NMS.isPotion(items.getType())) {
String effects = CustomPotionsName.getEffects(items);
if (!effects.isEmpty()) {
p.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.effects", effects));
}
}
if (Util.isTool(items.getType())) {
p.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.damage-percent-remaining", Util.getToolPercentage(items)));
}
if (shop.isSelling()) {
p.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.stock", "" + shop.getRemainingStock()));
} else {
p.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.space", "" + shop.getRemainingSpace()));
}
p.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.price-per", shop.getDataName(), Util.format(shop.getPrice())));
if (shop.isBuying()) {
p.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.this-shop-is-buying"));
} else {
p.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.this-shop-is-selling"));
}
Map<Enchantment, Integer> enchs = items.getItemMeta().getEnchants();
if (enchs != null && !enchs.isEmpty()) {
p.sendMessage(ChatColor.DARK_PURPLE + "+--------------------" + MsgUtil.getMessage("menu.enchants") + "-----------------------+");
for (Entry<Enchantment, Integer> entries : enchs.entrySet()) {
p.sendMessage(ChatColor.DARK_PURPLE + "| " + ChatColor.YELLOW + entries.getKey() + " " + entries.getValue());
}
}
try {
Class.forName("org.bukkit.inventory.meta.EnchantmentStorageMeta");
if (items.getItemMeta() instanceof EnchantmentStorageMeta) {
EnchantmentStorageMeta stor = (EnchantmentStorageMeta) items.getItemMeta();
stor.getStoredEnchants();
enchs = stor.getStoredEnchants();
if (enchs != null && !enchs.isEmpty()) {
p.sendMessage(ChatColor.DARK_PURPLE + "+-----------------" + MsgUtil.getMessage("menu.stored-enchants") + "--------------------+");
for (Entry<Enchantment, Integer> entries : enchs.entrySet()) {
p.sendMessage(ChatColor.DARK_PURPLE + "| " + ChatColor.YELLOW + entries.getKey() + " " + entries.getValue());
}
}
}
} catch (ClassNotFoundException e) {
// They don't have an up to date enough build of CB to do this.
// TODO: Remove this when it becomes redundant
}
p.sendMessage(ChatColor.DARK_PURPLE + "+---------------------------------------------------+");
}
public static void sendPurchaseSuccess(Player p, Shop shop, int amount) {
p.sendMessage(ChatColor.DARK_PURPLE + "+---------------------------------------------------+");
p.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.successful-purchase"));
p.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.item-name-and-price", "" + amount, shop.getDataName(), Util.format((amount * shop.getPrice()))));
Map<Enchantment, Integer> enchs = shop.getItem().getItemMeta().getEnchants();
if (enchs != null && !enchs.isEmpty()) {
p.sendMessage(ChatColor.DARK_PURPLE + "+--------------------" + MsgUtil.getMessage("menu.enchants") + "-----------------------+");
for (Entry<Enchantment, Integer> entries : enchs.entrySet()) {
p.sendMessage(ChatColor.DARK_PURPLE + "| " + ChatColor.YELLOW + entries.getKey() + " " + entries.getValue());
}
}
enchs = shop.getItem().getItemMeta().getEnchants();
if (enchs != null && !enchs.isEmpty()) {
p.sendMessage(ChatColor.DARK_PURPLE + "+-----------------" + MsgUtil.getMessage("menu.stored-enchants") + "--------------------+");
for (Entry<Enchantment, Integer> entries : enchs.entrySet()) {
p.sendMessage(ChatColor.DARK_PURPLE + "| " + ChatColor.YELLOW + entries.getKey() + " " + entries.getValue());
}
}
try {
Class.forName("org.bukkit.inventory.meta.EnchantmentStorageMeta");
if (shop.getItem().getItemMeta() instanceof EnchantmentStorageMeta) {
EnchantmentStorageMeta stor = (EnchantmentStorageMeta) shop.getItem().getItemMeta();
stor.getStoredEnchants();
enchs = stor.getStoredEnchants();
if (enchs != null && !enchs.isEmpty()) {
p.sendMessage(ChatColor.DARK_PURPLE + "+-----------------" + MsgUtil.getMessage("menu.stored-enchants") + "--------------------+");
for (Entry<Enchantment, Integer> entries : enchs.entrySet()) {
p.sendMessage(ChatColor.DARK_PURPLE + "| " + ChatColor.YELLOW + entries.getKey() + " " + entries.getValue());
}
}
}
} catch (ClassNotFoundException e) {
// They don't have an up to date enough build of CB to do this.
// TODO: Remove this when it becomes redundant
}
p.sendMessage(ChatColor.DARK_PURPLE + "+---------------------------------------------------+");
}
public static void sendSellSuccess(Player p, Shop shop, int amount) {
p.sendMessage(ChatColor.DARK_PURPLE + "+---------------------------------------------------+");
p.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.successfully-sold"));
p.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.item-name-and-price", "" + amount, shop.getDataName(), Util.format((amount * shop.getPrice()))));
if (plugin.getConfig().getBoolean("show-tax")) {
double tax = plugin.getConfig().getDouble("tax");
double total = amount * shop.getPrice();
if (tax != 0) {
if (!p.getUniqueId().equals(shop.getOwner())) {
p.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.sell-tax", "" + Util.format((tax * total))));
} else {
p.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.sell-tax-self"));
}
}
}
Map<Enchantment, Integer> enchs = shop.getItem().getItemMeta().getEnchants();
if (enchs != null && !enchs.isEmpty()) {
p.sendMessage(ChatColor.DARK_PURPLE + "+--------------------" + MsgUtil.getMessage("menu.enchants") + "-----------------------+");
for (Entry<Enchantment, Integer> entries : enchs.entrySet()) {
p.sendMessage(ChatColor.DARK_PURPLE + "| " + ChatColor.YELLOW + entries.getKey() + " " + entries.getValue());
}
}
try {
Class.forName("org.bukkit.inventory.meta.EnchantmentStorageMeta");
if (shop.getItem().getItemMeta() instanceof EnchantmentStorageMeta) {
EnchantmentStorageMeta stor = (EnchantmentStorageMeta) shop.getItem().getItemMeta();
stor.getStoredEnchants();
enchs = stor.getStoredEnchants();
if (enchs != null && !enchs.isEmpty()) {
p.sendMessage(ChatColor.DARK_PURPLE + "+--------------------" + MsgUtil.getMessage("menu.stored-enchants") + "-----------------------+");
for (Entry<Enchantment, Integer> entries : enchs.entrySet()) {
p.sendMessage(ChatColor.DARK_PURPLE + "| " + ChatColor.YELLOW + entries.getKey() + " " + entries.getValue());
}
}
}
} catch (ClassNotFoundException e) {
// They don't have an up to date enough build of CB to do this.
// TODO: Remove this when it becomes redundant
}
p.sendMessage(ChatColor.DARK_PURPLE + "+---------------------------------------------------+");
}
public static String getMessage(String loc, String... args) {
String raw = messages.getString(loc);
if (raw == null) {
return "Invalid message: " + loc;
}
if (raw.isEmpty()) {
return "";
}
if (args == null) {
return raw;
}
for (int i = 0; i < args.length; i++) {
raw = raw.replace("{" + i + "}", args[i]==null ? "null" : args[i]);
}
return raw;
}
}
package org.maxgamer.quickshop.Util;
import java.io.File;
import java.io.InputStreamReader;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import org.maxgamer.quickshop.QuickShop;
import org.maxgamer.quickshop.Shop.Shop;
public class MsgUtil {
private static QuickShop plugin;
private static YamlConfiguration messages;
private static HashMap<UUID, LinkedList<String>> player_messages = new HashMap<UUID, LinkedList<String>>();
static {
plugin = QuickShop.instance;
}
/**
* Loads all the messages from messages.yml
*/
public static void loadCfgMessages() {
// Load messages.yml
File messageFile = new File(plugin.getDataFolder(), "messages.yml");
if (!messageFile.exists()) {
plugin.getLogger().info("Creating messages.yml");
plugin.saveResource("messages.yml", true);
}
// Store it
messages = YamlConfiguration.loadConfiguration(messageFile);
messages.options().copyDefaults(true);
// Load default messages
YamlConfiguration defMessages = YamlConfiguration.loadConfiguration(new InputStreamReader(plugin.getResource("messages.yml")));
messages.setDefaults(defMessages);
// Parse colour codes
Util.parseColours(messages);
}
/**
* loads all player purchase messages from the database.
*/
public static void loadTransactionMessages() { //TODO Converted to UUID
player_messages.clear(); // Delete old messages
try {
ResultSet rs = plugin.getDB().getConnection().prepareStatement("SELECT * FROM messages").executeQuery();
while (rs.next()) {
UUID owner = UUID.fromString(rs.getString("owner"));
String message = rs.getString("message");
LinkedList<String> msgs = player_messages.get(owner);
if (msgs == null) {
msgs = new LinkedList<String>();
player_messages.put(owner, msgs);
}
msgs.add(message);
}
} catch (SQLException e) {
e.printStackTrace();
System.out.println("Could not load transaction messages from database. Skipping.");
}
}
/**
* @param player
* The name of the player to message
* @param message
* The message to send them Sends the given player a message if
* they're online. Else, if they're not online, queues it for
* them in the database.
*/
public static void send(UUID player, String message) { //TODO Converted to UUID
OfflinePlayer p = Bukkit.getOfflinePlayer(player);
if (p == null || !p.isOnline()) {
LinkedList<String> msgs = player_messages.get(player);
if (msgs == null) {
msgs = new LinkedList<String>();
player_messages.put(player, msgs);
}
msgs.add(message);
String q = "INSERT INTO messages (owner, message, time) VALUES (?, ?, ?)";
plugin.getDB().execute(q, player.toString(), message, System.currentTimeMillis());
} else {
p.getPlayer().sendMessage(message);
}
}
/**
* Deletes any messages that are older than a week in the database, to save
* on space.
*/
public static void clean() {
System.out.println("Cleaning purchase messages from database that are over a week old...");
// 604800,000 msec = 1 week.
long weekAgo = System.currentTimeMillis() - 604800000;
plugin.getDB().execute("DELETE FROM messages WHERE time < ?", weekAgo);
}
/**
* Empties the queue of messages a player has and sends them to the player.
*
* @param p
* The player to message
* @return true if success, false if the player is offline or null
*/
public static boolean flush(OfflinePlayer p) { //TODO Changed to UUID
if (p != null && p.isOnline()) {
UUID pName = p.getUniqueId();
LinkedList<String> msgs = player_messages.get(pName);
if (msgs != null) {
for (String msg : msgs) {
p.getPlayer().sendMessage(msg);
}
plugin.getDB().execute("DELETE FROM messages WHERE owner = ?", pName.toString());
msgs.clear();
}
return true;
}
return false;
}
public static void sendShopInfo(Player p, Shop shop) {
// Potentially faster with an array?
ItemStack items = shop.getItem();
p.sendMessage("");
p.sendMessage("");
p.sendMessage(ChatColor.DARK_PURPLE + "+---------------------------------------------------+");
p.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.shop-information"));
p.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.owner", shop.ownerName()));
p.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.item", shop.getDataName()));
// if (NMS.isPotion(items.getType())) {
// String effects = CustomPotionsName.getEffects(items);
// if (!effects.isEmpty()) {
// p.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.effects", effects));
// }
// }
if (Util.isTool(items.getType())) {
p.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.damage-percent-remaining", Util.getToolPercentage(items)));
}
if (shop.isSelling()) {
p.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.stock", "" + shop.getRemainingStock()));
} else {
p.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.space", "" + shop.getRemainingSpace()));
}
p.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.price-per", shop.getDataName(), Util.format(shop.getPrice())));
if (shop.isBuying()) {
p.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.this-shop-is-buying"));
} else {
p.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.this-shop-is-selling"));
}
Map<Enchantment, Integer> enchs = items.getItemMeta().getEnchants();
if (enchs != null && !enchs.isEmpty()) {
p.sendMessage(ChatColor.DARK_PURPLE + "+--------------------" + MsgUtil.getMessage("menu.enchants") + "-----------------------+");
for (Entry<Enchantment, Integer> entries : enchs.entrySet()) {
p.sendMessage(ChatColor.DARK_PURPLE + "| " + ChatColor.YELLOW + entries.getKey() + " " + entries.getValue());
}
}
try {
Class.forName("org.bukkit.inventory.meta.EnchantmentStorageMeta");
if (items.getItemMeta() instanceof EnchantmentStorageMeta) {
EnchantmentStorageMeta stor = (EnchantmentStorageMeta) items.getItemMeta();
stor.getStoredEnchants();
enchs = stor.getStoredEnchants();
if (enchs != null && !enchs.isEmpty()) {
p.sendMessage(ChatColor.DARK_PURPLE + "+-----------------" + MsgUtil.getMessage("menu.stored-enchants") + "--------------------+");
for (Entry<Enchantment, Integer> entries : enchs.entrySet()) {
p.sendMessage(ChatColor.DARK_PURPLE + "| " + ChatColor.YELLOW + entries.getKey() + " " + entries.getValue());
}
}
}
} catch (ClassNotFoundException e) {
// They don't have an up to date enough build of CB to do this.
// TODO: Remove this when it becomes redundant
}
p.sendMessage(ChatColor.DARK_PURPLE + "+---------------------------------------------------+");
}
public static void sendPurchaseSuccess(Player p, Shop shop, int amount) {
p.sendMessage(ChatColor.DARK_PURPLE + "+---------------------------------------------------+");
p.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.successful-purchase"));
p.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.item-name-and-price", "" + amount, shop.getDataName(), Util.format((amount * shop.getPrice()))));
Map<Enchantment, Integer> enchs = shop.getItem().getItemMeta().getEnchants();
if (enchs != null && !enchs.isEmpty()) {
p.sendMessage(ChatColor.DARK_PURPLE + "+--------------------" + MsgUtil.getMessage("menu.enchants") + "-----------------------+");
for (Entry<Enchantment, Integer> entries : enchs.entrySet()) {
p.sendMessage(ChatColor.DARK_PURPLE + "| " + ChatColor.YELLOW + entries.getKey() + " " + entries.getValue());
}
}
enchs = shop.getItem().getItemMeta().getEnchants();
if (enchs != null && !enchs.isEmpty()) {
p.sendMessage(ChatColor.DARK_PURPLE + "+-----------------" + MsgUtil.getMessage("menu.stored-enchants") + "--------------------+");
for (Entry<Enchantment, Integer> entries : enchs.entrySet()) {
p.sendMessage(ChatColor.DARK_PURPLE + "| " + ChatColor.YELLOW + entries.getKey() + " " + entries.getValue());
}
}
try {
Class.forName("org.bukkit.inventory.meta.EnchantmentStorageMeta");
if (shop.getItem().getItemMeta() instanceof EnchantmentStorageMeta) {
EnchantmentStorageMeta stor = (EnchantmentStorageMeta) shop.getItem().getItemMeta();
stor.getStoredEnchants();
enchs = stor.getStoredEnchants();
if (enchs != null && !enchs.isEmpty()) {
p.sendMessage(ChatColor.DARK_PURPLE + "+-----------------" + MsgUtil.getMessage("menu.stored-enchants") + "--------------------+");
for (Entry<Enchantment, Integer> entries : enchs.entrySet()) {
p.sendMessage(ChatColor.DARK_PURPLE + "| " + ChatColor.YELLOW + entries.getKey() + " " + entries.getValue());
}
}
}
} catch (ClassNotFoundException e) {
// They don't have an up to date enough build of CB to do this.
// TODO: Remove this when it becomes redundant
}
p.sendMessage(ChatColor.DARK_PURPLE + "+---------------------------------------------------+");
}
public static void sendSellSuccess(Player p, Shop shop, int amount) {
p.sendMessage(ChatColor.DARK_PURPLE + "+---------------------------------------------------+");
p.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.successfully-sold"));
p.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.item-name-and-price", "" + amount, shop.getDataName(), Util.format((amount * shop.getPrice()))));
if (plugin.getConfig().getBoolean("show-tax")) {
double tax = plugin.getConfig().getDouble("tax");
double total = amount * shop.getPrice();
if (tax != 0) {
if (!p.getUniqueId().equals(shop.getOwner())) {
p.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.sell-tax", "" + Util.format((tax * total))));
} else {
p.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.sell-tax-self"));
}
}
}
Map<Enchantment, Integer> enchs = shop.getItem().getItemMeta().getEnchants();
if (enchs != null && !enchs.isEmpty()) {
p.sendMessage(ChatColor.DARK_PURPLE + "+--------------------" + MsgUtil.getMessage("menu.enchants") + "-----------------------+");
for (Entry<Enchantment, Integer> entries : enchs.entrySet()) {
p.sendMessage(ChatColor.DARK_PURPLE + "| " + ChatColor.YELLOW + entries.getKey() + " " + entries.getValue());
}
}
try {
Class.forName("org.bukkit.inventory.meta.EnchantmentStorageMeta");
if (shop.getItem().getItemMeta() instanceof EnchantmentStorageMeta) {
EnchantmentStorageMeta stor = (EnchantmentStorageMeta) shop.getItem().getItemMeta();
stor.getStoredEnchants();
enchs = stor.getStoredEnchants();
if (enchs != null && !enchs.isEmpty()) {
p.sendMessage(ChatColor.DARK_PURPLE + "+--------------------" + MsgUtil.getMessage("menu.stored-enchants") + "-----------------------+");
for (Entry<Enchantment, Integer> entries : enchs.entrySet()) {
p.sendMessage(ChatColor.DARK_PURPLE + "| " + ChatColor.YELLOW + entries.getKey() + " " + entries.getValue());
}
}
}
} catch (ClassNotFoundException e) {
// They don't have an up to date enough build of CB to do this.
// TODO: Remove this when it becomes redundant
}
p.sendMessage(ChatColor.DARK_PURPLE + "+---------------------------------------------------+");
}
public static String getMessage(String loc, String... args) {
String raw = messages.getString(loc);
if (raw == null) {
return "Invalid message: " + loc;
}
if (raw.isEmpty()) {
return "";
}
if (args == null) {
return raw;
}
for (int i = 0; i < args.length; i++) {
raw = raw.replace("{" + i + "}", args[i]==null ? "null" : args[i]);
}
return raw;
}
}
+235 -228
View File
@@ -1,228 +1,235 @@
package org.maxgamer.quickshop.Util;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Item;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionType;
import org.maxgamer.quickshop.QuickShop;
import org.maxgamer.quickshop.Util.CustomPotionsName.GenericPotionData;
import org.maxgamer.quickshop.Util.CustomPotionsName.GenericPotionData.Category;
public class NMS {
private static ArrayList<NMSDependent> nmsDependencies = new ArrayList<NMSDependent>();
private static int nextId = 0;
private static NMSDependent nms;
static {
nmsDependencies.add(new NMSDependent("v1_12") {
@Override
public void safeGuard(Item item) {
if(QuickShop.debug)System.out.println("safeGuard");
org.bukkit.inventory.ItemStack iStack = item.getItemStack();
net.minecraft.server.v1_13_R1.ItemStack nmsI = org.bukkit.craftbukkit.v1_13_R1.inventory.CraftItemStack.asNMSCopy(iStack);
nmsI.setCount(0);
iStack = org.bukkit.craftbukkit.v1_13_R1.inventory.CraftItemStack.asBukkitCopy(nmsI);
item.setItemStack(iStack);
}
@Override
public byte[] getNBTBytes(org.bukkit.inventory.ItemStack iStack) {
try{
if(QuickShop.debug)System.out.println("getNBTBytes");
net.minecraft.server.v1_13_R1.ItemStack is = org.bukkit.craftbukkit.v1_13_R1.inventory.CraftItemStack.asNMSCopy(iStack);
net.minecraft.server.v1_13_R1.NBTTagCompound itemCompound = new net.minecraft.server.v1_13_R1.NBTTagCompound();
itemCompound = is.save(itemCompound);
ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
DataOutputStream dataoutputstream = new DataOutputStream(new GZIPOutputStream(bytearrayoutputstream));
try {
net.minecraft.server.v1_13_R1.NBTCompressedStreamTools.a(itemCompound, (DataOutput) dataoutputstream);
} finally {
dataoutputstream.close();
}
return bytearrayoutputstream.toByteArray();
}catch(Exception e){
return new byte[0];
}
}
@Override
public org.bukkit.inventory.ItemStack getItemStack(byte[] bytes) {
try{
if(QuickShop.debug)System.out.println("getItemStack");
DataInputStream datainputstream = new DataInputStream(new BufferedInputStream(new GZIPInputStream(new ByteArrayInputStream(bytes))));
net.minecraft.server.v1_13_R1.NBTTagCompound nbttagcompound;
try {
nbttagcompound = net.minecraft.server.v1_13_R1.NBTCompressedStreamTools.a((DataInput) datainputstream, null);
} finally {
datainputstream.close();
}
net.minecraft.server.v1_13_R1.ItemStack is = new net.minecraft.server.v1_13_R1.ItemStack(nbttagcompound);
return org.bukkit.craftbukkit.v1_13_R1.inventory.CraftItemStack.asBukkitCopy(is);
}catch(Exception e){
return new ItemStack(Material.AIR);
}
}
@Override
public GenericPotionData getPotionData(ItemStack potionItemStack) {
PotionMeta potionMeta = (PotionMeta) potionItemStack.getItemMeta();
Category category;
switch(potionItemStack.getType()) {
case POTION:
category = Category.NORMAL;
break;
case SPLASH_POTION:
category = Category.SPLASH;
break;
case LINGERING_POTION:
category = Category.LINGERING;
break;
default:
return null;
}
List<PotionEffect> effects = new ArrayList<PotionEffect>(potionMeta.getCustomEffects().size()+1);
if (potionMeta.hasCustomEffects()) {
effects.addAll(potionMeta.getCustomEffects());
}
PotionType potionType = potionMeta.getBasePotionData().getType();
return new GenericPotionData(potionType, effects, category, (potionType==PotionType.WATER || potionType==PotionType.MUNDANE || potionType==PotionType.UNCRAFTABLE || potionType==PotionType.THICK || potionType==PotionType.AWKWARD) && !effects.isEmpty(), potionMeta.getBasePotionData().isExtended() ? -1 : 0, potionMeta.getBasePotionData().isUpgraded() ? 2 : 1);
}
@Override
public boolean isPotion(Material material) {
switch(material) {
case POTION:
case SPLASH_POTION:
case LINGERING_POTION:
return true;
default:
return false;
}
}
});
}
public static void safeGuard(Item item) throws ClassNotFoundException {
if(QuickShop.debug)System.out.println("Renaming");
rename(item.getItemStack());
//if(QuickShop.debug)System.out.println("Protecting");
//protect(item);
if(QuickShop.debug)System.out.println("Seting pickup delay");
item.setPickupDelay(2147483647);
}
public static void init() {
String packageName = Bukkit.getServer().getClass().getPackage().getName();
packageName = packageName.substring(packageName.lastIndexOf(".") + 1);
//System.out.println("Package: " + packageName);
for (NMSDependent dep : nmsDependencies) {
if ((packageName.startsWith(dep.getVersion())) || ((dep.getVersion().isEmpty()) && ((packageName.equals("bukkit")) || (packageName.equals("craftbukkit"))))) {
nms = dep;
return;
}
}
throw new UnsupportedClassVersionError("This version of QuickShop is incompatible. Internal version: "+packageName);
}
private static void rename(ItemStack iStack) {
ItemMeta meta = iStack.getItemMeta();
meta.setDisplayName(ChatColor.RED + "QuickShop " + Util.getName(iStack) + " " + nextId++);
iStack.setItemMeta(meta);
}
public static byte[] getNBTBytes(org.bukkit.inventory.ItemStack iStack) throws ClassNotFoundException {
return nms.getNBTBytes(iStack);
}
public static ItemStack getItemStack(byte[] bytes) throws ClassNotFoundException {
return nms.getItemStack(bytes);
}
public static boolean isPotion(Material material) {
return nms.isPotion(material);
}
public static GenericPotionData getPotionData(ItemStack potion) {
return nms.getPotionData(potion);
}
/*private static void protect(Item item) {
try {
Field itemField = item.getClass().getDeclaredField("item");
itemField.setAccessible(true);
Object nmsEntityItem = itemField.get(item);
Method getItemStack;
try {
getItemStack = nmsEntityItem.getClass().getMethod("getItemStack", new Class[0]);
} catch (NoSuchMethodException e) {
try {
getItemStack = nmsEntityItem.getClass().getMethod("d", new Class[0]);
} catch (NoSuchMethodException e2) {
return;
}
}
Object itemStack = getItemStack.invoke(nmsEntityItem, new Object[0]);
Field countField;
try {
countField = itemStack.getClass().getDeclaredField("count");
} catch (NoSuchFieldException e) {
countField = itemStack.getClass().getDeclaredField("a");
}
countField.setAccessible(true);
countField.set(itemStack, Integer.valueOf(0));
} catch (NoSuchFieldException e) {
e.printStackTrace();
System.out.println("[QuickShop] Could not protect item from pickup properly! Dupes are now possible.");
} catch (Exception e) {
System.out.println("Other error");
e.printStackTrace();
}
}*/
private static abstract class NMSDependent {
private String version;
public String getVersion() {
return this.version;
}
public NMSDependent(String version) {
this.version = version;
}
public abstract boolean isPotion(Material material);
public abstract GenericPotionData getPotionData(ItemStack potionItemStack);
public abstract void safeGuard(Item paramItem);
public byte[] getNBTBytes(org.bukkit.inventory.ItemStack paramItemStack) {
throw new UnsupportedOperationException();
}
public org.bukkit.inventory.ItemStack getItemStack(byte[] paramArrayOfByte) {
throw new UnsupportedOperationException();
}
}
}
//package org.maxgamer.quickshop.Util;
//
//import java.io.BufferedInputStream;
//import java.io.ByteArrayInputStream;
//import java.io.ByteArrayOutputStream;
//import java.io.DataInput;
//import java.io.DataInputStream;
//import java.io.DataOutput;
//import java.io.DataOutputStream;
//import java.util.ArrayList;
//import java.util.List;
//import java.util.zip.GZIPInputStream;
//import java.util.zip.GZIPOutputStream;
//
//import org.bukkit.Bukkit;
//import org.bukkit.ChatColor;
//import org.bukkit.Material;
//import org.bukkit.entity.Item;
//import org.bukkit.inventory.ItemStack;
//import org.bukkit.inventory.meta.ItemMeta;
//import org.bukkit.inventory.meta.PotionMeta;
//import org.bukkit.potion.PotionEffect;
//import org.bukkit.potion.PotionType;
//import org.maxgamer.quickshop.QuickShop;
//import org.maxgamer.quickshop.Util.CustomPotionsName.GenericPotionData;
//import org.maxgamer.quickshop.Util.CustomPotionsName.GenericPotionData.Category;
//
//import net.minecraft.server.v1_13_R1.IMaterial;
//import net.minecraft.server.v1_13_R1.IMerchant;
//import net.minecraft.server.v1_13_R1.Items;
//
//public class NMS {
// private static ArrayList<NMSDependent> nmsDependencies = new ArrayList<NMSDependent>();
// private static int nextId = 0;
// private static NMSDependent nms;
//
// static {
// nmsDependencies.add(new NMSDependent("v1_13") {
// @Override
// public void safeGuard(Item item) {
// if(QuickShop.debug)System.out.println("safeGuard");
// ItemStack iStack = item.getItemStack();
// net.minecraft.server.v1_13_R1.ItemStack nmsItemStack = org.bukkit.craftbukkit.v1_13_R1.inventory.CraftItemStack.asNMSCopy(iStack);
// nmsItemStack.setCount(0);
// iStack = org.bukkit.craftbukkit.v1_13_R1.inventory.CraftItemStack.asBukkitCopy(nmsItemStack);
// item.setItemStack(iStack);
// }
//
// @Override
// public byte[] getNBTBytes(org.bukkit.inventory.ItemStack iStack) {
// try{
// if(QuickShop.debug)System.out.println("getNBTBytes");
// net.minecraft.server.v1_13_R1.ItemStack is = org.bukkit.craftbukkit.v1_13_R1.inventory.CraftItemStack.asNMSCopy(iStack);
// net.minecraft.server.v1_13_R1.NBTTagCompound itemCompound = new net.minecraft.server.v1_13_R1.NBTTagCompound();
// itemCompound = is.save(itemCompound);
// ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
// DataOutputStream dataoutputstream = new DataOutputStream(new GZIPOutputStream(bytearrayoutputstream));
// try {
// net.minecraft.server.v1_13_R1.NBTCompressedStreamTools.a(itemCompound, (DataOutput) dataoutputstream);
// } finally {
// dataoutputstream.close();
// }
// return bytearrayoutputstream.toByteArray();
// }catch(Exception e){
// return new byte[0];
// }
// }
//
// @Override
// public org.bukkit.inventory.ItemStack getItemStack(byte[] bytes) {
// ItemStack is = null;
// is.setData(null);
// try{
// if(QuickShop.debug)System.out.println("getItemStack");
// DataInputStream datainputstream = new DataInputStream(new BufferedInputStream(new GZIPInputStream(new ByteArrayInputStream(bytes))));
// net.minecraft.server.v1_13_R1.NBTTagCompound nbttagcompound;
// try {
// nbttagcompound = net.minecraft.server.v1_13_R1.NBTCompressedStreamTools.a((DataInput) datainputstream, null);
// } finally {
// datainputstream.close();
// }
// IMaterial.
// net.minecraft.server.v1_13_R1.ItemStack is = new net.minecraft.server.v1_13_R1.ItemStack()
// return org.bukkit.craftbukkit.v1_13_R1.inventory.CraftItemStack.asBukkitCopy(is1);
// }catch(Exception e){
// return new ItemStack(Material.AIR);
// }
// }
//
// @Override
// public GenericPotionData getPotionData(ItemStack potionItemStack) {
// PotionMeta potionMeta = (PotionMeta) potionItemStack.getItemMeta();
//
// Category category;
// switch(potionItemStack.getType()) {
// case POTION:
// category = Category.NORMAL;
// break;
// case SPLASH_POTION:
// category = Category.SPLASH;
// break;
// case LINGERING_POTION:
// category = Category.LINGERING;
// break;
// default:
// return null;
// }
// List<PotionEffect> effects = new ArrayList<PotionEffect>(potionMeta.getCustomEffects().size()+1);
//
// if (potionMeta.hasCustomEffects()) {
// effects.addAll(potionMeta.getCustomEffects());
// }
//
// PotionType potionType = potionMeta.getBasePotionData().getType();
//
// return new GenericPotionData(potionType, effects, category, (potionType==PotionType.WATER || potionType==PotionType.MUNDANE || potionType==PotionType.UNCRAFTABLE || potionType==PotionType.THICK || potionType==PotionType.AWKWARD) && !effects.isEmpty(), potionMeta.getBasePotionData().isExtended() ? -1 : 0, potionMeta.getBasePotionData().isUpgraded() ? 2 : 1);
// }
//
// @Override
// public boolean isPotion(Material material) {
// switch(material) {
// case POTION:
// case SPLASH_POTION:
// case LINGERING_POTION:
// return true;
// default:
// return false;
// }
// }
// });
// }
//
// public static void safeGuard(Item item) throws ClassNotFoundException {
// if(QuickShop.debug)System.out.println("Renaming");
// rename(item.getItemStack());
// //if(QuickShop.debug)System.out.println("Protecting");
// //protect(item);
// if(QuickShop.debug)System.out.println("Seting pickup delay");
// item.setPickupDelay(2147483647);
// }
//
// public static void init() {
// String packageName = Bukkit.getServer().getClass().getPackage().getName();
// packageName = packageName.substring(packageName.lastIndexOf(".") + 1);
// //System.out.println("Package: " + packageName);
// for (NMSDependent dep : nmsDependencies) {
// if ((packageName.startsWith(dep.getVersion())) || ((dep.getVersion().isEmpty()) && ((packageName.equals("bukkit")) || (packageName.equals("craftbukkit"))))) {
// nms = dep;
// return;
// }
// }
// throw new UnsupportedClassVersionError("This version of QuickShop is incompatible. Internal version: "+packageName);
// }
//
// private static void rename(ItemStack iStack) {
// ItemMeta meta = iStack.getItemMeta();
// meta.setDisplayName(ChatColor.RED + "QuickShop " + Util.getName(iStack) + " " + nextId++);
// iStack.setItemMeta(meta);
// }
//
// public static byte[] getNBTBytes(org.bukkit.inventory.ItemStack iStack) throws ClassNotFoundException {
// return nms.getNBTBytes(iStack);
// }
//
// public static ItemStack getItemStack(byte[] bytes) throws ClassNotFoundException {
// return nms.getItemStack(bytes);
// }
//
// public static boolean isPotion(Material material) {
// return nms.isPotion(material);
// }
//
// public static GenericPotionData getPotionData(ItemStack potion) {
// return nms.getPotionData(potion);
// }
//
// /*private static void protect(Item item) {
// try {
// Field itemField = item.getClass().getDeclaredField("item");
// itemField.setAccessible(true);
// Object nmsEntityItem = itemField.get(item);
// Method getItemStack;
// try {
// getItemStack = nmsEntityItem.getClass().getMethod("getItemStack", new Class[0]);
// } catch (NoSuchMethodException e) {
// try {
// getItemStack = nmsEntityItem.getClass().getMethod("d", new Class[0]);
// } catch (NoSuchMethodException e2) {
// return;
// }
// }
// Object itemStack = getItemStack.invoke(nmsEntityItem, new Object[0]);
// Field countField;
// try {
// countField = itemStack.getClass().getDeclaredField("count");
// } catch (NoSuchFieldException e) {
// countField = itemStack.getClass().getDeclaredField("a");
// }
// countField.setAccessible(true);
// countField.set(itemStack, Integer.valueOf(0));
// } catch (NoSuchFieldException e) {
// e.printStackTrace();
// System.out.println("[QuickShop] Could not protect item from pickup properly! Dupes are now possible.");
// } catch (Exception e) {
// System.out.println("Other error");
// e.printStackTrace();
// }
// }*/
//
// private static abstract class NMSDependent {
// private String version;
//
// public String getVersion() {
// return this.version;
// }
//
// public NMSDependent(String version) {
// this.version = version;
// }
//
// public abstract boolean isPotion(Material material);
//
// public abstract GenericPotionData getPotionData(ItemStack potionItemStack);
//
// public abstract void safeGuard(Item paramItem);
//
// public byte[] getNBTBytes(org.bukkit.inventory.ItemStack paramItemStack) {
// throw new UnsupportedOperationException();
// }
//
// public org.bukkit.inventory.ItemStack getItemStack(byte[] paramArrayOfByte) {
// throw new UnsupportedOperationException();
// }
// }
//}
@@ -310,9 +310,9 @@ public class Util {
* @return The human readable item name.
*/
public static String getName(ItemStack itemStack) {
if (NMS.isPotion(itemStack.getType())) {
return CustomPotionsName.getFullName(itemStack);
}
// if (NMS.isPotion(itemStack.getType())) {
// return CustomPotionsName.getFullName(itemStack);
// }
CustomItemName customItemName = QuickShop.instance.getCustomItemNames(itemStack);
if (customItemName!=null) {
@@ -347,9 +347,9 @@ public class Util {
// Let's make very long names shorter for our sign
public static String getNameForSign(ItemStack itemStack) {
if (NMS.isPotion(itemStack.getType())) {
return CustomPotionsName.getSignName(itemStack);
}
// if (NMS.isPotion(itemStack.getType())) {
// return CustomPotionsName.getSignName(itemStack);
// }
ItemStack is = itemStack.clone();
is.setAmount(1);