diff --git a/src/main/java/com/loganmagnan/eventcore/EventCore.java b/src/main/java/com/loganmagnan/eventcore/EventCore.java index 232717f..597797e 100644 --- a/src/main/java/com/loganmagnan/eventcore/EventCore.java +++ b/src/main/java/com/loganmagnan/eventcore/EventCore.java @@ -1,9 +1,7 @@ package com.loganmagnan.eventcore; -import com.loganmagnan.eventcore.managers.CooldownManager; -import com.loganmagnan.eventcore.managers.PlayerDataManager; -import com.loganmagnan.eventcore.managers.SpawnManager; -import com.loganmagnan.eventcore.managers.ChatManager; +import com.loganmagnan.eventcore.managers.*; +import com.loganmagnan.eventcore.managers.hotbar.HotBarManager; import com.loganmagnan.eventcore.managers.mongo.MongoManager; import com.loganmagnan.eventcore.menusystem.PlayerMenuUtil; import com.loganmagnan.eventcore.playerdata.PlayerData; @@ -20,6 +18,7 @@ import lombok.Setter; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; import java.util.HashMap; +import java.util.UUID; @Getter @Setter @@ -38,6 +37,8 @@ public class EventCore extends JavaPlugin { private SpawnManager spawnManager; private ChatManager chatManager; private CooldownManager cooldownManager; + private StaffManager staffManager; + private HotBarManager hotBarManager; // Menu System private HashMap playerMenuUtilMap = new HashMap<>(); @@ -73,6 +74,16 @@ public class EventCore extends JavaPlugin { this.playerDataManager.savePlayerData(playerData); } + for (UUID playerInStaffModeUUID : this.staffManager.getPlayersInStaffMode()) { + Player playerInStaffMode = this.getServer().getPlayer(playerInStaffModeUUID); + + if (playerInStaffMode == null) { + continue; + } + + this.staffManager.setStaffMode(playerInStaffMode, false); + } + this.mongoManager.disconnect(); instance = null; @@ -88,6 +99,8 @@ public class EventCore extends JavaPlugin { this.spawnManager = new SpawnManager(); this.chatManager = new ChatManager(); this.cooldownManager = new CooldownManager(); + this.staffManager = new StaffManager(); + this.hotBarManager = new HotBarManager(); } private void loadListeners() { diff --git a/src/main/java/com/loganmagnan/eventcore/commands/StaffModeCommand.java b/src/main/java/com/loganmagnan/eventcore/commands/StaffModeCommand.java new file mode 100644 index 0000000..104eecd --- /dev/null +++ b/src/main/java/com/loganmagnan/eventcore/commands/StaffModeCommand.java @@ -0,0 +1,28 @@ +package com.loganmagnan.eventcore.commands; + +import com.loganmagnan.eventcore.EventCore; +import com.loganmagnan.eventcore.utils.command.BaseCommand; +import com.loganmagnan.eventcore.utils.command.Command; +import com.loganmagnan.eventcore.utils.command.CommandArguments; +import org.bukkit.entity.Player; + +public class StaffModeCommand extends BaseCommand { + + private EventCore main = EventCore.getInstance(); + + @Command(name = "staffmode", permission = "eventcore.command.staffmode", aliases = {"staff", "mode", "sm", "mod", "moderator", "moderatormode"}) + @Override + public void executeAs(CommandArguments command) { + Player player = command.getPlayer(); + + String[] args = command.getArgs(); + + if (args.length == 0) { + if (this.main.getStaffManager().getPlayersInStaffMode().contains(player.getUniqueId())) { + this.main.getStaffManager().setStaffMode(player, false); + } else { + this.main.getStaffManager().setStaffMode(player, true); + } + } + } +} diff --git a/src/main/java/com/loganmagnan/eventcore/listeners/StaffModeListener.java b/src/main/java/com/loganmagnan/eventcore/listeners/StaffModeListener.java new file mode 100644 index 0000000..cea5113 --- /dev/null +++ b/src/main/java/com/loganmagnan/eventcore/listeners/StaffModeListener.java @@ -0,0 +1,181 @@ +package com.loganmagnan.eventcore.listeners; + +import com.loganmagnan.eventcore.EventCore; +import org.bukkit.Material; +import org.bukkit.entity.Entity; +import org.bukkit.entity.HumanEntity; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.event.block.BlockPlaceEvent; +import org.bukkit.event.entity.EntityDamageByEntityEvent; +import org.bukkit.event.inventory.InventoryCreativeEvent; +import org.bukkit.event.inventory.InventoryInteractEvent; +import org.bukkit.event.player.*; +import org.bukkit.inventory.ItemStack; +import java.util.UUID; + +public class StaffModeListener implements Listener { + + private EventCore main = EventCore.getInstance(); + + @EventHandler + public void onPlayerJoin(PlayerJoinEvent event) { + Player player = event.getPlayer(); + + for (Player playerOnline : this.main.getServer().getOnlinePlayers()) { + for (UUID playerVanishedUUID : this.main.getStaffManager().getPlayersVanished()) { + if (player == playerOnline) { + continue; + } + + Player playerVanished = this.main.getServer().getPlayer(playerVanishedUUID); + + if (playerVanished == null) { + continue; + } + + playerOnline.hidePlayer(playerVanished); + } + } + } + + @EventHandler + public void onPlayerQuit(PlayerQuitEvent event) { + Player player = event.getPlayer(); + + if (!this.main.getStaffManager().isPlayerInStaffMode(player)) { + return; + } + + this.main.getStaffManager().setStaffMode(player, false); + } + + @EventHandler + public void onBlockPlace(BlockPlaceEvent event) { + Player player = event.getPlayer(); + + if (!this.main.getStaffManager().isPlayerInStaffMode(player)) { + return; + } + + event.setCancelled(true); + } + + @EventHandler + public void onBlockBreak(BlockBreakEvent event) { + Player player = event.getPlayer(); + + if (!this.main.getStaffManager().isPlayerInStaffMode(player)) { + return; + } + + event.setCancelled(true); + } + + @EventHandler + public void onPlayerPickupItem(PlayerPickupItemEvent event) { + Player player = event.getPlayer(); + + if (!this.main.getStaffManager().isPlayerInStaffMode(player)) { + return; + } + + event.setCancelled(true); + } + + @EventHandler + public void onPlayerDropItem(PlayerDropItemEvent event) { + Player player = event.getPlayer(); + + if (!this.main.getStaffManager().isPlayerInStaffMode(player)) { + return; + } + + event.setCancelled(true); + } + + @EventHandler + public void onInventoryCreative(InventoryCreativeEvent event) { + HumanEntity humanEntity = event.getWhoClicked(); + + if (!(humanEntity instanceof Player)) { + return; + } + + Player player = (Player) humanEntity; + + if (!this.main.getStaffManager().isPlayerInStaffMode(player)) { + return; + } + + event.setCancelled(true); + } + + @EventHandler + public void onInventoryInteract(InventoryInteractEvent event) { + HumanEntity humanEntity = event.getWhoClicked(); + + if (!(humanEntity instanceof Player)) { + return; + } + + Player player = (Player) humanEntity; + + if (!this.main.getStaffManager().isPlayerInStaffMode(player)) { + return; + } + + event.setCancelled(true); + } + + @EventHandler + public void onPlayerInteract(PlayerInteractEvent event) { + Player player = event.getPlayer(); + + if (!this.main.getStaffManager().isPlayerInStaffMode(player)) { + return; + } + + event.setCancelled(true); + } + + @EventHandler + public void onPlayerInteractEntity(PlayerInteractEntityEvent event) { + Player player = event.getPlayer(); + + ItemStack itemStack = player.getItemInHand(); + + if (!this.main.getStaffManager().isPlayerInStaffMode(player)) { + return; + } + + if (itemStack.getType() == Material.BOOK) { + return; + } + + if (!(event.getRightClicked() instanceof Player)) { + return; + } + + event.setCancelled(true); + } + + @EventHandler + public void onEntityDamageByEntity(EntityDamageByEntityEvent event) { + Entity entity = event.getEntity(); + + if (!(entity instanceof Player)) { + return; + } + + Player player = (Player) entity; + + if (!this.main.getStaffManager().isPlayerVanished(player)) { + return; + } + + event.setCancelled(true); + } +} diff --git a/src/main/java/com/loganmagnan/eventcore/managers/EventManager.java b/src/main/java/com/loganmagnan/eventcore/managers/EventManager.java new file mode 100644 index 0000000..aaad304 --- /dev/null +++ b/src/main/java/com/loganmagnan/eventcore/managers/EventManager.java @@ -0,0 +1,14 @@ +package com.loganmagnan.eventcore.managers; + +import com.loganmagnan.eventcore.EventCore; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class EventManager { + + private EventCore main = EventCore.getInstance(); + + // To Do: Event Management System +} diff --git a/src/main/java/com/loganmagnan/eventcore/managers/StaffManager.java b/src/main/java/com/loganmagnan/eventcore/managers/StaffManager.java new file mode 100644 index 0000000..df638ab --- /dev/null +++ b/src/main/java/com/loganmagnan/eventcore/managers/StaffManager.java @@ -0,0 +1,95 @@ +package com.loganmagnan.eventcore.managers; + +import com.loganmagnan.eventcore.EventCore; +import com.loganmagnan.eventcore.managers.hotbar.types.StaffModeHotBar; +import com.loganmagnan.eventcore.utils.CachedInventory; +import com.loganmagnan.eventcore.utils.ColorUtils; +import lombok.Getter; +import lombok.Setter; +import org.bukkit.GameMode; +import org.bukkit.entity.Player; + +import java.util.*; + +@Getter +@Setter +public class StaffManager { + + private EventCore main = EventCore.getInstance(); + + private List playersInStaffMode = new ArrayList(); + private List playersVanished = new ArrayList(); + private List playersHidingStaff = new ArrayList(); + + private Map cachedInventories = new HashMap(); + + private StaffModeHotBar staffModeHotBar; + + public StaffManager() { + this.staffModeHotBar = new StaffModeHotBar("staff-mode"); + this.main.getHotBarManager().addHotBar(this.staffModeHotBar); + } + + public boolean isPlayerInStaffMode(Player player) { + return this.playersInStaffMode.contains(player.getUniqueId()); + } + + public boolean isPlayerVanished(Player player) { + return this.playersVanished.contains(player.getUniqueId()); + } + + public void setStaffMode(Player player, boolean enabled) { + if (enabled) { + if (!this.playersInStaffMode.contains(player.getUniqueId())) { + for (Player playerOnline : this.main.getServer().getOnlinePlayers()) { + player.showPlayer(playerOnline); + } + } + + this.setVanish(player, true); + this.playersInStaffMode.add(player.getUniqueId()); + this.cachedInventories.put(player.getUniqueId(), new CachedInventory(player, false)); + this.staffModeHotBar.applyToPlayer(player, true); + + player.getInventory().clear(); + player.getInventory().setArmorContents(null); + player.setGameMode(GameMode.CREATIVE); + player.sendMessage(ColorUtils.getMessageType("&bYou've &aenabled &bstaff mode")); + } else { + this.setVanish(player, false); + this.playersInStaffMode.remove(player.getUniqueId()); + this.cachedInventories.get(player.getUniqueId()).applyToPlayer(player, false); + + player.setGameMode(GameMode.SURVIVAL); + player.sendMessage(ColorUtils.getMessageType("&bYou've &cdisabled &bstaff mode")); + } + } + + public void setVanish(Player player, boolean enabled) { + if (enabled) { + if (!this.playersVanished.contains(player.getUniqueId())) { + this.playersVanished.add(player.getUniqueId()); + } + + for (Player playerOnline : this.main.getServer().getOnlinePlayers()) { + if (!this.isPlayerInStaffMode(playerOnline)) { + playerOnline.hidePlayer(player); + + continue; + } + + if (this.playersHidingStaff.contains(playerOnline.getUniqueId())) { + playerOnline.hidePlayer(player); + } + } + } else { + if (this.playersVanished.contains(player.getUniqueId())) { + this.playersVanished.remove(player.getUniqueId()); + } + + for (Player playerOnline : this.main.getServer().getOnlinePlayers()) { + playerOnline.showPlayer(player); + } + } + } +} diff --git a/src/main/java/com/loganmagnan/eventcore/managers/hotbar/ClickHandler.java b/src/main/java/com/loganmagnan/eventcore/managers/hotbar/ClickHandler.java new file mode 100644 index 0000000..09a48e5 --- /dev/null +++ b/src/main/java/com/loganmagnan/eventcore/managers/hotbar/ClickHandler.java @@ -0,0 +1,8 @@ +package com.loganmagnan.eventcore.managers.hotbar; + +import org.bukkit.entity.Player; + +public interface ClickHandler { + + void click(Player player); +} diff --git a/src/main/java/com/loganmagnan/eventcore/managers/hotbar/ClickableItem.java b/src/main/java/com/loganmagnan/eventcore/managers/hotbar/ClickableItem.java new file mode 100644 index 0000000..2a5ebf4 --- /dev/null +++ b/src/main/java/com/loganmagnan/eventcore/managers/hotbar/ClickableItem.java @@ -0,0 +1,79 @@ +package com.loganmagnan.eventcore.managers.hotbar; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import lombok.Setter; +import org.bukkit.inventory.ItemStack; + +import java.util.Objects; + +@Getter +@Setter +@AllArgsConstructor +@RequiredArgsConstructor +public class ClickableItem { + + private ClickHandler clickHandler; + + private ItemStack itemStack; + + private boolean moveable = false; + private boolean placeable = false; + private boolean droppable = false; + + @Override + public boolean equals(Object object) { + if (object == this) { + return true; + } + + if (!(object instanceof ClickableItem)) { + return false; + } + + ClickableItem clickableItem = (ClickableItem) object; + + if (!clickableItem.canEqual(this)) { + return false; + } + + Object thisClickHandler = this.clickHandler; + Object otherClickHandler = clickableItem.getClickHandler(); + + if (!Objects.equals(thisClickHandler, otherClickHandler)) { + return false; + } + + Object thisItemStack = getItemStack(); + Object otherItemStack = clickableItem.getItemStack(); + + return (Objects.equals(thisItemStack, otherItemStack)) && (this.droppable == clickableItem.isDroppable() && (this.moveable == clickableItem.isMoveable() && (this.placeable == clickableItem.isPlaceable()))); + } + + @Override + public int hashCode() { + int result = 1; + + Object clickHandler = this.clickHandler; + + result = result * 59 + ((clickHandler == null) ? 43 : clickHandler.hashCode()); + + Object itemStack = this.itemStack; + + result = result * 59 + ((itemStack == null) ? 43 : itemStack.hashCode()); + result = result * 59 + (this.droppable ? 79 : 97); + result = result * 59 + (this.moveable ? 79 : 97); + + return result * 59 + (this.placeable ? 79 : 97); + } + + protected boolean canEqual(Object object) { + return object instanceof ClickableItem; + } + + @Override + public String toString() { + return "ClickableItem(clickHandler=" + this.clickHandler + ", itemStack=" + this.itemStack + ", droppable=" + this.droppable + ", moveable=" + this.moveable + ", placeable=" + this.placeable + ")"; + } +} diff --git a/src/main/java/com/loganmagnan/eventcore/managers/hotbar/HotBar.java b/src/main/java/com/loganmagnan/eventcore/managers/hotbar/HotBar.java new file mode 100644 index 0000000..e38b386 --- /dev/null +++ b/src/main/java/com/loganmagnan/eventcore/managers/hotbar/HotBar.java @@ -0,0 +1,71 @@ +package com.loganmagnan.eventcore.managers.hotbar; + +import com.loganmagnan.eventcore.EventCore; +import lombok.Getter; +import lombok.Setter; +import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitRunnable; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +@Getter +@Setter +public abstract class HotBar { + + private EventCore main = EventCore.getInstance(); + + private Map cachedItems = new ConcurrentHashMap(); + + private String id; + + public HotBar(String id) { + this.id = id; + } + + public ClickableItem getClickableItem(String id) { + return this.cachedItems.get(id); + } + + public void addClickableItem(String id, ClickableItem clickableItem) { + this.cachedItems.put(id, clickableItem); + } + + public void removeClickableItem(String id) { + this.cachedItems.remove(id); + } + + public void applyToPlayer(Player player, ClickableItem clickableItem, int slot) { + player.getInventory().setItem(slot, null); + player.getInventory().setItem(slot, clickableItem.getItemStack()); + + (new BukkitRunnable() { + @Override + public void run() { + player.updateInventory(); + } + }).runTaskLater(this.main, 1); + } + + public void applyToPlayer(Player player, boolean clearHotBar) { + if (clearHotBar) { + for (int i = 0; i < 8; i++) { + player.getInventory().setItem(i, null); + } + } + + Map clickableItemsToApply = this.getClickableItemsToApply(player); + + for (int slot : clickableItemsToApply.keySet()) { + player.getInventory().setItem(slot, clickableItemsToApply.get(slot).getItemStack()); + } + + (new BukkitRunnable() { + @Override + public void run() { + player.updateInventory(); + } + }).runTaskLater(this.main, 1); + } + + public abstract Map getClickableItemsToApply(Player player); +} diff --git a/src/main/java/com/loganmagnan/eventcore/managers/hotbar/HotBarManager.java b/src/main/java/com/loganmagnan/eventcore/managers/hotbar/HotBarManager.java new file mode 100644 index 0000000..8f098de --- /dev/null +++ b/src/main/java/com/loganmagnan/eventcore/managers/hotbar/HotBarManager.java @@ -0,0 +1,26 @@ +package com.loganmagnan.eventcore.managers.hotbar; + +import com.loganmagnan.eventcore.EventCore; +import lombok.Getter; +import lombok.Setter; + +import java.util.HashMap; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +@Getter +@Setter +public class HotBarManager { + + private EventCore main = EventCore.getInstance(); + + private Set hotBars = ConcurrentHashMap.newKeySet(); + + public void addHotBar(HotBar hotBar) { + this.hotBars.add(hotBar); + } + + public void removeHotBar(HotBar hotBar) { + this.hotBars.remove(hotBar); + } +} diff --git a/src/main/java/com/loganmagnan/eventcore/managers/hotbar/types/StaffModeHotBar.java b/src/main/java/com/loganmagnan/eventcore/managers/hotbar/types/StaffModeHotBar.java new file mode 100644 index 0000000..305ae6e --- /dev/null +++ b/src/main/java/com/loganmagnan/eventcore/managers/hotbar/types/StaffModeHotBar.java @@ -0,0 +1,79 @@ +package com.loganmagnan.eventcore.managers.hotbar.types; + +import com.loganmagnan.eventcore.EventCore; +import com.loganmagnan.eventcore.managers.hotbar.ClickableItem; +import com.loganmagnan.eventcore.managers.hotbar.HotBar; +import com.loganmagnan.eventcore.utils.ColorUtils; +import com.loganmagnan.eventcore.utils.ItemBuilder; +import lombok.Getter; +import lombok.Setter; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import java.util.HashMap; +import java.util.Map; + +@Getter +@Setter +public class StaffModeHotBar extends HotBar { + + private EventCore main = EventCore.getInstance(); + + public StaffModeHotBar(String id) { + super(id); + + this.registerClickableItems(); + } + + @Override + public Map getClickableItemsToApply(Player player) { + Map clickableItems = new HashMap(); + clickableItems.put(0, this.getClickableItem("teleport-compass")); + clickableItems.put(1, this.getClickableItem("inventory-inspect")); + + if (player.hasPermission("worldedit.wand")) { + clickableItems.put(2, this.getClickableItem("world-edit-wand")); + } + + clickableItems.put(3, this.getClickableItem("better-looking")); + clickableItems.put(7, this.getClickableItem("staff-online")); + clickableItems.put(8, this.getClickableItem("vanish")); + + return clickableItems; + } + + public void registerClickableItems() { + this.addClickableItem("teleport-compass", new ClickableItem(player -> { + + }, (new ItemBuilder(Material.COMPASS).name(ColorUtils.getMessageType("&bTeleport Compass"))).build(), false, false, false)); + + this.addClickableItem("inventory-inspect", new ClickableItem(player -> { + + }, (new ItemBuilder(Material.BOOK).name(ColorUtils.getMessageType("&bInventory Inspect"))).build(), false, false, false)); + + this.addClickableItem("world-edit-wand", new ClickableItem(player -> { + + }, (new ItemBuilder(Material.WOODEN_AXE).name(ColorUtils.getMessageType("&bWorld Edit Wand"))).build(), false, false, false)); + + this.addClickableItem("better-looking", new ClickableItem(player -> { + + }, (new ItemBuilder(Material.RED_CARPET).name(ColorUtils.getMessageType(""))).build(), false, false, false)); + + this.addClickableItem("staff-online", new ClickableItem(player -> { + // To Do: Staff Online System + }, (new ItemBuilder(Material.PLAYER_HEAD).name(ColorUtils.getMessageType("&bStaff Online"))).build(), false, false, false)); + + this.addClickableItem("vanish", new ClickableItem(player -> { + this.applyToPlayer(player, this.getClickableItem("unvanish"), 8); + this.main.getStaffManager().setVanish(player, true); + + player.sendMessage(ColorUtils.getMessageType("&bYou've &aenabled &bvanish")); + }, (new ItemBuilder(Material.LIME_DYE).name(ColorUtils.getMessageType("&bVanish"))).build(), false, false, false)); + + this.addClickableItem("unvanish", new ClickableItem(player -> { + this.applyToPlayer(player, this.getClickableItem("vanish"), 8); + this.main.getStaffManager().setVanish(player, false); + + player.sendMessage(ColorUtils.getMessageType("&bYou've &cdisabled &bvanish")); + }, (new ItemBuilder(Material.GRAY_DYE).name(ColorUtils.getMessageType("&bUnvanish"))).build(), false, false, false)); + } +} diff --git a/src/main/java/com/loganmagnan/eventcore/utils/CachedInventory.java b/src/main/java/com/loganmagnan/eventcore/utils/CachedInventory.java new file mode 100644 index 0000000..914b394 --- /dev/null +++ b/src/main/java/com/loganmagnan/eventcore/utils/CachedInventory.java @@ -0,0 +1,190 @@ +package com.loganmagnan.eventcore.utils; + +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonPrimitive; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import lombok.Getter; +import lombok.Setter; +import org.bukkit.configuration.Configuration; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.scheduler.BukkitRunnable; + +@Getter +@Setter +public class CachedInventory { + + private ItemStack[] cachedInventory; + + private ItemStack[] cachedArmor; + + public CachedInventory() { + + } + + private double health = -1.0D; + + private int totalExperience = -1; + private int food = -1; + + private float exp = -1.0F; + + public CachedInventory(Player player, boolean justContents) { + this.cachedInventory = player.getInventory().getContents(); + this.cachedArmor = player.getInventory().getArmorContents(); + if (!justContents) { + this.health = player.getHealth(); + this.food = player.getFoodLevel(); + this.totalExperience = player.getTotalExperience(); + this.exp = player.getExp(); + } + } + + private void delayedUpdateInventory(final Player player, JavaPlugin plugin) { + (new BukkitRunnable() { + public void run() { + player.updateInventory(); + } + }).runTaskLater((Plugin)plugin, 1); + } + + public static CachedInventory fromPlayer(Player player, boolean justContents) { + return new CachedInventory(player, justContents); + } + + public void applyToPlayer(Player player, boolean justContents) { + player.getInventory().setContents(this.cachedInventory); + player.getInventory().setArmorContents(this.cachedArmor); + if (!justContents) { + if (this.health > 0.0D) + player.setHealth(this.health); + if (this.food != -1) + player.setFoodLevel(this.food); + if (this.totalExperience != -1) + player.setTotalExperience(this.totalExperience); + if (this.exp != -1.0F) + player.setExp(this.exp); + } + } + + public void applyToPlayer(Player player, boolean justContents, JavaPlugin plugin) { + applyToPlayer(player, justContents); + delayedUpdateInventory(player, plugin); + } + + public static CachedInventory fromConfigurationSection(ConfigurationSection section) { + CachedInventory cachedInventory = new CachedInventory(); + List armorArray = section.getStringList("armor"); + List armor = new ArrayList<>(); + armorArray.forEach(armorElement -> { + try { + armor.add(ItemBuilder.itemFrom64(armorElement)); + } catch (IOException e) { + e.printStackTrace(); + } + }); + cachedInventory.setCachedArmor((ItemStack[])armor.stream().toArray(x$0 -> new ItemStack[x$0])); + List invArray = section.getStringList("inv"); + List inv = new ArrayList<>(); + invArray.forEach(invElement -> { + try { + inv.add(ItemBuilder.itemFrom64(invElement)); + } catch (IOException e) { + e.printStackTrace(); + } + }); + cachedInventory.setCachedInventory((ItemStack[])inv.stream().toArray(x$0 -> new ItemStack[x$0])); + if (section.contains("health")) + cachedInventory.setHealth(section.getDouble("health")); + if (section.contains("totalExperience")) + cachedInventory.setHealth(section.getInt("totalExperience")); + if (section.contains("food")) + cachedInventory.setHealth(section.getInt("food")); + if (section.contains("exp")) + cachedInventory.setHealth(section.getInt("exp")); + return cachedInventory; + } + + public ConfigurationSection toConfigurationSection(Configuration configuration, String sectionName) { + ConfigurationSection section = configuration.createSection(sectionName); + if (this.health != -1.0D) + section.set("health", Double.valueOf(this.health)); + if (this.totalExperience != -1) + section.set("totalExperience", Integer.valueOf(this.totalExperience)); + if (this.food != -1) + section.set("food", Integer.valueOf(this.food)); + if (this.exp != -1.0F) + section.set("exp", Float.valueOf(this.exp)); + List armor = new ArrayList<>(); + for (ItemStack armorItem : getCachedArmor()) + armor.add(ItemBuilder.itemTo64(armorItem)); + section.set("armor", armor); + List inv = new ArrayList<>(); + for (ItemStack invItem : getCachedInventory()) + inv.add(ItemBuilder.itemTo64(invItem)); + section.set("inv", inv); + return section; + } + + public static CachedInventory fromJson(JsonObject jsonObject) { + CachedInventory cachedInventory = new CachedInventory(); + JsonArray armorArray = jsonObject.get("armor").getAsJsonArray(); + List armor = new ArrayList<>(); + armorArray.forEach(armorElement -> { + try { + armor.add(ItemBuilder.itemFrom64(armorElement.getAsString())); + } catch (IOException e) { + e.printStackTrace(); + } + }); + cachedInventory.setCachedArmor(armor.toArray(new ItemStack[0])); + JsonArray invArray = jsonObject.get("inv").getAsJsonArray(); + List inv = new ArrayList<>(); + invArray.forEach(invElement -> { + try { + inv.add(ItemBuilder.itemFrom64(invElement.getAsString())); + } catch (IOException e) { + e.printStackTrace(); + } + }); + cachedInventory.setCachedInventory(inv.toArray(new ItemStack[0])); + if (jsonObject.has("health")) + cachedInventory.setHealth(jsonObject.get("health").getAsDouble()); + if (jsonObject.has("totalExperience")) + cachedInventory.setHealth(jsonObject.get("totalExperience").getAsInt()); + if (jsonObject.has("food")) + cachedInventory.setHealth(jsonObject.get("food").getAsInt()); + if (jsonObject.has("exp")) + cachedInventory.setHealth(jsonObject.get("exp").getAsFloat()); + return cachedInventory; + } + + public JsonObject toJson() { + JsonObject jsonObject = new JsonObject(); + if (this.health != -1.0D) + jsonObject.addProperty("health", this.health); + if (this.totalExperience != -1) + jsonObject.addProperty("totalExperience", this.totalExperience); + if (this.food != -1) + jsonObject.addProperty("food", this.food); + if (this.exp != -1.0F) + jsonObject.addProperty("exp", this.exp); + JsonArray armorArray = new JsonArray(); + for (ItemStack itemStack : this.cachedArmor) + armorArray.add(new JsonPrimitive(ItemBuilder.itemTo64(itemStack))); + jsonObject.add("armor", armorArray); + JsonArray invArray = new JsonArray(); + for (ItemStack itemStack : this.cachedInventory) + invArray.add(new JsonPrimitive(ItemBuilder.itemTo64(itemStack))); + jsonObject.add("inv", invArray); + return jsonObject; + } +} \ No newline at end of file diff --git a/src/main/java/com/loganmagnan/eventcore/utils/ItemBuilder.java b/src/main/java/com/loganmagnan/eventcore/utils/ItemBuilder.java index 095cf20..d78a7b9 100644 --- a/src/main/java/com/loganmagnan/eventcore/utils/ItemBuilder.java +++ b/src/main/java/com/loganmagnan/eventcore/utils/ItemBuilder.java @@ -11,7 +11,13 @@ import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.LeatherArmorMeta; import org.bukkit.inventory.meta.SkullMeta; import org.bukkit.material.MaterialData; +import org.bukkit.util.io.BukkitObjectInputStream; +import org.bukkit.util.io.BukkitObjectOutputStream; +import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -178,6 +184,32 @@ public class ItemBuilder implements Listener { return this; } + public static String itemTo64(ItemStack stack) throws IllegalStateException { + try { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream); + dataOutput.writeObject(stack); + dataOutput.close(); + return Base64Coder.encodeLines(outputStream.toByteArray()); + } catch (Exception e) { + throw new IllegalStateException("Unable to save item stack.", e); + } + } + + public static ItemStack itemFrom64(String data) throws IOException { + try { + ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data)); + BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream); + try { + return (ItemStack)dataInput.readObject(); + } finally { + dataInput.close(); + } + } catch (ClassNotFoundException e) { + throw new IOException("Unable to decode class type.", e); + } + } + public ItemStack build() { return is; } diff --git a/src/main/resources/messages.yml b/src/main/resources/messages.yml index bce7dc3..1ef7333 100644 --- a/src/main/resources/messages.yml +++ b/src/main/resources/messages.yml @@ -16,6 +16,9 @@ MESSAGES: INVALID-AMOUNT: "&cEnter a valid amount of time to slow the chat for" SET-SPAWN: # Permission Node - eventcore.command.setspawn SPAWN-SET: "&aSet the spawn location" + STAFF-MODE: # Permission Node - eventcore.command.staffmode + ENABLED: "&bYou've &aenabled &bstaff mode" + DISABLED: "&bYou've &cdisabled &bstaff mode" LISTENERS: CHAT-MUTED: "&cChat is muted" CHAT-COOLDOWN: