From 15b494b4badeaf7cf6558af0ea37daf5301b806a Mon Sep 17 00:00:00 2001 From: BuildTools Date: Sun, 12 Aug 2018 15:33:54 +0200 Subject: [PATCH] first commit --- .classpath | 11 ++ .project | 17 ++ .settings/org.eclipse.jdt.core.prefs | 11 ++ config.yml | 21 +++ plugin.yml | 13 ++ .../JeffChestSortCommandExecutor.java | 50 ++++++ .../JeffChestSort/JeffChestSortListener.java | 157 ++++++++++++++++++ .../JeffChestSort/JeffChestSortMessages.java | 22 +++ .../JeffChestSortPlayerSetting.java | 15 ++ .../JeffChestSort/JeffChestSortPlugin.java | 37 +++++ 10 files changed, 354 insertions(+) create mode 100644 .classpath create mode 100644 .project create mode 100644 .settings/org.eclipse.jdt.core.prefs create mode 100644 config.yml create mode 100644 plugin.yml create mode 100644 src/de/jeffclan/JeffChestSort/JeffChestSortCommandExecutor.java create mode 100644 src/de/jeffclan/JeffChestSort/JeffChestSortListener.java create mode 100644 src/de/jeffclan/JeffChestSort/JeffChestSortMessages.java create mode 100644 src/de/jeffclan/JeffChestSort/JeffChestSortPlayerSetting.java create mode 100644 src/de/jeffclan/JeffChestSort/JeffChestSortPlugin.java diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..92a93c0 --- /dev/null +++ b/.classpath @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..a0461cf --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + ChestSort + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..bb35fa0 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/config.yml b/config.yml new file mode 100644 index 0000000..d9b2fda --- /dev/null +++ b/config.yml @@ -0,0 +1,21 @@ +# when set to false, new players will have +# to run /chestsort once to enable automatic +# chest sorting. +sorting-enabled-by-default: false + +# when set to true, players with sorting +# disabled will be shown a message on how +# to enable automatic chest sorting when +# they use a chest for the first time. +show-message-when-using-chest: true + +# when set to true, the message is shown again +# when a player logs out and back in and then +# uses a chest again. +show-message-again-after-logout: true + +# localization +message-when-using-chest: "&7Hint: Type &6/chestsort&7 to enable automatic chest sorting." +message-sorting-disabled: "&7Automatic chest sorting has been &cdisabled&7.&r" +message-sorting-enabled: "&7Automatic chest sorting has been &aenabled&7.&r" +message-error-players-only: "&cError: This command can only be run by players.&r" \ No newline at end of file diff --git a/plugin.yml b/plugin.yml new file mode 100644 index 0000000..6522800 --- /dev/null +++ b/plugin.yml @@ -0,0 +1,13 @@ +main: de.jeffclan.JeffChestSort.JeffChestSortPlugin +name: ChestSort +version: 1.3.1 +api-version: 1.13 +description: Allows automatic chest sorting +author: mfnalex +website: www.jeff-media.de +prefix: [ChestSort] +commands: + chestsort: + description: Toggle automatic chest sorting + usage: / + aliases: sort \ No newline at end of file diff --git a/src/de/jeffclan/JeffChestSort/JeffChestSortCommandExecutor.java b/src/de/jeffclan/JeffChestSort/JeffChestSortCommandExecutor.java new file mode 100644 index 0000000..0ad8545 --- /dev/null +++ b/src/de/jeffclan/JeffChestSort/JeffChestSortCommandExecutor.java @@ -0,0 +1,50 @@ +package de.jeffclan.JeffChestSort; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class JeffChestSortCommandExecutor implements CommandExecutor { + + JeffChestSortPlugin plugin; + + JeffChestSortCommandExecutor(JeffChestSortPlugin plugin) + { + this.plugin = plugin; + } + + @Override + public boolean onCommand(CommandSender sender, Command arg1, String arg2, String[] arg3) { + if(!(sender instanceof Player)) + { + return false; + } + + if(arg1.getName().equalsIgnoreCase("chestsort")) + { + if ( !(sender instanceof Player)) { + sender.sendMessage(plugin.msg.MSG_PLAYERSONLY); + return true; + } + + Player p = (Player) sender; + JeffChestSortPlayerSetting setting = plugin.PerPlayerSettings.get(p.getUniqueId().toString()); + setting.sortingEnabled = !setting.sortingEnabled; + + if(setting.sortingEnabled) { + p.sendMessage(plugin.msg.MSG_ACTIVATED); + } else { + p.sendMessage(plugin.msg.MSG_DEACTIVATED); + } + + return true; + + } + + return false; + } + + +} + diff --git a/src/de/jeffclan/JeffChestSort/JeffChestSortListener.java b/src/de/jeffclan/JeffChestSort/JeffChestSortListener.java new file mode 100644 index 0000000..9e68827 --- /dev/null +++ b/src/de/jeffclan/JeffChestSort/JeffChestSortListener.java @@ -0,0 +1,157 @@ +package de.jeffclan.JeffChestSort; + +import java.util.Arrays; +import java.util.UUID; +import java.io.File; +import java.io.IOException; + +import org.bukkit.block.Chest; +import org.bukkit.block.DoubleChest; +import org.bukkit.block.ShulkerBox; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.inventory.InventoryCloseEvent; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; + +public class JeffChestSortListener implements Listener { + + + JeffChestSortPlugin plugin; + + JeffChestSortListener(JeffChestSortPlugin plugin){ + this.plugin = plugin; + } + + @EventHandler + public void onPlayerJoin(PlayerJoinEvent event) { + UUID uniqueId=event.getPlayer().getUniqueId(); + if(!plugin.PerPlayerSettings.containsKey(uniqueId.toString())) { + + File playerFile = new File(plugin.getDataFolder() + File.separator + "playerdata" ,event.getPlayer().getUniqueId().toString() + ".yml"); + YamlConfiguration playerConfig = YamlConfiguration.loadConfiguration(playerFile); + + boolean activeForThisPlayer; + + if(!playerFile.exists()) { + activeForThisPlayer = plugin.getConfig().getBoolean("sorting-enabled-by-default"); + } else { + activeForThisPlayer = playerConfig.getBoolean("sortingEnabled"); + } + + JeffChestSortPlayerSetting newSettings = new JeffChestSortPlayerSetting(activeForThisPlayer); + if (!plugin.getConfig().getBoolean("show-message-again-after-logout")) { + newSettings.hasSeenMessage = playerConfig.getBoolean("hasSeenMessage"); + } + plugin.PerPlayerSettings.put(uniqueId.toString(),newSettings); + + + } + } + + @EventHandler + public void onPlayerQuit(PlayerQuitEvent event) { + UUID uniqueId=event.getPlayer().getUniqueId(); + if(plugin.PerPlayerSettings.containsKey(uniqueId.toString())) { + JeffChestSortPlayerSetting setting = plugin.PerPlayerSettings.get(event.getPlayer().getUniqueId().toString()); + File playerFile = new File(plugin.getDataFolder() + File.separator + "playerdata" ,event.getPlayer().getUniqueId().toString() + ".yml"); + YamlConfiguration playerConfig = YamlConfiguration.loadConfiguration(playerFile); + playerConfig.set("sortingEnabled", setting.sortingEnabled); + playerConfig.set("hasSeenMessage", setting.hasSeenMessage); + try { + playerConfig.save(playerFile); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + plugin.PerPlayerSettings.remove(uniqueId.toString()); + } + } + + @EventHandler + public void onInventoryClose(InventoryCloseEvent event) + { + + if(!(event.getPlayer() instanceof Player)) { + return; + } + + Player p = (Player) event.getPlayer(); + JeffChestSortPlayerSetting setting = plugin.PerPlayerSettings.get(p.getUniqueId().toString()); + + if(!(event.getInventory().getHolder() instanceof Chest) + && !(event.getInventory().getHolder() instanceof DoubleChest) + && !(event.getInventory().getHolder() instanceof ShulkerBox)) + { + return; + } + + if(!plugin.sortingEnabled(p)) { + if(!setting.hasSeenMessage) { + setting.hasSeenMessage = true; + if(plugin.getConfig().getBoolean("show-message-when-using-chest")) { + //p.sendMessage(JeffChestSortMessages.MSG_COMMANDMESSAGE); + p.sendMessage(plugin.msg.MSG_COMMANDMESSAGE); + } + } + return; + } + + Inventory inv = event.getInventory(); + ItemStack[] items = inv.getContents(); + event.getInventory().clear(); + String[] itemList = new String[inv.getSize()]; + + int i=0; + for(ItemStack item : items) + { + if(item!=null) + { + itemList[i] = item.getType().name() + "," + String.valueOf(item.hashCode()); + i++; + } + } + + // count all items that are not null + int count = 0; + for(String s: itemList) + { + if(s!=null) + { + count++; + } + } + + // create new array with just the size we need + String[] shortenedArray = new String[count]; + + // fill new array with items + for(int j=0; j < count; j++) + { + shortenedArray[j] = itemList[j]; + } + + // sort array alphabetically + Arrays.sort(shortenedArray); + + // put everything back in the inventory + for(String s : shortenedArray) + { + //System.out.println(s); + for(ItemStack item : items) { + if(item!=null && s != null) { + if(item.hashCode() == Integer.parseInt(s.split(",")[1])) { + inv.addItem(item); + item = null; + s = null; + } + } + } + } + } + } diff --git a/src/de/jeffclan/JeffChestSort/JeffChestSortMessages.java b/src/de/jeffclan/JeffChestSort/JeffChestSortMessages.java new file mode 100644 index 0000000..5c4eca4 --- /dev/null +++ b/src/de/jeffclan/JeffChestSort/JeffChestSortMessages.java @@ -0,0 +1,22 @@ +package de.jeffclan.JeffChestSort; + +import org.bukkit.ChatColor; + +public class JeffChestSortMessages { + + JeffChestSortPlugin plugin; + + final String MSG_ACTIVATED, MSG_DEACTIVATED, MSG_COMMANDMESSAGE, MSG_PLAYERSONLY; + + JeffChestSortMessages(JeffChestSortPlugin plugin) { + this.plugin=plugin; + + + MSG_ACTIVATED=ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("message-sorting-enabled")); + MSG_DEACTIVATED=ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("message-sorting-disabled")); + MSG_COMMANDMESSAGE=ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("message-when-using-chest")); + MSG_PLAYERSONLY=ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("message-error-players-only")); + } + + +} diff --git a/src/de/jeffclan/JeffChestSort/JeffChestSortPlayerSetting.java b/src/de/jeffclan/JeffChestSort/JeffChestSortPlayerSetting.java new file mode 100644 index 0000000..8c3f0b1 --- /dev/null +++ b/src/de/jeffclan/JeffChestSort/JeffChestSortPlayerSetting.java @@ -0,0 +1,15 @@ +package de.jeffclan.JeffChestSort; + +public class JeffChestSortPlayerSetting { + + // Sorting enabled for this player? + boolean sortingEnabled; + + // Did we already show the message how to activate sorting? + boolean hasSeenMessage = false; + + JeffChestSortPlayerSetting(boolean sortingEnabled) { + this.sortingEnabled = sortingEnabled; + } + +} diff --git a/src/de/jeffclan/JeffChestSort/JeffChestSortPlugin.java b/src/de/jeffclan/JeffChestSort/JeffChestSortPlugin.java new file mode 100644 index 0000000..3bd34d2 --- /dev/null +++ b/src/de/jeffclan/JeffChestSort/JeffChestSortPlugin.java @@ -0,0 +1,37 @@ +package de.jeffclan.JeffChestSort; + +import java.io.File; +import java.util.HashMap; +import java.util.Map; + +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; + +public class JeffChestSortPlugin extends JavaPlugin { + + Map PerPlayerSettings = new HashMap(); + FileConfiguration config = getConfig(); + JeffChestSortMessages msg; + + @Override + public void onEnable() { + createConfig(); + msg = new JeffChestSortMessages(this); + getServer().getPluginManager().registerEvents(new JeffChestSortListener(this), this); + JeffChestSortCommandExecutor commandExecutor = new JeffChestSortCommandExecutor(this); + this.getCommand("chestsort").setExecutor(commandExecutor); + } + + public boolean sortingEnabled(Player p) { + return PerPlayerSettings.get(p.getUniqueId().toString()).sortingEnabled; + } + + void createConfig() { + this.saveDefaultConfig(); + File playerDataFolder = new File(getDataFolder().getPath() + File.separator+"playerdata"); + if(!playerDataFolder.exists()) + playerDataFolder.mkdir(); + } + +}