Merged all versions of ShopChest to one jar file

This commit is contained in:
Eric 2015-09-03 20:51:30 +02:00
parent 4b8a0af2f5
commit 06e1d20576
10 changed files with 80 additions and 368 deletions

View File

@ -2,7 +2,7 @@
name: ShopChest
main: de.epiceric.shopchest.ShopChest
version: 1.4.8
version: 1.4.9
author: EpicEric
website: https://www.spigotmc.org/resources/shopchest.11431/
depend: [Vault]

View File

@ -4,32 +4,25 @@ import java.lang.reflect.Method;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.defaults.BukkitCommand;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.inventory.ItemStack;
import de.epiceric.shopchest.config.Config;
import de.epiceric.shopchest.shop.Hologram;
import de.epiceric.shopchest.shop.Shop;
import de.epiceric.shopchest.utils.ClickType;
import de.epiceric.shopchest.utils.JsonBuilder;
import de.epiceric.shopchest.interfaces.JsonBuilder;
import de.epiceric.shopchest.utils.ClickType.EnumClickType;
import de.epiceric.shopchest.utils.JsonBuilder.ClickAction;
import de.epiceric.shopchest.utils.JsonBuilder.HoverAction;
import de.epiceric.shopchest.utils.ShopUtils;
import de.epiceric.shopchest.interfaces.JsonBuilder.ClickAction;
import de.epiceric.shopchest.interfaces.JsonBuilder.HoverAction;
import de.epiceric.shopchest.interfaces.Utils;
import de.epiceric.shopchest.interfaces.jsonbuilder.*;
import de.epiceric.shopchest.utils.UpdateChecker;
import net.md_5.bungee.api.ChatColor;
import net.milkbowl.vault.permission.Permission;
import net.minecraft.server.v1_8_R3.EntityArmorStand;
public class Commands extends BukkitCommand {
@ -165,67 +158,35 @@ public class Commands extends BukkitCommand {
private void checkUpdates(Player player) {
JsonBuilder jb = new JsonBuilder().parse(Config.checking_update());
jb.sendJson(player);
player.sendMessage(Config.checking_update());
UpdateChecker uc = new UpdateChecker(ShopChest.getInstance(), ShopChest.getInstance().getDescription().getWebsite());
if (uc.updateNeeded()) {
ShopChest.latestVersion = uc.getVersion();
ShopChest.downloadLink = uc.getLink();
ShopChest.isUpdateNeeded = true;
JsonBuilder jb2 = new JsonBuilder(Config.update_available(ShopChest.latestVersion)).withHoverEvent(HoverAction.SHOW_TEXT, Config.click_to_download()).withClickEvent(ClickAction.OPEN_URL, ShopChest.downloadLink);
jb2.sendJson(player);
JsonBuilder jb;
switch (Utils.getVersion(plugin.getServer())) {
case "v1_8_R1": jb = new JsonBuilder_R1(Config.update_available(ShopChest.latestVersion)).withHoverEvent(HoverAction.SHOW_TEXT, Config.click_to_download()).withClickEvent(ClickAction.OPEN_URL, ShopChest.downloadLink); break;
case "v1_8_R2": jb = new JsonBuilder_R2(Config.update_available(ShopChest.latestVersion)).withHoverEvent(HoverAction.SHOW_TEXT, Config.click_to_download()).withClickEvent(ClickAction.OPEN_URL, ShopChest.downloadLink); break;
case "v1_8_R3": jb = new JsonBuilder_R3(Config.update_available(ShopChest.latestVersion)).withHoverEvent(HoverAction.SHOW_TEXT, Config.click_to_download()).withClickEvent(ClickAction.OPEN_URL, ShopChest.downloadLink); break;
default: return;
}
jb.sendJson(player);
} else {
ShopChest.latestVersion = "";
ShopChest.downloadLink = "";
ShopChest.isUpdateNeeded = false;
JsonBuilder jb3 = new JsonBuilder().parse(Config.no_new_update());
jb3.sendJson(player);
player.sendMessage(Config.no_new_update());
}
}
private void reload(Player player) {
for (Shop shop : ShopUtils.getShops()) {
Hologram hologram = shop.getHologram();
shop.getItem().remove();
ShopUtils.removeShop(shop);
for (Player p : ShopChest.getInstance().getServer().getOnlinePlayers()) {
hologram.hidePlayer(p);
}
for (Object o : hologram.getEntities()) {
EntityArmorStand e = (EntityArmorStand) o;
e.getWorld().removeEntity(e);
}
}
for (String key : ShopChest.getInstance().shopChests.getKeys(false)) {
OfflinePlayer vendor = ShopChest.getInstance().shopChests.getOfflinePlayer(key + ".vendor");
int locationX = ShopChest.getInstance().shopChests.getInt(key + ".location.x");
int locationY = ShopChest.getInstance().shopChests.getInt(key + ".location.y");
int locationZ = ShopChest.getInstance().shopChests.getInt(key + ".location.z");
World locationWorld = ShopChest.getInstance().getServer().getWorld(ShopChest.getInstance().shopChests.getString(key + ".location.world"));
Location location = new Location(locationWorld, locationX, locationY, locationZ);
ItemStack product = ShopChest.getInstance().shopChests.getItemStack(key + ".product");
double buyPrice = ShopChest.getInstance().shopChests.getDouble(key + ".price.buy");
double sellPrice = ShopChest.getInstance().shopChests.getDouble(key + ".price.sell");
boolean infinite = ShopChest.getInstance().shopChests.getBoolean(key + ".infinite");
ShopUtils.addShop(new Shop(ShopChest.getInstance(), vendor, product, location, buyPrice, sellPrice, infinite));
}
for (Player p : Bukkit.getOnlinePlayers()) {
Bukkit.getPluginManager().callEvent(new PlayerMoveEvent(p, p.getLocation(), p.getLocation()));
}
ShopChest.utils.reload();
player.sendMessage(Config.reloaded_shops(ShopChest.getInstance().shopChests.getKeys(false).size()));
}

View File

@ -26,17 +26,22 @@ import de.epiceric.shopchest.event.ProtectChest;
import de.epiceric.shopchest.event.RegenerateShopItem;
import de.epiceric.shopchest.event.RegenerateShopItemAfterRemove;
import de.epiceric.shopchest.event.UpdateHolograms;
import de.epiceric.shopchest.shop.Hologram;
import de.epiceric.shopchest.interfaces.Utils;
import de.epiceric.shopchest.interfaces.utils.Utils_R1;
import de.epiceric.shopchest.interfaces.utils.Utils_R2;
import de.epiceric.shopchest.interfaces.utils.Utils_R3;
import de.epiceric.shopchest.shop.Shop;
import de.epiceric.shopchest.utils.JsonBuilder;
import de.epiceric.shopchest.utils.JsonBuilder.ClickAction;
import de.epiceric.shopchest.utils.JsonBuilder.HoverAction;
import de.epiceric.shopchest.interfaces.JsonBuilder;
import de.epiceric.shopchest.interfaces.JsonBuilder.ClickAction;
import de.epiceric.shopchest.interfaces.JsonBuilder.HoverAction;
import de.epiceric.shopchest.interfaces.jsonbuilder.JsonBuilder_R1;
import de.epiceric.shopchest.interfaces.jsonbuilder.JsonBuilder_R2;
import de.epiceric.shopchest.interfaces.jsonbuilder.JsonBuilder_R3;
import de.epiceric.shopchest.utils.Metrics;
import de.epiceric.shopchest.utils.ShopUtils;
import de.epiceric.shopchest.utils.UpdateChecker;
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.permission.Permission;
import net.minecraft.server.v1_8_R3.EntityArmorStand;
public class ShopChest extends JavaPlugin{
@ -53,6 +58,8 @@ public class ShopChest extends JavaPlugin{
public static String latestVersion = "";
public static String downloadLink = "";
public static Utils utils;
public static ShopChest getInstance() {
return instance;
}
@ -97,6 +104,17 @@ public class ShopChest extends JavaPlugin{
logger.severe("[ShopChest] [PluginMetrics] Could not submit stats.");
}
switch (Utils.getVersion(getServer())) {
case "v1_8_R1": utils = new Utils_R1(); break;
case "v1_8_R2": utils = new Utils_R2(); break;
case "v1_8_R3": utils = new Utils_R3(); break;
default:
logger.severe("[ShopChest] Incompatible Server Version!");
getServer().getPluginManager().disablePlugin(this);
return;
}
setupPermissions();
instance = this;
@ -118,7 +136,13 @@ public class ShopChest extends JavaPlugin{
if (isUpdateNeeded) {
for (Player p : getServer().getOnlinePlayers()) {
if (p.isOp() || perm.has(p, "shopchest.notification.update")) {
JsonBuilder jb = new JsonBuilder(Config.update_available(latestVersion)).withHoverEvent(HoverAction.SHOW_TEXT, Config.click_to_download()).withClickEvent(ClickAction.OPEN_URL, downloadLink);
JsonBuilder jb;
switch (Utils.getVersion(getServer())) {
case "v1_8_R1": jb = new JsonBuilder_R1(Config.update_available(latestVersion)).withHoverEvent(HoverAction.SHOW_TEXT, Config.click_to_download()).withClickEvent(ClickAction.OPEN_URL, downloadLink); break;
case "v1_8_R2": jb = new JsonBuilder_R2(Config.update_available(latestVersion)).withHoverEvent(HoverAction.SHOW_TEXT, Config.click_to_download()).withClickEvent(ClickAction.OPEN_URL, downloadLink); break;
case "v1_8_R3": jb = new JsonBuilder_R3(Config.update_available(latestVersion)).withHoverEvent(HoverAction.SHOW_TEXT, Config.click_to_download()).withClickEvent(ClickAction.OPEN_URL, downloadLink); break;
default: return;
}
jb.sendJson(p);
}
@ -165,21 +189,7 @@ public class ShopChest extends JavaPlugin{
@Override
public void onDisable() {
for (Shop shop : ShopUtils.getShops()) {
Hologram hologram = shop.getHologram();
for (Player p : getServer().getOnlinePlayers()) {
hologram.hidePlayer(p);
}
for (Object o : hologram.getEntities()) {
EntityArmorStand e = (EntityArmorStand) o;
e.getWorld().removeEntity(e);
}
shop.getItem().remove();
}
utils.removeShops();
}

View File

@ -27,7 +27,8 @@ import de.epiceric.shopchest.utils.ClickType;
import de.epiceric.shopchest.utils.EnchantmentNames;
import de.epiceric.shopchest.utils.ItemNames;
import de.epiceric.shopchest.utils.ShopUtils;
import de.epiceric.shopchest.utils.Utils;
import de.epiceric.shopchest.interfaces.Utils;
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.economy.EconomyResponse;
import net.milkbowl.vault.permission.Permission;

View File

@ -7,9 +7,13 @@ import org.bukkit.event.player.PlayerJoinEvent;
import de.epiceric.shopchest.ShopChest;
import de.epiceric.shopchest.config.Config;
import de.epiceric.shopchest.utils.JsonBuilder;
import de.epiceric.shopchest.utils.JsonBuilder.ClickAction;
import de.epiceric.shopchest.utils.JsonBuilder.HoverAction;
import de.epiceric.shopchest.interfaces.Utils;
import de.epiceric.shopchest.interfaces.jsonbuilder.JsonBuilder_R1;
import de.epiceric.shopchest.interfaces.jsonbuilder.JsonBuilder_R2;
import de.epiceric.shopchest.interfaces.jsonbuilder.JsonBuilder_R3;
import de.epiceric.shopchest.interfaces.JsonBuilder;
import de.epiceric.shopchest.interfaces.JsonBuilder.ClickAction;
import de.epiceric.shopchest.interfaces.JsonBuilder.HoverAction;
import net.milkbowl.vault.permission.Permission;
public class NotifyUpdate implements Listener {
@ -25,9 +29,14 @@ public class NotifyUpdate implements Listener {
if (ShopChest.isUpdateNeeded) {
if (p.isOp() || perm.has(p, "shopchest.notification.update")) {
String version = ShopChest.latestVersion;
String link = ShopChest.downloadLink;
JsonBuilder jb = new JsonBuilder(Config.update_available(version)).withHoverEvent(HoverAction.SHOW_TEXT, Config.click_to_download()).withClickEvent(ClickAction.OPEN_URL, link);
JsonBuilder jb;
switch (Utils.getVersion(ShopChest.getInstance().getServer())) {
case "v1_8_R1": jb = new JsonBuilder_R1(Config.update_available(ShopChest.latestVersion)).withHoverEvent(HoverAction.SHOW_TEXT, Config.click_to_download()).withClickEvent(ClickAction.OPEN_URL, ShopChest.downloadLink); break;
case "v1_8_R2": jb = new JsonBuilder_R2(Config.update_available(ShopChest.latestVersion)).withHoverEvent(HoverAction.SHOW_TEXT, Config.click_to_download()).withClickEvent(ClickAction.OPEN_URL, ShopChest.downloadLink); break;
case "v1_8_R3": jb = new JsonBuilder_R3(Config.update_available(ShopChest.latestVersion)).withHoverEvent(HoverAction.SHOW_TEXT, Config.click_to_download()).withClickEvent(ClickAction.OPEN_URL, ShopChest.downloadLink); break;
default: return;
}
jb.sendJson(p);
}
}

View File

@ -1,22 +1,10 @@
package de.epiceric.shopchest.event;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.inventory.ItemStack;
import de.epiceric.shopchest.ShopChest;
import de.epiceric.shopchest.shop.Hologram;
import de.epiceric.shopchest.shop.Shop;
import de.epiceric.shopchest.utils.ShopUtils;
import net.minecraft.server.v1_8_R3.EntityArmorStand;
public class RegenerateShopItemAfterRemove implements Listener {
@ -31,50 +19,8 @@ public class RegenerateShopItemAfterRemove implements Listener {
}
}
if (containsShopItem) reload();
}
private void reload() {
for (Shop shop : ShopUtils.getShops()) {
Hologram hologram = shop.getHologram();
shop.getItem().remove();
ShopUtils.removeShop(shop);
for (Player p : ShopChest.getInstance().getServer().getOnlinePlayers()) {
hologram.hidePlayer(p);
}
for (Object o : hologram.getEntities()) {
EntityArmorStand e = (EntityArmorStand) o;
e.getWorld().removeEntity(e);
if (containsShopItem) ShopChest.utils.reload();
}
}
for (String key : ShopChest.getInstance().shopChests.getKeys(false)) {
OfflinePlayer vendor = ShopChest.getInstance().shopChests.getOfflinePlayer(key + ".vendor");
int locationX = ShopChest.getInstance().shopChests.getInt(key + ".location.x");
int locationY = ShopChest.getInstance().shopChests.getInt(key + ".location.y");
int locationZ = ShopChest.getInstance().shopChests.getInt(key + ".location.z");
World locationWorld = ShopChest.getInstance().getServer().getWorld(ShopChest.getInstance().shopChests.getString(key + ".location.world"));
Location location = new Location(locationWorld, locationX, locationY, locationZ);
ItemStack product = ShopChest.getInstance().shopChests.getItemStack(key + ".product");
double buyPrice = ShopChest.getInstance().shopChests.getDouble(key + ".price.buy");
double sellPrice = ShopChest.getInstance().shopChests.getDouble(key + ".price.sell");
boolean infinite = ShopChest.getInstance().shopChests.getBoolean(key + ".infinite");
ShopUtils.addShop(new Shop(ShopChest.getInstance(), vendor, product, location, buyPrice, sellPrice, infinite));
}
for (Player p : Bukkit.getOnlinePlayers()) {
Bukkit.getPluginManager().callEvent(new PlayerMoveEvent(p, p.getLocation(), p.getLocation()));
}
}
}

View File

@ -1,89 +0,0 @@
package de.epiceric.shopchest.shop;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.craftbukkit.v1_8_R3.CraftWorld;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
import net.minecraft.server.v1_8_R3.EntityArmorStand;
import net.minecraft.server.v1_8_R3.PacketPlayOutEntityDestroy;
import net.minecraft.server.v1_8_R3.PacketPlayOutSpawnEntityLiving;
public class Hologram {
private List<EntityArmorStand> entitylist = new ArrayList<EntityArmorStand>();
private String[] text;
private Location location;
private double DISTANCE = 0.25D;
int count;
private HashMap<OfflinePlayer, Boolean> visible = new HashMap<OfflinePlayer, Boolean>();
public Hologram(String[] text, Location location) {
this.text = text;
this.location = location;
create();
}
public Hologram(String text, Location location) {
this.text = new String[] {text};
this.location = location;
create();
}
public String getText(int line) {
return text[line];
}
public Location getLocation() {
return location;
}
public List<EntityArmorStand> getEntities() {
return entitylist;
}
public void showPlayer(OfflinePlayer p) {
for (EntityArmorStand armor : entitylist) {
PacketPlayOutSpawnEntityLiving packet = new PacketPlayOutSpawnEntityLiving(armor);
((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet);
}
visible.put(p, true);
}
public void hidePlayer(OfflinePlayer p) {
for (EntityArmorStand armor : entitylist) {
PacketPlayOutEntityDestroy packet = new PacketPlayOutEntityDestroy(armor.getId());
((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet);
}
visible.put(p, false);
}
public boolean isVisible(OfflinePlayer p) {
if (visible.containsKey(p)) return visible.get(p); else return false;
}
private void create() {
for (String text : this.text) {
EntityArmorStand entity = new EntityArmorStand(((CraftWorld) this.location.getWorld()).getHandle(),this.location.getX(), this.location.getY(),this.location.getZ());
entity.setCustomName(text);
entity.setCustomNameVisible(true);
entity.setInvisible(true);
entity.setGravity(false);
entitylist.add(entity);
this.location.subtract(0, this.DISTANCE, 0);
count++;
}
for (int i = 0; i < count; i++) {
this.location.add(0, this.DISTANCE, 0);
}
this.count = 0;
}
}

View File

@ -1,7 +1,6 @@
package de.epiceric.shopchest.shop;
import java.util.ArrayList;
import java.util.Random;
import java.util.UUID;
import org.bukkit.Location;
@ -19,6 +18,11 @@ import org.bukkit.util.Vector;
import de.epiceric.shopchest.ShopChest;
import de.epiceric.shopchest.config.Config;
import de.epiceric.shopchest.interfaces.Hologram;
import de.epiceric.shopchest.interfaces.Utils;
import de.epiceric.shopchest.interfaces.hologram.Hologram_R1;
import de.epiceric.shopchest.interfaces.hologram.Hologram_R2;
import de.epiceric.shopchest.interfaces.hologram.Hologram_R3;
import de.epiceric.shopchest.utils.ItemNames;
public class Shop {
@ -137,7 +141,12 @@ public class Shop {
else if ((buyPrice > 0) && (sellPrice > 0)) holoText[1] = Config.hologram_buy_sell(buyPrice, sellPrice);
else holoText[1] = Config.hologram_buy_sell(buyPrice, sellPrice);
hologram = new Hologram(holoText, holoLocation);
switch (Utils.getVersion(plugin.getServer())) {
case "v1_8_R1": hologram = new Hologram_R1(holoText, holoLocation); break;
case "v1_8_R2": hologram = new Hologram_R2(holoText, holoLocation); break;
case "v1_8_R3": hologram = new Hologram_R3(holoText, holoLocation); break;
default: return null;
}
return hologram;

View File

@ -1,106 +0,0 @@
package de.epiceric.shopchest.utils;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import org.bukkit.ChatColor;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
import org.bukkit.entity.Player;
import net.minecraft.server.v1_8_R3.IChatBaseComponent.ChatSerializer;
import net.minecraft.server.v1_8_R3.PacketPlayOutChat;
public class JsonBuilder {
/* JsonBuilder by FisheyLP */
public enum ClickAction {
RUN_COMMAND, SUGGEST_COMMAND, OPEN_URL
}
public enum HoverAction {
SHOW_TEXT
}
private List<String> extras = new ArrayList<String>();
public JsonBuilder(String... text) {
for(String extra : text)
parse(extra);
}
public JsonBuilder parse(String text) {
String regex = "[&§]{1}([a-fA-Fl-oL-O0-9]){1}";
text = text.replaceAll(regex, "§$1");
if(!Pattern.compile(regex).matcher(text).find()) {
withText(text);
return this;
}
String[] words = text.split(regex);
int index = words[0].length();
for(String word : words) {
try {
if(index != words[0].length())
withText(word).withColor("§"+text.charAt(index - 1));
} catch(Exception e){}
index += word.length() + 2;
}
return this;
}
public JsonBuilder withText(String text) {
extras.add("{text:\"" + text + "\"}");
return this;
}
public JsonBuilder withColor(ChatColor color) {
String c = color.name().toLowerCase();
addSegment(color.isColor() ? "color:" + c : c + ":true");
return this;
}
public JsonBuilder withColor(String color) {
while(color.length() != 1) color = color.substring(1).trim();
withColor(ChatColor.getByChar(color));
return this;
}
public JsonBuilder withClickEvent(ClickAction action, String value) {
addSegment("clickEvent:{action:" + action.toString().toLowerCase()
+ ",value:\"" + value + "\"}");
return this;
}
public JsonBuilder withHoverEvent(HoverAction action, String value) {
addSegment("hoverEvent:{action:" + action.toString().toLowerCase()
+ ",value:\"" + value + "\"}");
return this;
}
private void addSegment(String segment) {
String lastText = extras.get(extras.size() - 1);
lastText = lastText.substring(0, lastText.length() - 1)
+ ","+segment+"}";
extras.remove(extras.size() - 1);
extras.add(lastText);
}
public String toString() {
if(extras.size() <= 1) return extras.size() == 0 ? "{text:\"\"}" : extras.get(0);
String text = extras.get(0).substring(0, extras.get(0).length() - 1) + ",extra:[";
extras.remove(0);;
for (String extra : extras)
text = text + extra + ",";
text = text.substring(0, text.length() - 1) + "]}";
return text;
}
public void sendJson(Player p) {
((CraftPlayer) p).getHandle().playerConnection.sendPacket(
new PacketPlayOutChat(ChatSerializer.a(toString())));
}
}

View File

@ -1,29 +0,0 @@
package de.epiceric.shopchest.utils;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
public class Utils {
public static int getAmount(Inventory inventory, Material type, short damage, ItemMeta itemMeta) {
ItemStack[] items = inventory.getContents();
int amount = 0;
for (ItemStack item : items) {
if ((item != null) && (item.getType().equals(type)) && (item.getDurability() == damage) && (item.getAmount() > 0) && (item.getItemMeta().equals(itemMeta))) {
amount += item.getAmount();
}
}
return amount;
}
public static String getVersion() {
return Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3];
}
}