| @@ -0,0 +1,329 @@ | ||
| package com.turt2live.antishare.inventory; | ||
|
|
||
| import java.io.File; | ||
| import java.sql.PreparedStatement; | ||
| import java.sql.ResultSet; | ||
| import java.sql.SQLException; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
|
|
||
| import org.bukkit.Bukkit; | ||
| import org.bukkit.GameMode; | ||
| import org.bukkit.Material; | ||
| import org.bukkit.World; | ||
| import org.bukkit.enchantments.Enchantment; | ||
| import org.bukkit.entity.Player; | ||
| import org.bukkit.inventory.ItemStack; | ||
| import org.bukkit.inventory.PlayerInventory; | ||
| import org.bukkit.material.MaterialData; | ||
|
|
||
| import com.feildmaster.lib.configuration.EnhancedConfiguration; | ||
| import com.turt2live.antishare.AntiShare; | ||
| import com.turt2live.antishare.storage.SQL; | ||
|
|
||
| /** | ||
| * AntiShare Inventory | ||
| * | ||
| * @author turt2live | ||
| */ | ||
| public class ASInventory { | ||
|
|
||
| /** | ||
| * An enum to represent inventory types | ||
| * | ||
| * @author turt2live | ||
| */ | ||
| public static enum InventoryType{ | ||
| PLAYER("players"), | ||
| REGION("regions"), | ||
| TEMPORARY("temporary"); | ||
|
|
||
| private String relativeFolderName; | ||
|
|
||
| private InventoryType(String relativeFolderName){ | ||
| this.relativeFolderName = relativeFolderName; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the relative folder name | ||
| * | ||
| * @return the folder | ||
| */ | ||
| public String getRelativeFolderName(){ | ||
| return relativeFolderName; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Generates an AntiShare Inventory from a player | ||
| * | ||
| * @param player the player | ||
| * @return the inventory | ||
| */ | ||
| public static ASInventory generate(Player player, InventoryType type){ | ||
| ASInventory inventory = new ASInventory(type, player.getName(), player.getWorld(), player.getGameMode()); | ||
| ItemStack[] contents = player.getInventory().getContents(); | ||
| int slot = 0; | ||
| for(ItemStack item : contents){ | ||
| inventory.set(slot, item); | ||
| slot++; | ||
| } | ||
| return inventory; | ||
| } | ||
|
|
||
| /** | ||
| * Generates an inventory list | ||
| * | ||
| * @param name inventory name | ||
| * @param type the Inventory Type | ||
| * @return the inventories | ||
| */ | ||
| public static List<ASInventory> generateInventory(String name, InventoryType type){ | ||
| // Setup | ||
| List<ASInventory> inventories = new ArrayList<ASInventory>(); | ||
|
|
||
| if(AntiShare.instance.useSQL()){ | ||
| // SQL load | ||
|
|
||
| // Setup | ||
| for(World world : Bukkit.getWorlds()){ | ||
| for(GameMode gamemode : GameMode.values()){ | ||
| try{ | ||
| ResultSet items = AntiShare.instance.getSQL().getQuery(AntiShare.instance.getSQL().getConnection().prepareStatement("SELECT * FROM " + SQL.INVENTORIES_TABLE + " WHERE name='" + name + "' AND type='" + type.name() + "' AND gamemode='" + gamemode.name() + "' AND world='" + world.getName() + "'")); | ||
| ASInventory inventory = new ASInventory(type, name, world, gamemode); | ||
|
|
||
| // Get items | ||
| if(items != null){ | ||
| while (items.next()){ | ||
| int slot = items.getInt("slot"); | ||
|
|
||
| // Item properties | ||
| int id = items.getInt("itemID"); | ||
| String durability = items.getString("itemDurability"); | ||
| int amount = items.getInt("itemAmount"); | ||
| byte data = Byte.parseByte(items.getString("itemData")); | ||
|
|
||
| // Create item | ||
| ItemStack item = new ItemStack(id); | ||
| item.setAmount(amount); | ||
| MaterialData itemData = item.getData(); | ||
| itemData.setData(data); | ||
| item.setData(itemData); | ||
| item.setDurability(Short.parseShort(durability)); | ||
| String enchants[] = items.getString("itemEnchant").split(" "); | ||
| if(items.getString("itemEnchant").length() > 0){ | ||
| for(String enchant : enchants){ | ||
| String parts[] = enchant.split("\\|"); | ||
| String enchantID = parts[0]; | ||
| int level = Integer.parseInt(parts[1]); | ||
| Enchantment e = Enchantment.getById(Integer.parseInt(enchantID)); | ||
| item.addEnchantment(e, level); | ||
| } | ||
| } | ||
|
|
||
| // Set | ||
| inventory.set(slot, item); | ||
| } | ||
| } | ||
|
|
||
| // Save item to map | ||
| inventories.add(inventory); | ||
| }catch(SQLException e){ | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
| } | ||
| }else{ | ||
| // Flat-File (YAML) load | ||
|
|
||
| // Setup | ||
| File dir = new File(AntiShare.instance.getDataFolder(), "inventories" + File.separator + type.getRelativeFolderName()); | ||
| dir.mkdirs(); | ||
| File saveFile = new File(dir, name + ".yml"); | ||
| if(!saveFile.exists()){ | ||
| return inventories; | ||
| } | ||
| EnhancedConfiguration file = new EnhancedConfiguration(saveFile, AntiShare.instance); | ||
| file.load(); | ||
|
|
||
| // Load data | ||
| // Structure: yml:world.gamemode.slot.properties | ||
| for(String world : file.getKeys(false)){ | ||
| for(String gamemode : file.getConfigurationSection(world).getKeys(false)){ | ||
| ASInventory inventory = new ASInventory(type, name, Bukkit.getWorld(world), GameMode.valueOf(gamemode)); | ||
| for(String strSlot : file.getConfigurationSection(world + "." + gamemode).getKeys(false)){ | ||
| Integer slot = Integer.valueOf(strSlot); | ||
| inventory.set(slot, file.getItemStack(world + "." + gamemode + "." + strSlot)); | ||
| } | ||
| inventories.add(inventory); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // return | ||
| return inventories; | ||
| } | ||
|
|
||
| private ConcurrentHashMap<Integer, ItemStack> inventory = new ConcurrentHashMap<Integer, ItemStack>(); | ||
| private AntiShare plugin; | ||
| private InventoryType type = InventoryType.PLAYER; | ||
| private String inventoryName = "UNKNOWN"; | ||
| private World world; | ||
| private GameMode gamemode; | ||
|
|
||
| /** | ||
| * Creates a new AntiShare Inventory | ||
| */ | ||
| public ASInventory(InventoryType type, String inventoryName, World world, GameMode gamemode){ | ||
| plugin = AntiShare.instance; | ||
| this.type = type; | ||
| this.inventoryName = inventoryName; | ||
| this.world = world; | ||
| this.gamemode = gamemode; | ||
| } | ||
|
|
||
| /** | ||
| * Sets a slot to an item | ||
| * | ||
| * @param slot the slot | ||
| * @param item the item | ||
| */ | ||
| public void set(int slot, ItemStack item){ | ||
| if(item == null){ | ||
| item = new ItemStack(Material.AIR, 1); | ||
| } | ||
| inventory.put(slot, item); | ||
| } | ||
|
|
||
| /** | ||
| * Sets the player's inventory to this inventory | ||
| * | ||
| * @param player the player | ||
| */ | ||
| @SuppressWarnings ("deprecation") | ||
| public void setTo(Player player){ | ||
| PlayerInventory pInventory = player.getInventory(); | ||
| pInventory.clear(); | ||
| for(Integer slot : inventory.keySet()){ | ||
| ItemStack item = inventory.get(slot); | ||
| if(item == null){ | ||
| inventory.put(slot, new ItemStack(Material.AIR, 1)); | ||
| item = new ItemStack(Material.AIR, 1); | ||
| } | ||
| pInventory.setItem(slot, item); | ||
| } | ||
| player.getInventory().setContents(pInventory.getContents()); | ||
| player.updateInventory(); | ||
| } | ||
|
|
||
| /** | ||
| * Saves the inventory to disk | ||
| */ | ||
| public void save(){ | ||
| if(plugin.useSQL()){ | ||
| // SQL save | ||
|
|
||
| // Loop | ||
| for(Integer slot : inventory.keySet()){ | ||
| try{ | ||
| // Setup | ||
| PreparedStatement statement = plugin.getSQL().getConnection().prepareStatement("INSERT INTO " + SQL.INVENTORIES_TABLE + " (type, name, gamemode, world, slot, itemID, itemName, itemDurability, itemAmount, itemData, itemEnchant) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); | ||
| ItemStack item = inventory.get(slot); | ||
| int itemID = item.getTypeId(); | ||
| String itemName = item.getType().name(); | ||
| int itemDurability = item.getDurability(); | ||
| int itemAmount = item.getAmount(); | ||
| int itemData = item.getData().getData(); | ||
|
|
||
| // Setup enchants | ||
| String enchant = ""; | ||
| Set<Enchantment> enchantsSet = item.getEnchantments().keySet(); | ||
| Map<Enchantment, Integer> enchantsMap = item.getEnchantments(); | ||
| for(Enchantment e : enchantsSet){ | ||
| enchant = enchant + e.getId() + "|" + enchantsMap.get(e) + " "; | ||
| } | ||
| if(enchant.length() > 0){ | ||
| enchant = enchant.substring(0, enchant.length() - 1); | ||
| } | ||
|
|
||
| // Insert into query | ||
| statement.setString(1, type.name()); | ||
| statement.setString(2, inventoryName); | ||
| statement.setString(3, gamemode.name()); | ||
| statement.setString(4, world.getName()); | ||
| statement.setInt(5, slot); | ||
| statement.setInt(6, itemID); | ||
| statement.setString(7, itemName); | ||
| statement.setInt(8, itemDurability); | ||
| statement.setInt(9, itemAmount); | ||
| statement.setInt(10, itemData); | ||
| statement.setString(11, enchant); | ||
|
|
||
| // Save | ||
| plugin.getSQL().insertQuery(statement); | ||
| }catch(SQLException e){ | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
| }else{ | ||
| // Flat-File (YAML) save | ||
|
|
||
| // Setup | ||
| File dir = new File(plugin.getDataFolder(), "inventories" + File.separator + type.getRelativeFolderName()); | ||
| dir.mkdirs(); | ||
| File saveFile = new File(dir, inventoryName + ".yml"); | ||
| EnhancedConfiguration file = new EnhancedConfiguration(saveFile, plugin); | ||
| file.load(); | ||
|
|
||
| // Save data | ||
| // Structure: yml:world.gamemode.slot.properties | ||
| for(Integer slot : inventory.keySet()){ | ||
| if(inventory.get(slot) == null){ | ||
| continue; | ||
| } | ||
| file.set(world.getName() + "." + gamemode.name() + "." + String.valueOf(slot), inventory.get(slot)); | ||
| } | ||
| file.save(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Gets the world of this inventory | ||
| * | ||
| * @return the world | ||
| */ | ||
| public World getWorld(){ | ||
| return world; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the game mode of this inventory | ||
| * | ||
| * @return the game mode | ||
| */ | ||
| public GameMode getGameMode(){ | ||
| return gamemode; | ||
| } | ||
|
|
||
| /** | ||
| * Changes the type of this inventory | ||
| * | ||
| * @param type the new type | ||
| */ | ||
| public void setType(InventoryType type){ | ||
| this.type = type; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the inventory type | ||
| * | ||
| * @return the type | ||
| */ | ||
| public InventoryType getType(){ | ||
| return type; | ||
| } | ||
|
|
||
| } |
| @@ -0,0 +1,311 @@ | ||
| package com.turt2live.antishare.inventory; | ||
|
|
||
| import java.io.File; | ||
| import java.util.List; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.logging.Level; | ||
|
|
||
| import org.bukkit.Bukkit; | ||
| import org.bukkit.GameMode; | ||
| import org.bukkit.Material; | ||
| import org.bukkit.World; | ||
| import org.bukkit.entity.Player; | ||
| import org.bukkit.inventory.ItemStack; | ||
|
|
||
| import com.turt2live.antishare.AntiShare; | ||
| import com.turt2live.antishare.AntiShare.LogType; | ||
| import com.turt2live.antishare.inventory.ASInventory.InventoryType; | ||
| import com.turt2live.antishare.regions.ASRegion; | ||
| import com.turt2live.antishare.storage.SQL; | ||
|
|
||
| /** | ||
| * Manages inventories within AntiShare | ||
| * | ||
| * @author turt2live | ||
| */ | ||
| public class InventoryManager { | ||
|
|
||
| private ConcurrentHashMap<String, ASInventory> creative = new ConcurrentHashMap<String, ASInventory>(); | ||
| private ConcurrentHashMap<String, ASInventory> survival = new ConcurrentHashMap<String, ASInventory>(); | ||
| private ConcurrentHashMap<String, ASInventory> region = new ConcurrentHashMap<String, ASInventory>(); | ||
| private ConcurrentHashMap<String, TemporaryASInventory> playerTemp = new ConcurrentHashMap<String, TemporaryASInventory>(); | ||
|
|
||
| /** | ||
| * Creates a new Inventory Manager | ||
| */ | ||
| public InventoryManager(){ | ||
| load(); | ||
| } | ||
|
|
||
| /** | ||
| * Loads a specific player from file | ||
| * | ||
| * @param player the player | ||
| */ | ||
| public void loadPlayer(Player player){ | ||
| // Standard inventories | ||
| List<ASInventory> list = ASInventory.generateInventory(player.getName(), InventoryType.PLAYER); | ||
| for(ASInventory inventory : list){ | ||
| World world = inventory.getWorld(); | ||
| GameMode gamemode = inventory.getGameMode(); | ||
| switch (gamemode){ | ||
| case CREATIVE: | ||
| creative.put(player.getName() + "." + world.getName(), inventory); | ||
| break; | ||
| case SURVIVAL: | ||
| survival.put(player.getName() + "." + world.getName(), inventory); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| // Temporary inventories | ||
| list = ASInventory.generateInventory(player.getName(), InventoryType.TEMPORARY); | ||
| for(ASInventory inventory : list){ | ||
| TemporaryASInventory spec = new TemporaryASInventory(ASInventory.generate(player, InventoryType.PLAYER), inventory); | ||
| playerTemp.put(player.getName(), spec); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Cleans up player data | ||
| * | ||
| * @param player the player | ||
| */ | ||
| public void releasePlayer(Player player){ | ||
| // Release | ||
| if(isInTemporary(player)){ | ||
| removeFromTemporary(player); | ||
| } | ||
|
|
||
| // Save | ||
| switch (player.getGameMode()){ | ||
| case CREATIVE: | ||
| saveCreativeInventory(player, player.getWorld()); | ||
| break; | ||
| case SURVIVAL: | ||
| saveSurvivalInventory(player, player.getWorld()); | ||
| break; | ||
| } | ||
|
|
||
| // Cleanup | ||
| for(World world : Bukkit.getWorlds()){ | ||
| if(creative.get(player.getName() + "." + world.getName()) != null) | ||
| creative.get(player.getName() + "." + world.getName()).save(); | ||
| if(survival.get(player.getName() + "." + world.getName()) != null) | ||
| survival.get(player.getName() + "." + world.getName()).save(); | ||
| creative.remove(player.getName() + "." + world.getName()); | ||
| survival.remove(player.getName() + "." + world.getName()); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Sets a player to a temporary inventory, such as a region inventory | ||
| * | ||
| * @param player the player | ||
| * @param inventory the temporary inventory | ||
| */ | ||
| @SuppressWarnings ("deprecation") | ||
| public void setToTemporary(Player player, ASInventory inventory){ | ||
| // Save current inventory | ||
| switch (player.getGameMode()){ | ||
| case CREATIVE: | ||
| saveCreativeInventory(player, player.getWorld()); | ||
| break; | ||
| case SURVIVAL: | ||
| saveSurvivalInventory(player, player.getWorld()); | ||
| break; | ||
| } | ||
|
|
||
| // Set to temp | ||
| TemporaryASInventory spec = new TemporaryASInventory(ASInventory.generate(player, InventoryType.PLAYER), inventory); | ||
| playerTemp.put(player.getName(), spec); | ||
| if(inventory == null){ | ||
| player.getInventory().clear(); | ||
| player.updateInventory(); | ||
| }else{ | ||
| inventory.setTo(player); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Removes the player from their temporary inventory, discarding it | ||
| * | ||
| * @param player the player | ||
| */ | ||
| public void removeFromTemporary(Player player){ | ||
| TemporaryASInventory inventory = playerTemp.get(player.getName()); | ||
| if(inventory != null){ | ||
| inventory.getLastInventory().setTo(player); | ||
| playerTemp.remove(player.getName()); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Determines if a player is in a temporary inventory | ||
| * | ||
| * @param player the player | ||
| * @return true if they are in temporary | ||
| */ | ||
| public boolean isInTemporary(Player player){ | ||
| return playerTemp.containsKey(player.getName()); | ||
| } | ||
|
|
||
| /** | ||
| * Gets the temporary inventory handler for a player | ||
| * | ||
| * @param player the player | ||
| * @return the temporary inventory handler, this can be null if the player doesn't have one | ||
| */ | ||
| public TemporaryASInventory getTemporaryInventoryHandler(Player player){ | ||
| return playerTemp.get(player.getName()); | ||
| } | ||
|
|
||
| /** | ||
| * Saves a player's creative inventory | ||
| * | ||
| * @param player the player | ||
| * @param world the world | ||
| */ | ||
| public void saveCreativeInventory(Player player, World world){ | ||
| creative.put(player.getName() + "." + world.getName(), ASInventory.generate(player, InventoryType.PLAYER)); | ||
| } | ||
|
|
||
| /** | ||
| * Saves a player's survival inventory | ||
| * | ||
| * @param player the player | ||
| * @param world the world | ||
| */ | ||
| public void saveSurvivalInventory(Player player, World world){ | ||
| survival.put(player.getName() + "." + world.getName(), ASInventory.generate(player, InventoryType.PLAYER)); | ||
| } | ||
|
|
||
| /** | ||
| * Gets a player's creative inventory | ||
| * | ||
| * @param player the player | ||
| * @return the inventory | ||
| * @param world the world | ||
| */ | ||
| public ASInventory getCreativeInventory(Player player, World world){ | ||
| ASInventory inventory = creative.get(player.getName() + "." + world.getName()); | ||
| if(inventory == null){ | ||
| inventory = new ASInventory(InventoryType.PLAYER, player.getName(), world, player.getGameMode()); | ||
| creative.put(player.getName() + "." + world.getName(), inventory); | ||
| } | ||
| return inventory; | ||
| } | ||
|
|
||
| /** | ||
| * Gets a player's survival inventory | ||
| * | ||
| * @param player the player | ||
| * @return the inventory | ||
| * @param world the world | ||
| */ | ||
| public ASInventory getSurvivalInventory(Player player, World world){ | ||
| ASInventory inventory = survival.get(player.getName() + "." + world.getName()); | ||
| if(inventory == null){ | ||
| inventory = new ASInventory(InventoryType.PLAYER, player.getName(), world, player.getGameMode()); | ||
| survival.put(player.getName() + "." + world.getName(), inventory); | ||
| } | ||
| return inventory; | ||
| } | ||
|
|
||
| /** | ||
| * Gets a region's inventory | ||
| * | ||
| * @param region the region | ||
| * @return the inventory | ||
| */ | ||
| public ASInventory getRegionInventory(ASRegion region){ | ||
| ASInventory inventory = new ASInventory(InventoryType.REGION, region.getUniqueID(), region.getWorld(), region.getGameMode()); | ||
| inventory.set(0, new ItemStack(Material.BOAT)); | ||
| return inventory; | ||
| //return this.region.get(region.getUniqueID()); | ||
| } | ||
|
|
||
| /** | ||
| * Loads the inventory manager | ||
| */ | ||
| public void load(){ | ||
| // Load players | ||
| for(Player player : Bukkit.getOnlinePlayers()){ | ||
| loadPlayer(player); | ||
| } | ||
|
|
||
| // Loads regions | ||
| for(ASRegion region : AntiShare.instance.getRegionManager().getAllRegions()){ | ||
| String UID = region.getUniqueID(); | ||
| List<ASInventory> inventory = ASInventory.generateInventory(UID, InventoryType.REGION); | ||
| if(inventory != null){ | ||
| if(inventory.size() >= 1){ | ||
| region.setInventory(inventory.get(0)); | ||
| }else{ | ||
| region.setInventory(null); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Status | ||
| int loaded = creative.size() + survival.size() + region.size() + playerTemp.size(); | ||
| AntiShare.instance.getMessenger().log("Inventories loaded: " + loaded, Level.INFO, LogType.INFO); | ||
| } | ||
|
|
||
| /** | ||
| * Saves the inventory manager | ||
| */ | ||
| public void save(){ | ||
| // Save players | ||
| for(Player player : Bukkit.getOnlinePlayers()){ | ||
| if(isInTemporary(player)){ | ||
| removeFromTemporary(player); | ||
| } | ||
| switch (player.getGameMode()){ | ||
| case CREATIVE: | ||
| saveCreativeInventory(player, player.getWorld()); | ||
| break; | ||
| case SURVIVAL: | ||
| saveSurvivalInventory(player, player.getWorld()); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| // Clear targets | ||
| if(AntiShare.instance.useSQL()){ | ||
| AntiShare.instance.getSQL().wipeTable(SQL.INVENTORIES_TABLE); | ||
| }else{ | ||
| for(InventoryType type : InventoryType.values()){ | ||
| File dir = new File(AntiShare.instance.getDataFolder(), "inventories" + File.separator + type.getRelativeFolderName()); | ||
| if(dir.listFiles() != null){ | ||
| for(File file : dir.listFiles()){ | ||
| file.delete(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Save inventories | ||
| for(String key : creative.keySet()){ | ||
| creative.get(key).save(); | ||
| } | ||
| for(String key : survival.keySet()){ | ||
| survival.get(key).save(); | ||
| } | ||
| for(String key : region.keySet()){ | ||
| region.get(key).save(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Reloads the inventory manager | ||
| */ | ||
| public void reload(){ | ||
| save(); | ||
| creative.clear(); | ||
| survival.clear(); | ||
| playerTemp.clear(); | ||
| region.clear(); | ||
| load(); | ||
| } | ||
| } |
| @@ -0,0 +1,42 @@ | ||
| package com.turt2live.antishare.inventory; | ||
|
|
||
| /** | ||
| * Class for temporary inventories | ||
| * | ||
| * @author turt2live | ||
| */ | ||
| public class TemporaryASInventory { | ||
|
|
||
| private ASInventory lastInventory; | ||
| private ASInventory tempInventory; | ||
|
|
||
| /** | ||
| * Creates a new temporary inventory | ||
| * | ||
| * @param last the last used inventory | ||
| * @param temp the temporary inventory | ||
| */ | ||
| public TemporaryASInventory(ASInventory last, ASInventory temp){ | ||
| this.lastInventory = last; | ||
| this.tempInventory = temp; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the last used inventory | ||
| * | ||
| * @return the last used inventory | ||
| */ | ||
| public ASInventory getLastInventory(){ | ||
| return lastInventory; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the temporary inventory | ||
| * | ||
| * @return the temporary inventory | ||
| */ | ||
| public ASInventory getTempInventory(){ | ||
| return tempInventory; | ||
| } | ||
|
|
||
| } |
| @@ -0,0 +1,229 @@ | ||
| package com.turt2live.antishare.notification; | ||
|
|
||
| import java.io.BufferedWriter; | ||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
|
|
||
| import org.bukkit.Bukkit; | ||
| import org.bukkit.command.CommandSender; | ||
| import org.bukkit.entity.Player; | ||
|
|
||
| import com.feildmaster.lib.configuration.EnhancedConfiguration; | ||
| import com.turt2live.antishare.ASUtils; | ||
| import com.turt2live.antishare.AntiShare; | ||
| import com.turt2live.antishare.permissions.PermissionNodes; | ||
|
|
||
| /** | ||
| * Alerts people | ||
| * | ||
| * @author turt2live | ||
| */ | ||
| public class Alert { | ||
|
|
||
| /** | ||
| * An enum to represent what type of alert is being sent | ||
| * | ||
| * @author turt2live | ||
| */ | ||
| public static enum AlertType{ | ||
| ILLEGAL, | ||
| LEGAL, | ||
| GENERAL, | ||
| REGION; | ||
| } | ||
|
|
||
| /** | ||
| * An enum to better determine what alerts to send based on configuration | ||
| * | ||
| * @author turt2live | ||
| */ | ||
| public static enum AlertTrigger{ | ||
| BLOCK_BREAK("types.block-break"), | ||
| BLOCK_PLACE("types.block-place"), | ||
| PLAYER_DEATH("types.player-death"), | ||
| ITEM_DROP("types.item-drop"), | ||
| ITEM_PICKUP("types.item-pickup"), | ||
| RIGHT_CLICK("types.right-click"), | ||
| USE_ITEM("types.use-item"), | ||
| CREATIVE_BLOCK("types.creative-block-break"), | ||
| SURVIVAL_BLOCK("types.survival-block-break"), | ||
| HIT_PLAYER("types.hit-player"), | ||
| HIT_MOB("types.hit-mob"), | ||
| COMMAND("types.command"), | ||
| GENERAL("send-general-notifications"); | ||
|
|
||
| private String node; | ||
|
|
||
| private AlertTrigger(String node){ | ||
| this.node = node; | ||
| } | ||
|
|
||
| /** | ||
| * Determines whether or not to show this alert | ||
| * | ||
| * @return true if it should be shown | ||
| */ | ||
| public boolean show(){ | ||
| EnhancedConfiguration notifications = new EnhancedConfiguration(new File(AntiShare.instance.getDataFolder(), "notifications.yml"), AntiShare.instance); | ||
| notifications.loadDefaults(AntiShare.instance.getResource("resources/notifications.yml")); | ||
| if(!notifications.fileExists() || !notifications.checkDefaults()){ | ||
| notifications.saveDefaults(); | ||
| } | ||
| notifications.load(); | ||
| return notifications.getBoolean(node); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A class to represent alert details | ||
| * | ||
| * @author turt2live | ||
| */ | ||
| private class AlertDetails { | ||
| public long admin_last_sent = 0L; | ||
| public long player_last_sent = 0L; | ||
| } | ||
|
|
||
| private ConcurrentHashMap<String, AlertDetails> alerts = new ConcurrentHashMap<String, AlertDetails>(); | ||
| private boolean send = true; | ||
| private boolean toConsole = true; | ||
| private boolean toPlayers = true; | ||
| private boolean sendIllegal = true; | ||
| private boolean sendLegal = false; | ||
| private boolean sendGeneral = false; | ||
|
|
||
| /** | ||
| * Creates a new Alerter | ||
| */ | ||
| public Alert(){ | ||
| reload(); | ||
| } | ||
|
|
||
| /** | ||
| * Sends an alert | ||
| * | ||
| * @param message the message | ||
| * @param sender the sender | ||
| * @param playerMessage the player message | ||
| * @param type the Alert Type | ||
| * @param trigger the Alert Trigger | ||
| */ | ||
| public void alert(String message, CommandSender sender, String playerMessage, AlertType type, AlertTrigger trigger){ | ||
| String hashmapKey = type.name() + message + playerMessage + sender.hashCode(); | ||
| boolean sendToPlayer = true; | ||
| boolean sendToAdmins = true; | ||
|
|
||
| // Determine if we should even send the message | ||
| if(!send || AntiShare.instance.getPermissions().has(sender, PermissionNodes.SILENT_NOTIFICATIONS)){ | ||
| if(type != AlertType.REGION){ | ||
| return; | ||
| }else{ | ||
| sendToAdmins = false; | ||
| } | ||
| } | ||
|
|
||
| // Alert Switch | ||
| switch (type){ | ||
| case ILLEGAL: | ||
| if(!sendIllegal){ | ||
| sendToAdmins = false; | ||
| } | ||
| break; | ||
| case LEGAL: | ||
| if(!sendLegal){ | ||
| sendToAdmins = false; | ||
| } | ||
| break; | ||
| case GENERAL: | ||
| if(!sendGeneral){ | ||
| sendToAdmins = false; | ||
| } | ||
| break; | ||
| case REGION: | ||
| if(!sendGeneral){ | ||
| sendToAdmins = false; | ||
| } | ||
| break; | ||
| } | ||
|
|
||
| // Check who we need to send to | ||
| AlertDetails details = alerts.get(hashmapKey); | ||
| if(details == null){ | ||
| details = new AlertDetails(); | ||
| details.admin_last_sent = System.currentTimeMillis(); | ||
| details.player_last_sent = System.currentTimeMillis(); | ||
| }else{ | ||
| long now = System.currentTimeMillis(); | ||
| if((now - details.admin_last_sent) < 1000){ | ||
| sendToAdmins = false; | ||
| } | ||
| if((now - details.player_last_sent) < 1000){ | ||
| sendToPlayer = false; | ||
| } | ||
| } | ||
|
|
||
| // Check trigger | ||
| if(sendToAdmins){ | ||
| sendToAdmins = trigger.show(); | ||
| } | ||
|
|
||
| // Send if needed | ||
| if(sendToPlayer && (type == AlertType.ILLEGAL || type == AlertType.GENERAL || type == AlertType.REGION)){ | ||
| details.player_last_sent = System.currentTimeMillis(); | ||
| ASUtils.sendToPlayer(sender, playerMessage); | ||
| } | ||
| if(sendToAdmins && toPlayers){ | ||
| details.admin_last_sent = System.currentTimeMillis(); | ||
| for(Player player : Bukkit.getOnlinePlayers()){ | ||
| if(AntiShare.instance.getPermissions().has(player, PermissionNodes.GET_NOTIFICATIONS)){ | ||
| if(!player.getName().equalsIgnoreCase(sender.getName())){ | ||
| ASUtils.sendToPlayer(player, message); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| if(sendToAdmins && toConsole){ | ||
| ASUtils.sendToPlayer(Bukkit.getConsoleSender(), "[" + type.name() + "] " + message); | ||
| } | ||
|
|
||
| // Reinsert (or insert if not found before) into the hashmap | ||
| alerts.put(hashmapKey, details); | ||
| } | ||
|
|
||
| /** | ||
| * Reloads the alerter | ||
| */ | ||
| public void reload(){ | ||
| // Setup configurations | ||
| EnhancedConfiguration notifications = new EnhancedConfiguration(new File(AntiShare.instance.getDataFolder(), "notifications.yml"), AntiShare.instance); | ||
| notifications.loadDefaults(AntiShare.instance.getResource("resources/notifications.yml")); | ||
| if(!notifications.fileExists() || !notifications.checkDefaults()){ | ||
| notifications.saveDefaults(); | ||
| } | ||
| notifications.load(); | ||
|
|
||
| // Setup settings | ||
| send = notifications.getBoolean("send-notifications"); | ||
| toConsole = notifications.getBoolean("send-to-console"); | ||
| toPlayers = notifications.getBoolean("send-to-players"); | ||
| sendIllegal = notifications.getBoolean("send-illegal-notifications"); | ||
| sendLegal = notifications.getBoolean("send-legal-notifications"); | ||
| sendGeneral = notifications.getBoolean("send-general-notifications"); | ||
| } | ||
|
|
||
| /** | ||
| * Prints the entire contents of the notifications.yml to the writer | ||
| * | ||
| * @param out the writer | ||
| * @throws IOException for external handling | ||
| */ | ||
| public void print(BufferedWriter out) throws IOException{ | ||
| EnhancedConfiguration notifications = new EnhancedConfiguration(new File(AntiShare.instance.getDataFolder(), "notifications.yml"), AntiShare.instance); | ||
| notifications.load(); | ||
| for(String key : notifications.getKeys(true)){ | ||
| out.write(key + ": " + (notifications.getString(key).startsWith("MemorySection") ? "" : notifications.getString(key, "")) + "\r\n"); | ||
| } | ||
| } | ||
|
|
||
| } |
| @@ -0,0 +1,64 @@ | ||
| package com.turt2live.antishare.notification; | ||
|
|
||
| import java.io.BufferedWriter; | ||
| import java.io.File; | ||
| import java.io.IOException; | ||
|
|
||
| import com.feildmaster.lib.configuration.EnhancedConfiguration; | ||
| import com.turt2live.antishare.AntiShare; | ||
|
|
||
| /** | ||
| * Represents messages in AntiShare | ||
| * | ||
| * @author turt2live | ||
| */ | ||
| public class Messages { | ||
|
|
||
| private EnhancedConfiguration messages; | ||
|
|
||
| /** | ||
| * Creates a new Message handler | ||
| */ | ||
| public Messages(){ | ||
| reload(); | ||
| } | ||
|
|
||
| /** | ||
| * Reloads messages | ||
| */ | ||
| public void reload(){ | ||
| // Setup configuration | ||
| messages = new EnhancedConfiguration(new File(AntiShare.instance.getDataFolder(), "messages.yml"), AntiShare.instance); | ||
| messages.loadDefaults(AntiShare.instance.getResource("resources/messages.yml")); | ||
| if(!messages.fileExists() || !messages.checkDefaults()){ | ||
| messages.saveDefaults(); | ||
| } | ||
| messages.load(); | ||
| } | ||
|
|
||
| /** | ||
| * Gets a message | ||
| * | ||
| * @param path the message path | ||
| * @return the message | ||
| */ | ||
| public String getMessage(String path){ | ||
| String message = messages.getString(path); | ||
| if(message == null){ | ||
| return "no message"; | ||
| } | ||
| return message; | ||
| } | ||
|
|
||
| /** | ||
| * Prints the entire contents of the messages.yml to the writer | ||
| * | ||
| * @param out the writer | ||
| * @throws IOException for external handling | ||
| */ | ||
| public void print(BufferedWriter out) throws IOException{ | ||
| for(String key : messages.getKeys(true)){ | ||
| out.write(key + ": " + (messages.getString(key).startsWith("MemorySection") ? "" : messages.getString(key, "")) + "\r\n"); | ||
| } | ||
| } | ||
| } |