diff --git a/pom.xml b/pom.xml index a27b79e..da3f8b9 100644 --- a/pom.xml +++ b/pom.xml @@ -1,75 +1,75 @@ - - 4.0.0 - de.iani.cubeside - TreasureChest - 1.3-SNAPSHOT - - UTF-8 - DEV - - - - spigotmc-repo - https://hub.spigotmc.org/nexus/content/groups/public - - - vault-repo - http://nexus.hc.to/content/repositories/pub_releases - - - brokko-repo - https://www.iani.de/nexus/repository/public/ - - - - - org.spigotmc - spigot-api - 1.12-R0.1-SNAPSHOT - provided - - - de.iani.cubeside - PlayerUUIDCache - 1.4.0 - provided - - - net.milkbowl.vault - VaultAPI - 1.6 - provided - - - - - - maven-compiler-plugin - 3.6.1 - - 1.8 - 1.8 - - - - - - src/main/resources - true - - - - - - nexus - Releases - https://www.iani.de/nexus/content/repositories/releases/ - - - nexus - Snapshot - https://www.iani.de/nexus/content/repositories/snapshots/ - - + + 4.0.0 + de.iani.cubeside + TreasureChest + 1.3-SNAPSHOT + + UTF-8 + DEV + + + + spigotmc-repo + https://hub.spigotmc.org/nexus/content/groups/public + + + vault-repo + http://nexus.hc.to/content/repositories/pub_releases + + + brokko-repo + https://www.iani.de/nexus/content/groups/public + + + + + org.spigotmc + spigot-api + 1.12-R0.1-SNAPSHOT + provided + + + de.iani.cubeside + PlayerUUIDCache + 1.4.0 + provided + + + net.milkbowl.vault + VaultAPI + 1.6 + provided + + + + + + maven-compiler-plugin + 3.6.1 + + 1.8 + 1.8 + + + + + + src/main/resources + true + + + + + + nexus + Releases + https://www.iani.de/nexus/content/repositories/releases/ + + + nexus + Snapshot + https://www.iani.de/nexus/content/repositories/snapshots/ + + \ No newline at end of file diff --git a/src/main/java/de/iani/treasurechest/ChestInventoryListener.java b/src/main/java/de/iani/treasurechest/ChestInventoryListener.java index 24858f9..4844d93 100644 --- a/src/main/java/de/iani/treasurechest/ChestInventoryListener.java +++ b/src/main/java/de/iani/treasurechest/ChestInventoryListener.java @@ -1,214 +1,211 @@ -package de.iani.treasurechest; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; - -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.Sound; -import org.bukkit.block.Block; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.Listener; -import org.bukkit.event.block.Action; -import org.bukkit.event.inventory.InventoryAction; -import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.event.inventory.InventoryCloseEvent; -import org.bukkit.event.inventory.InventoryDragEvent; -import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.event.player.PlayerQuitEvent; -import org.bukkit.inventory.Inventory; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; - -public class ChestInventoryListener implements Listener { - private TreasureChest plugin; - - HashMap openInventories = new HashMap(); - - public ChestInventoryListener(TreasureChest plugin) { - this.plugin = plugin; - } - - @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) - public void onChestOpen(PlayerInteractEvent e) { - if (plugin.getChestLocation() == null) { - return; - } - Block b = e.getClickedBlock(); - if (e.getAction() == Action.RIGHT_CLICK_BLOCK && b != null && b.getLocation().equals(plugin.getChestLocation())) { - e.setCancelled(true); - if (!e.getPlayer().hasPermission("treasurechest.access")) { - plugin.sendMessage(e.getPlayer(), "Du hast keinen Zugriff auf die Schatztruhe!"); - return; - } - if (openInventories.containsKey(e.getPlayer())) { - return;// ist schon offen? - } - - PlayerTreasureChestContent content = plugin.getData().getChestContent(e.getPlayer().getUniqueId()); - if (content == null || content.isEmpty()) { - plugin.sendMessage(e.getPlayer(), "Deine Schatztruhe ist leer!"); - return; - } - Inventory inventory = plugin.getServer().createInventory(null, 9 * 6, "Schatztruhe"); - OpenInventoryData openInventory = new OpenInventoryData(b.getLocation(), inventory); - - int pos = 0; - for (TreasureChestItem item : content.getItems()) { - if (pos < 9 * 6) { - ItemStack di = item.getDisplayItem().clone(); - ItemMeta meta = di.getItemMeta(); - ItemStack[] priceList = item.getPriceItems(); - ArrayList lore = new ArrayList<>(); - if (priceList != null) { - for (ItemStack stack : priceList) { - StringBuilder t = new StringBuilder(); - if (stack.getAmount() > 1) { - t.append(stack.getAmount()).append(" "); - } - t.append(TreasureChest.capitalize(stack.getType().name(), true)); - if (stack.getDurability() > 0) { - t.append(':').append(stack.getDurability()); - } - if (stack.getItemMeta().hasDisplayName()) { - t.append(" (benanntes Item)"); - } - lore.add(t.toString()); - } - } - if (item.getPriceMoney() > 0) { - lore.add(plugin.formatMoney(item.getPriceMoney())); - } - meta.setLore(lore); - di.setItemMeta(meta); - inventory.setItem(pos, di); - openInventory.setItemAtPosition(pos, pos); - } - pos += 1; - } - - e.getPlayer().openInventory(inventory); - openInventories.put(e.getPlayer(), openInventory); - e.getPlayer().playSound(b.getLocation(), Sound.BLOCK_CHEST_OPEN, 0.5f, 0.9f); - } - } - - @EventHandler(priority = EventPriority.HIGH) - public void onInventoryClick(InventoryClickEvent event) { - OpenInventoryData openInventory = openInventories.get(event.getWhoClicked()); - if (openInventory == null) { - return; - } - - InventoryAction action = event.getAction(); - switch (action) { - case PICKUP_ALL: - case PICKUP_HALF: - case PICKUP_SOME: - case PICKUP_ONE: { - event.setCancelled(true); - if (event.getRawSlot() < event.getView().getTopInventory().getSize() && event.getWhoClicked() instanceof Player) { - Player player = (Player) event.getWhoClicked(); - - ItemStack[] playerInv = player.getInventory().getContents(); - playerInv = Arrays.copyOf(playerInv, 36); - Inventory clonedPlayerInventory = Bukkit.createInventory(null, 36); - clonedPlayerInventory.setContents(playerInv); - - PlayerTreasureChestContent content = plugin.getData().getChestContent(player.getUniqueId()); - int entryId = openInventory.getEntryAtPosition(event.getRawSlot()); - TreasureChestItem selectedItem = content.getItem(entryId); - if (selectedItem != null) { - ItemStack[] priceList = selectedItem.getPriceItems(); - int priceCount = priceList == null ? 0 : priceList.length; - if (priceCount > 0) { - ItemStack[] temp = new ItemStack[priceCount]; - for (int i = 0; i < priceCount; i++) { - temp[i] = priceList[i].clone(); - } - if (!clonedPlayerInventory.addItem(temp).isEmpty()) { - plugin.sendMessage(player, "Du hast nicht genügend Platz in deinem Inventar!", true); - player.updateInventory(); - return; - } - } - plugin.sendMessage(player, ChatColor.GRAY + "Hole Preis ab:"); - - if (priceCount > 0) { - ItemStack[] temp = new ItemStack[priceCount]; - for (int i = 0; i < priceCount; i++) { - temp[i] = priceList[i].clone(); - } - player.getInventory().addItem(temp); - for (ItemStack stack : priceList) { - StringBuilder t = new StringBuilder(" "); - if (stack.getAmount() > 1) { - t.append(stack.getAmount()).append(" "); - } - t.append(TreasureChest.capitalize(stack.getType().name(), true)); - if (stack.getDurability() > 0) { - t.append(':').append(stack.getDurability()); - } - ItemMeta meta = stack.getItemMeta(); - if (meta.hasDisplayName()) { - t.append(" (").append(meta.getDisplayName()).append(ChatColor.YELLOW).append(")"); - } - plugin.sendMessage(player, t.toString()); - } - } - if (selectedItem.getPriceMoney() > 0) { - plugin.giveMoney(player, selectedItem.getPriceMoney()); - plugin.sendMessage(player, " " + plugin.formatMoney(selectedItem.getPriceMoney())); - } - - plugin.sendMessage(player, ChatColor.GRAY + "Du hast einen Preis abgeholt!"); - - player.playSound(event.getWhoClicked().getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 1f, 1f); - content.removeItem(entryId); - openInventory.getInventory().setItem(event.getRawSlot(), null); - openInventory.removeEntryAtPositionAndShift(event.getRawSlot()); - player.updateInventory(); - } - } - } - case CLONE_STACK: - case COLLECT_TO_CURSOR: - case DROP_ALL_CURSOR: - case DROP_ALL_SLOT: - case DROP_ONE_CURSOR: - case DROP_ONE_SLOT: - case HOTBAR_MOVE_AND_READD: - case HOTBAR_SWAP: - case UNKNOWN: - default: { - Player player = (Player) event.getWhoClicked(); - player.updateInventory(); - event.setCancelled(true); - return; - } - } - } - - @EventHandler(priority = EventPriority.HIGH) - public void onInventoryDrag(InventoryDragEvent event) { - if (openInventories.containsKey(event.getWhoClicked())) { - event.setCancelled(true); - } - } - - @EventHandler - public void onInventoryClose(InventoryCloseEvent e) { - OpenInventoryData data = openInventories.remove(e.getPlayer()); - if (data != null) { - ((Player) e.getPlayer()).playSound(data.getLocation(), Sound.BLOCK_CHEST_CLOSE, 0.5f, 0.9f); - } - } - - @EventHandler - public void onPlayerQuit(PlayerQuitEvent event) { - openInventories.remove(event.getPlayer()); - } -} +package de.iani.treasurechest; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; + +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.Sound; +import org.bukkit.block.Block; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; +import org.bukkit.event.inventory.InventoryAction; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryCloseEvent; +import org.bukkit.event.inventory.InventoryDragEvent; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + +public class ChestInventoryListener implements Listener { + private TreasureChest plugin; + + HashMap openInventories = new HashMap(); + + public ChestInventoryListener(TreasureChest plugin) { + this.plugin = plugin; + } + + @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) + public void onChestOpen(PlayerInteractEvent e) { + if (plugin.getChestLocation() == null) { + return; + } + Block b = e.getClickedBlock(); + if (e.getAction() == Action.RIGHT_CLICK_BLOCK && b != null && b.getLocation().equals(plugin.getChestLocation())) { + e.setCancelled(true); + if (!e.getPlayer().hasPermission("treasurechest.access")) { + plugin.sendMessage(e.getPlayer(), "Du hast keinen Zugriff auf die Schatztruhe!"); + return; + } + if (openInventories.containsKey(e.getPlayer())) { + return;// ist schon offen? + } + + PlayerTreasureChestContent content = plugin.getData().getChestContent(e.getPlayer().getUniqueId()); + if (content == null || content.isEmpty()) { + plugin.sendMessage(e.getPlayer(), "Deine Schatztruhe ist leer!"); + return; + } + Inventory inventory = plugin.getServer().createInventory(null, 9 * 6, "Schatztruhe"); + OpenInventoryData openInventory = new OpenInventoryData(b.getLocation(), inventory); + + int pos = 0; + for (TreasureChestItem item : content.getItems()) { + if (pos < 9 * 6) { + ItemStack di = item.getDisplayItem().clone(); + ItemMeta meta = di.getItemMeta(); + ItemStack[] priceList = item.getPriceItems(); + ArrayList lore = new ArrayList<>(); + if (priceList != null) { + for (ItemStack stack : priceList) { + StringBuilder t = new StringBuilder(); + if (stack.getAmount() > 1) { + t.append(stack.getAmount()).append(" "); + } + t.append(TreasureChest.capitalize(stack.getType().name(), true)); + if (stack.getDurability() > 0) { + t.append(':').append(stack.getDurability()); + } + if (stack.getItemMeta().hasDisplayName()) { + t.append(" (benanntes Item)"); + } + lore.add(t.toString()); + } + } + if (item.getPriceMoney() > 0) { + lore.add(plugin.formatMoney(item.getPriceMoney())); + } + meta.setLore(lore); + di.setItemMeta(meta); + inventory.setItem(pos, di); + openInventory.setItemAtPosition(pos, pos); + } + pos += 1; + } + + e.getPlayer().openInventory(inventory); + openInventories.put(e.getPlayer(), openInventory); + e.getPlayer().playSound(b.getLocation(), Sound.BLOCK_CHEST_OPEN, 0.5f, 0.9f); + } + } + + @EventHandler(priority = EventPriority.HIGH) + public void onInventoryClick(InventoryClickEvent event) { + OpenInventoryData openInventory = openInventories.get(event.getWhoClicked()); + if (openInventory == null) { + return; + } + + InventoryAction action = event.getAction(); + switch (action) { + case PICKUP_ALL: + case PICKUP_HALF: + case PICKUP_SOME: + case PICKUP_ONE: { + event.setCancelled(true); + if (event.getRawSlot() < event.getView().getTopInventory().getSize() && event.getWhoClicked() instanceof Player) { + Player player = (Player) event.getWhoClicked(); + + ItemStack[] playerInv = player.getInventory().getContents(); + playerInv = Arrays.copyOf(playerInv, 36); + Inventory clonedPlayerInventory = Bukkit.createInventory(null, 36); + clonedPlayerInventory.setContents(playerInv); + + PlayerTreasureChestContent content = plugin.getData().getChestContent(player.getUniqueId()); + int entryId = openInventory.getEntryAtPosition(event.getRawSlot()); + TreasureChestItem selectedItem = content.getItem(entryId); + if (selectedItem != null) { + ItemStack[] priceList = selectedItem.getPriceItems(); + int priceCount = priceList == null ? 0 : priceList.length; + if (priceCount > 0) { + ItemStack[] temp = new ItemStack[priceCount]; + for (int i = 0; i < priceCount; i++) { + temp[i] = priceList[i].clone(); + } + if (!clonedPlayerInventory.addItem(temp).isEmpty()) { + plugin.sendMessage(player, "Du hast nicht genügend Platz in deinem Inventar!", true); + player.updateInventory(); + return; + } + } + plugin.sendMessage(player, ChatColor.GRAY + "Du hast folgende Items erhalten:"); + if (priceCount > 0) { + ItemStack[] temp = new ItemStack[priceCount]; + for (int i = 0; i < priceCount; i++) { + temp[i] = priceList[i].clone(); + } + player.getInventory().addItem(temp); + for (ItemStack stack : priceList) { + StringBuilder t = new StringBuilder(" "); + if (stack.getAmount() > 1) { + t.append(stack.getAmount()).append(" "); + } + t.append(TreasureChest.capitalize(stack.getType().name(), true)); + if (stack.getDurability() > 0) { + t.append(':').append(stack.getDurability()); + } + ItemMeta meta = stack.getItemMeta(); + if (meta.hasDisplayName()) { + t.append(" (").append(meta.getDisplayName()).append(ChatColor.YELLOW).append(")"); + } + plugin.sendMessage(player, t.toString()); + } + } + if (selectedItem.getPriceMoney() > 0) { + plugin.giveMoney(player, selectedItem.getPriceMoney()); + plugin.sendMessage(player, " " + plugin.formatMoney(selectedItem.getPriceMoney())); + } + + player.playSound(event.getWhoClicked().getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 1f, 1f); + content.removeItem(entryId); + openInventory.getInventory().setItem(event.getRawSlot(), null); + openInventory.removeEntryAtPositionAndShift(event.getRawSlot()); + player.updateInventory(); + } + } + } + case CLONE_STACK: + case COLLECT_TO_CURSOR: + case DROP_ALL_CURSOR: + case DROP_ALL_SLOT: + case DROP_ONE_CURSOR: + case DROP_ONE_SLOT: + case HOTBAR_MOVE_AND_READD: + case HOTBAR_SWAP: + case UNKNOWN: + default: { + Player player = (Player) event.getWhoClicked(); + player.updateInventory(); + event.setCancelled(true); + return; + } + } + } + + @EventHandler(priority = EventPriority.HIGH) + public void onInventoryDrag(InventoryDragEvent event) { + if (openInventories.containsKey(event.getWhoClicked())) { + event.setCancelled(true); + } + } + + @EventHandler + public void onInventoryClose(InventoryCloseEvent e) { + OpenInventoryData data = openInventories.remove(e.getPlayer()); + if (data != null) { + ((Player) e.getPlayer()).playSound(data.getLocation(), Sound.BLOCK_CHEST_CLOSE, 0.5f, 0.9f); + } + } + + @EventHandler + public void onPlayerQuit(PlayerQuitEvent event) { + openInventories.remove(event.getPlayer()); + } +} diff --git a/src/main/java/de/iani/treasurechest/PlayerTreasureChestContent.java b/src/main/java/de/iani/treasurechest/PlayerTreasureChestContent.java index 0d9d715..15d892a 100644 --- a/src/main/java/de/iani/treasurechest/PlayerTreasureChestContent.java +++ b/src/main/java/de/iani/treasurechest/PlayerTreasureChestContent.java @@ -1,137 +1,137 @@ -package de.iani.treasurechest; - -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.logging.Level; - -import org.bukkit.Material; -import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; - -public class PlayerTreasureChestContent { - private TreasureChest plugin; - - private File file; - - private ArrayList items; - - private long online; - - public PlayerTreasureChestContent(TreasureChest plugin, File file) { - this.plugin = plugin; - this.file = file; - try { - if (file.exists()) { - YamlConfiguration conf = YamlConfiguration.loadConfiguration(file); - - ConfigurationSection prices = conf.getConfigurationSection("prices"); - if (prices != null) { - for (String e : prices.getKeys(false)) { - ConfigurationSection price = prices.getConfigurationSection(e); - if (price != null) { - ItemStack display = price.getItemStack("displayItem"); - if (display == null) { - display = new ItemStack(Material.BEDROCK); - ItemMeta meta = display.getItemMeta(); - meta.setDisplayName("Unset displayitem"); - display.setItemMeta(meta); - } - int priceMoney = price.getInt("priceMoney"); - ArrayList priceItems = new ArrayList<>(); - ConfigurationSection itemPricesSection = price.getConfigurationSection("itemPrices"); - if (itemPricesSection != null) { - for (String pricee : itemPricesSection.getKeys(false)) { - ItemStack priceItem = itemPricesSection.getItemStack(pricee); - if (priceItem != null) { - priceItems.add(priceItem); - } - } - } - if (items == null) { - items = new ArrayList<>(); - } - items.add(new TreasureChestItem(display, priceItems.toArray(new ItemStack[priceItems.size()]), priceMoney)); - } - } - } - } - } catch (Exception e) { - plugin.getLogger().log(Level.SEVERE, "Could not load user chest file: " + file.getName(), e); - } - } - - private void save() { - file.getParentFile().mkdirs(); - YamlConfiguration conf = new YamlConfiguration(); - ConfigurationSection prices = conf.createSection("prices"); - if (items != null) { - int nr = 0; - for (TreasureChestItem i : items) { - ConfigurationSection price = prices.createSection(Integer.toString(nr++)); - price.set("displayItem", i.getDisplayItem().clone()); - price.set("priceMoney", i.getPriceMoney()); - ConfigurationSection itemsSec = price.createSection("itemPrices"); - if (i.getPriceItems() != null) { - int nr2 = 0; - for (ItemStack st : i.getPriceItems()) { - itemsSec.set(Integer.toString(nr2++), st.clone()); - } - } - } - } - try { - conf.save(file); - } catch (IOException e) { - plugin.getLogger().log(Level.SEVERE, "Could not save user chest file: " + file.getName(), e); - } - } - - public void addItem(TreasureChestItem item) { - item = item.clone(); - if (items == null) { - items = new ArrayList<>(); - } - items.add(item); - save(); - } - - public boolean removeItem(int pos) { - if (items == null || items.size() <= pos || pos < 0) { - return false; - } - items.remove(pos); - if (items.size() == 0) { - items = null; - } - save(); - return true; - } - - public boolean isEmpty() { - return items == null || items.isEmpty(); - } - - public List getItems() { - return items == null ? Collections.EMPTY_LIST : Collections.unmodifiableList(items); - } - - public TreasureChestItem getItem(int pos) { - if (items == null || items.size() <= pos || pos < 0) { - return null; - } - return items.get(pos); - } - - public void setOnline(long t) { - online = t; - } - - public boolean isOnline(long t) { - return online == t; - } -} +package de.iani.treasurechest; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.logging.Level; + +import org.bukkit.Material; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + +public class PlayerTreasureChestContent { + private TreasureChest plugin; + + private File file; + + private ArrayList items; + + private long online; + + public PlayerTreasureChestContent(TreasureChest plugin, File file) { + this.plugin = plugin; + this.file = file; + try { + if (file.exists()) { + YamlConfiguration conf = YamlConfiguration.loadConfiguration(file); + + ConfigurationSection prices = conf.getConfigurationSection("prices"); + if (prices != null) { + for (String e : prices.getKeys(false)) { + ConfigurationSection price = prices.getConfigurationSection(e); + if (price != null) { + ItemStack display = price.getItemStack("displayItem"); + if (display == null) { + display = new ItemStack(Material.BEDROCK); + ItemMeta meta = display.getItemMeta(); + meta.setDisplayName("Unset displayitem"); + display.setItemMeta(meta); + } + int priceMoney = price.getInt("priceMoney"); + ArrayList priceItems = new ArrayList<>(); + ConfigurationSection itemPricesSection = price.getConfigurationSection("itemPrices"); + if (itemPricesSection != null) { + for (String pricee : itemPricesSection.getKeys(false)) { + ItemStack priceItem = itemPricesSection.getItemStack(pricee); + if (priceItem != null) { + priceItems.add(priceItem); + } + } + } + if (items == null) { + items = new ArrayList<>(); + } + items.add(new TreasureChestItem(display, priceItems.toArray(new ItemStack[priceItems.size()]), priceMoney)); + } + } + } + } + } catch (Exception e) { + plugin.getLogger().log(Level.SEVERE, "Could not load user chest file: " + file.getName(), e); + } + } + + private void save() { + file.getParentFile().mkdirs(); + YamlConfiguration conf = new YamlConfiguration(); + ConfigurationSection prices = conf.createSection("prices"); + if (items != null) { + int nr = 0; + for (TreasureChestItem i : items) { + ConfigurationSection price = prices.createSection(Integer.toString(nr++)); + price.set("displayItem", i.getDisplayItem().clone()); + price.set("priceMoney", i.getPriceMoney()); + ConfigurationSection itemsSec = price.createSection("itemPrices"); + if (i.getPriceItems() != null) { + int nr2 = 0; + for (ItemStack st : i.getPriceItems()) { + itemsSec.set(Integer.toString(nr2++), st.clone()); + } + } + } + } + try { + conf.save(file); + } catch (IOException e) { + plugin.getLogger().log(Level.SEVERE, "Could not save user chest file: " + file.getName(), e); + } + } + + public void addItem(TreasureChestItem item) { + item = item.clone(); + if (items == null) { + items = new ArrayList<>(); + } + items.add(item); + save(); + } + + public boolean removeItem(int pos) { + if (items == null || items.size() <= pos || pos < 0) { + return false; + } + items.remove(pos); + if (items.size() == 0) { + items = null; + } + save(); + return true; + } + + public boolean isEmpty() { + return items == null || items.isEmpty(); + } + + public List getItems() { + return items == null ? Collections.emptyList() : Collections.unmodifiableList(items); + } + + public TreasureChestItem getItem(int pos) { + if (items == null || items.size() <= pos || pos < 0) { + return null; + } + return items.get(pos); + } + + public void setOnline(long t) { + online = t; + } + + public boolean isOnline(long t) { + return online == t; + } +} diff --git a/src/main/java/de/iani/treasurechest/TreasureChest.java b/src/main/java/de/iani/treasurechest/TreasureChest.java index 7bc5b49..ef77380 100644 --- a/src/main/java/de/iani/treasurechest/TreasureChest.java +++ b/src/main/java/de/iani/treasurechest/TreasureChest.java @@ -1,258 +1,258 @@ -package de.iani.treasurechest; - -import java.util.ArrayList; -import java.util.Random; -import java.util.UUID; -import java.util.logging.Level; - -import org.bukkit.ChatColor; -import org.bukkit.Effect; -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.OfflinePlayer; -import org.bukkit.World; -import org.bukkit.command.CommandSender; -import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; -import org.bukkit.plugin.RegisteredServiceProvider; -import org.bukkit.plugin.java.JavaPlugin; - -import de.iani.playerUUIDCache.CachedPlayer; -import de.iani.playerUUIDCache.PlayerUUIDCache; -import net.milkbowl.vault.economy.Economy; - -public class TreasureChest extends JavaPlugin implements TreasureChestAPI { - private TreasureChestData data; - - private Location chestLocation; - - private Random random; - - // private PlayerUUIDTools uuidTools; - - private boolean hasEconomy; - - private Economy economy; - - private PlayerUUIDCache playerUUIDCache; - - @Override - public void onEnable() { - random = new Random(); - setupEconomy(); - // uuidTools = new PlayerUUIDTools(this); - playerUUIDCache = (PlayerUUIDCache) getServer().getPluginManager().getPlugin("PlayerUUIDCache"); - readConfig(); - // chestLocation = new Location(getServer().getWorlds().get(0), 308, 127, -879); - data = new TreasureChestData(this); - getCommand("treasurechest").setExecutor(new TreasureChestCommandExecutor(this)); - - getServer().getPluginManager().registerEvents(new ChestInventoryListener(this), this); - getServer().getPluginManager().registerEvents(new PlayerEventListener(this), this); - getServer().getScheduler().runTaskTimer(this, new Runnable() { - @Override - public void run() { - createParticles(); - } - }, 1, 1); - - getServer().getScheduler().runTaskTimer(this, new Runnable() { - @Override - public void run() { - doGC(); - } - }, 8456, 140000); - } - - public PlayerUUIDCache getPlayerUUIDCache() { - return playerUUIDCache; - } - - @Override - public boolean addItem(String player, ItemStack displayItem, ItemStack[] items, int money) { - if (!getServer().isPrimaryThread()) { - return false; - } - CachedPlayer id = playerUUIDCache.getPlayerFromNameOrUUID(player, true); - if (id == null) { - return false; - } - return addItem(id.getUUID(), displayItem, items, money); - } - - @Override - public boolean addItem(OfflinePlayer player, ItemStack displayItem, ItemStack[] items, int money) { - if (!getServer().isPrimaryThread()) { - return false; - } - return addItem(player.getUniqueId(), displayItem, items, money); - } - - private boolean addItem(UUID player, ItemStack displayItem, ItemStack[] items, int money) { - if (displayItem == null || displayItem.getAmount() == 0 || displayItem.getType() == Material.AIR) { - return false; - } - ArrayList copied = new ArrayList<>(); - for (ItemStack is : items) { - if (is != null && is.getAmount() > 0 && is.getType() != Material.AIR) { - copied.add(is); - } - } - items = copied.toArray(new ItemStack[copied.size()]); - - PlayerTreasureChestContent content = getData().getChestContent(player); - content.addItem(new TreasureChestItem(displayItem, items, money)); - return true; - } - - protected void doGC() { - data.doGC(); - } - - private void readConfig() { - // saveDefaultConfig(); - FileConfiguration config = getConfig(); - ConfigurationSection section = config.getConfigurationSection("chestLocation"); - if (section != null) { - String worldName = section.getString("world"); - int x = section.getInt("x"); - int y = section.getInt("y"); - int z = section.getInt("z"); - if (worldName != null) { - World world = getServer().getWorld(worldName); - if (world != null) { - chestLocation = new Location(world, x, y, z); - } - } - } - } - - @Override - public void saveConfig() { - FileConfiguration config = getConfig(); - if (chestLocation == null || chestLocation.getWorld() == null) { - config.set("chestLocation", null); - } else { - ConfigurationSection section = config.getConfigurationSection("chestLocation"); - if (section == null) { - section = config.createSection("chestLocation"); - } - section.set("world", chestLocation.getWorld().getName()); - section.set("x", chestLocation.getBlockX()); - section.set("y", chestLocation.getBlockY()); - section.set("z", chestLocation.getBlockZ()); - } - super.saveConfig(); - } - - // public PlayerUUIDTools getUUIDTools() - // { - // return uuidTools; - // } - - public TreasureChestData getData() { - return data; - } - - public Location getChestLocation() { - return chestLocation; - } - - protected void createParticles() { - if (random.nextInt(10) == 0 && chestLocation != null) { - Location playerLocation = new Location(null, 0, 0, 0); - for (Player player : getServer().getOnlinePlayers()) { - player.getLocation(playerLocation); - if (playerLocation.getWorld() == chestLocation.getWorld()) { - double dx = playerLocation.getX() - chestLocation.getX(); - double dy = playerLocation.getY() - chestLocation.getY(); - double dz = playerLocation.getZ() - chestLocation.getZ(); - double dsquared = dx * dx + dy * dy + dz * dz; - if (dsquared < 16 * 16) { - if (!data.getChestContent(player.getUniqueId()).isEmpty()) { - player.playEffect(chestLocation, Effect.MOBSPAWNER_FLAMES, null); - } - } - } - } - } - } - - public void sendMessage(CommandSender p, String text) { - sendMessage(p, text, false); - } - - public void sendMessage(CommandSender p, String text, boolean error) { - p.sendMessage(ChatColor.BLUE + "[TC] " + (error ? ChatColor.DARK_RED : ChatColor.YELLOW) + text); - } - - public void setChestLocation(Location location) { - chestLocation = location; - saveConfig(); - } - - private void setupEconomy() { - try { - RegisteredServiceProvider economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class); - if (economyProvider != null) { - economy = economyProvider.getProvider(); - hasEconomy = true; - } else { - getLogger().severe("Kein Economy-Plugin! Es können keine Preisgelder ausgezahlt werden!"); - } - } catch (Throwable e) { - getLogger().severe("Vault nicht gefunden! Es können keine Preisgelder ausgezahlt werden!"); - } - } - - public boolean hasEconomy() { - return hasEconomy; - } - - public void giveMoney(Player player, int amount) { - if (hasEconomy) { - try { - economy.depositPlayer(player, amount); - } catch (Throwable e) { - getLogger().log(Level.SEVERE, "Error paying money", e); - } - } - } - - public String formatMoney(int amount) { - if (hasEconomy) { - try { - return economy.format(amount); - } catch (Throwable e) { - getLogger().log(Level.SEVERE, "Error formating money", e); - } - } - return Integer.toString(amount); - } - - public static String capitalize(String s, boolean replaceUnderscores) { - char[] cap = s.toCharArray(); - boolean lastSpace = true; - for (int i = 0; i < cap.length; i++) { - if (cap[i] == '_') { - if (replaceUnderscores) { - cap[i] = ' '; - } - lastSpace = true; - } else if (cap[i] >= '0' && cap[i] <= '9') { - lastSpace = true; - } else { - if (lastSpace) { - cap[i] = Character.toUpperCase(cap[i]); - } else { - cap[i] = Character.toLowerCase(cap[i]); - } - lastSpace = false; - } - } - return new String(cap); - } - -} +package de.iani.treasurechest; + +import java.util.ArrayList; +import java.util.Random; +import java.util.UUID; +import java.util.logging.Level; + +import org.bukkit.ChatColor; +import org.bukkit.Effect; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.OfflinePlayer; +import org.bukkit.World; +import org.bukkit.command.CommandSender; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.plugin.RegisteredServiceProvider; +import org.bukkit.plugin.java.JavaPlugin; + +import de.iani.playerUUIDCache.CachedPlayer; +import de.iani.playerUUIDCache.PlayerUUIDCache; +import net.milkbowl.vault.economy.Economy; + +public class TreasureChest extends JavaPlugin implements TreasureChestAPI { + private TreasureChestData data; + + private Location chestLocation; + + private Random random; + + // private PlayerUUIDTools uuidTools; + + private boolean hasEconomy; + + private Economy economy; + + private PlayerUUIDCache playerUUIDCache; + + @Override + public void onEnable() { + random = new Random(); + setupEconomy(); + // uuidTools = new PlayerUUIDTools(this); + playerUUIDCache = (PlayerUUIDCache) getServer().getPluginManager().getPlugin("PlayerUUIDCache"); + readConfig(); + // chestLocation = new Location(getServer().getWorlds().get(0), 308, 127, -879); + data = new TreasureChestData(this); + getCommand("treasurechest").setExecutor(new TreasureChestCommandExecutor(this)); + + getServer().getPluginManager().registerEvents(new ChestInventoryListener(this), this); + getServer().getPluginManager().registerEvents(new PlayerEventListener(this), this); + getServer().getScheduler().runTaskTimer(this, new Runnable() { + @Override + public void run() { + createParticles(); + } + }, 1, 1); + + getServer().getScheduler().runTaskTimer(this, new Runnable() { + @Override + public void run() { + doGC(); + } + }, 8456, 140000); + } + + public PlayerUUIDCache getPlayerUUIDCache() { + return playerUUIDCache; + } + + @Override + public boolean addItem(String player, ItemStack displayItem, ItemStack[] items, int money) { + if (!getServer().isPrimaryThread()) { + return false; + } + CachedPlayer id = playerUUIDCache.getPlayerFromNameOrUUID(player, true); + if (id == null) { + return false; + } + return addItem(id.getUUID(), displayItem, items, money); + } + + @Override + public boolean addItem(OfflinePlayer player, ItemStack displayItem, ItemStack[] items, int money) { + if (!getServer().isPrimaryThread()) { + return false; + } + return addItem(player.getUniqueId(), displayItem, items, money); + } + + private boolean addItem(UUID player, ItemStack displayItem, ItemStack[] items, int money) { + if (displayItem == null || displayItem.getAmount() == 0 || displayItem.getType() == Material.AIR) { + return false; + } + ArrayList copied = new ArrayList<>(); + for (ItemStack is : items) { + if (is != null && is.getAmount() > 0 && is.getType() != Material.AIR) { + copied.add(is.clone()); + } + } + items = copied.toArray(new ItemStack[copied.size()]); + + PlayerTreasureChestContent content = getData().getChestContent(player); + content.addItem(new TreasureChestItem(displayItem, items, money)); + return true; + } + + protected void doGC() { + data.doGC(); + } + + private void readConfig() { + // saveDefaultConfig(); + FileConfiguration config = getConfig(); + ConfigurationSection section = config.getConfigurationSection("chestLocation"); + if (section != null) { + String worldName = section.getString("world"); + int x = section.getInt("x"); + int y = section.getInt("y"); + int z = section.getInt("z"); + if (worldName != null) { + World world = getServer().getWorld(worldName); + if (world != null) { + chestLocation = new Location(world, x, y, z); + } + } + } + } + + @Override + public void saveConfig() { + FileConfiguration config = getConfig(); + if (chestLocation == null || chestLocation.getWorld() == null) { + config.set("chestLocation", null); + } else { + ConfigurationSection section = config.getConfigurationSection("chestLocation"); + if (section == null) { + section = config.createSection("chestLocation"); + } + section.set("world", chestLocation.getWorld().getName()); + section.set("x", chestLocation.getBlockX()); + section.set("y", chestLocation.getBlockY()); + section.set("z", chestLocation.getBlockZ()); + } + super.saveConfig(); + } + + // public PlayerUUIDTools getUUIDTools() + // { + // return uuidTools; + // } + + public TreasureChestData getData() { + return data; + } + + public Location getChestLocation() { + return chestLocation; + } + + protected void createParticles() { + if (random.nextInt(10) == 0 && chestLocation != null && chestLocation.getWorld() != null) { + Location playerLocation = new Location(null, 0, 0, 0); + for (Player player : getServer().getOnlinePlayers()) { + if (player.getWorld() == chestLocation.getWorld()) { + player.getLocation(playerLocation); + double dx = playerLocation.getX() - chestLocation.getX(); + double dy = playerLocation.getY() - chestLocation.getY(); + double dz = playerLocation.getZ() - chestLocation.getZ(); + double dsquared = dx * dx + dy * dy + dz * dz; + if (dsquared < 16 * 16) { + if (!data.getChestContent(player.getUniqueId()).isEmpty()) { + player.playEffect(chestLocation, Effect.MOBSPAWNER_FLAMES, null); + } + } + } + } + } + } + + public void sendMessage(CommandSender p, String text) { + sendMessage(p, text, false); + } + + public void sendMessage(CommandSender p, String text, boolean error) { + p.sendMessage(ChatColor.BLUE + "[TC] " + (error ? ChatColor.DARK_RED : ChatColor.YELLOW) + text); + } + + public void setChestLocation(Location location) { + chestLocation = location; + saveConfig(); + } + + private void setupEconomy() { + try { + RegisteredServiceProvider economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class); + if (economyProvider != null && economyProvider.getProvider() != null) { + economy = economyProvider.getProvider(); + hasEconomy = true; + } else { + getLogger().warning("Kein Economy-Plugin! Es können keine Preisgelder ausgezahlt werden!"); + } + } catch (Throwable e) { + getLogger().warning("Vault nicht gefunden! Es können keine Preisgelder ausgezahlt werden!"); + } + } + + public boolean hasEconomy() { + return hasEconomy; + } + + public void giveMoney(Player player, int amount) { + if (hasEconomy) { + try { + economy.depositPlayer(player, amount); + } catch (Throwable e) { + getLogger().log(Level.SEVERE, "Error paying money", e); + } + } + } + + public String formatMoney(int amount) { + if (hasEconomy) { + try { + return economy.format(amount); + } catch (Throwable e) { + getLogger().log(Level.SEVERE, "Error formating money", e); + } + } + return Integer.toString(amount); + } + + public static String capitalize(String s, boolean replaceUnderscores) { + char[] cap = s.toCharArray(); + boolean lastSpace = true; + for (int i = 0; i < cap.length; i++) { + if (cap[i] == '_') { + if (replaceUnderscores) { + cap[i] = ' '; + } + lastSpace = true; + } else if (cap[i] >= '0' && cap[i] <= '9') { + lastSpace = true; + } else { + if (lastSpace) { + cap[i] = Character.toUpperCase(cap[i]); + } else { + cap[i] = Character.toLowerCase(cap[i]); + } + lastSpace = false; + } + } + return new String(cap); + } + +}