From c8dca8dd3bd865efa23b3c749264d29fdf4c04f4 Mon Sep 17 00:00:00 2001
From: Jesse Prescott <bleedobsidian@gmail.com>
Date: Thu, 31 May 2018 14:23:22 +0100
Subject: [PATCH] Add create command.

---
 .../bleedobsidian/itemcase/ChatLogger.java    |   4 +-
 .../itemcase/CommandHandler.java              |  95 ++++++++++++++++
 .../bleedobsidian/itemcase/GenericLogger.java |  62 ++++++++++
 .../bleedobsidian/itemcase/ItemCaseCore.java  |  22 ++++
 .../itemcase/commands/Command.java            |  34 ++++++
 .../itemcase/commands/CreateCommand.java      | 107 ++++++++++++++++++
 .../itemcase/managers/ItemcaseManager.java    |   3 +-
 src/main/resources/languages/EN.yml           |  32 +++++-
 src/main/resources/plugin.yml                 |  11 +-
 9 files changed, 365 insertions(+), 5 deletions(-)
 create mode 100644 src/main/java/com/gmail/bleedobsidian/itemcase/CommandHandler.java
 create mode 100644 src/main/java/com/gmail/bleedobsidian/itemcase/GenericLogger.java
 create mode 100644 src/main/java/com/gmail/bleedobsidian/itemcase/commands/Command.java
 create mode 100644 src/main/java/com/gmail/bleedobsidian/itemcase/commands/CreateCommand.java

diff --git a/src/main/java/com/gmail/bleedobsidian/itemcase/ChatLogger.java b/src/main/java/com/gmail/bleedobsidian/itemcase/ChatLogger.java
index 39ed511..051e91b 100644
--- a/src/main/java/com/gmail/bleedobsidian/itemcase/ChatLogger.java
+++ b/src/main/java/com/gmail/bleedobsidian/itemcase/ChatLogger.java
@@ -18,7 +18,7 @@ import net.md_5.bungee.api.ChatColor;
 import org.bukkit.entity.Player;
 
 /**
- * Utility class used for communicating in different dialects.
+ * Utility class used for communicating to players in game with a translator.
  * 
  * @author Jesse Prescott (BleedObsidian).
  */
@@ -33,7 +33,7 @@ public final class ChatLogger {
     /**
      * Language translator.
      */
-    private LanguageTranslator translator;
+    private final LanguageTranslator translator;
     
     /**
      * Constructor.
diff --git a/src/main/java/com/gmail/bleedobsidian/itemcase/CommandHandler.java b/src/main/java/com/gmail/bleedobsidian/itemcase/CommandHandler.java
new file mode 100644
index 0000000..5ef95db
--- /dev/null
+++ b/src/main/java/com/gmail/bleedobsidian/itemcase/CommandHandler.java
@@ -0,0 +1,95 @@
+/*
+ * ItemCase is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/gpl.html>.
+ */
+package com.gmail.bleedobsidian.itemcase;
+
+import com.gmail.bleedobsidian.itemcase.commands.CreateCommand;
+import org.bukkit.command.Command;
+import org.bukkit.command.CommandExecutor;
+import org.bukkit.command.CommandSender;
+import org.bukkit.command.ConsoleCommandSender;
+import org.bukkit.entity.Player;
+
+/**
+ * The central command handler for all ItemCase commands.
+ * 
+ * @author Jesse Prescott (BleedObsidian)
+ */
+public final class CommandHandler implements CommandExecutor {
+
+    @Override
+    public boolean onCommand(CommandSender sender, Command command,
+            String label, String[] args) {
+        
+        // Check we have atleast 1 argument (sub-command label).
+        if(args.length <= 0) {
+            
+            // Show help message.
+            this.showHelp(sender, label);
+            
+            // We have handled this error.
+            return true;
+        }
+        
+        // Direct sub-command to corresponding command handler.
+        switch(args[0]) {
+            
+            case "create":
+                new CreateCommand().execute(sender, label, args);
+                break;
+            default:
+                this.showHelp(sender, label);
+                break;
+        }
+        
+        // We have handled this command.
+        return true;
+    }
+    
+    /**
+     * Show help for main command to given sender.
+     * 
+     * @param sender CommandSender.
+     */
+    private void showHelp(CommandSender sender, String label) {
+        
+        // Get translator.
+        LanguageTranslator translator = ItemCaseCore.instance.getTranslator();
+        
+        // Set placeholder.
+        translator.setPlaceholder("%COMMAND%", 
+                "/" + label + " [create/destroy/modify/storage]");
+        
+        // Check if sender is a player or console.
+        if(sender instanceof Player) {
+            
+            // Cast sender to player.
+            Player player = (Player) sender;
+            
+            // Get chat logger.
+            ChatLogger logger = ItemCaseCore.instance.getChatLogger();
+            
+            // Send message to player.
+            logger.message(player, "command.itemcase-help");
+            
+        } else {
+            
+            // Get console logger.
+            GenericLogger logger = ItemCaseCore.instance.getGenericLogger();
+            
+            // Send console message.
+            logger.message(sender, "command.itemcase-help");
+        }
+    }
+}
diff --git a/src/main/java/com/gmail/bleedobsidian/itemcase/GenericLogger.java b/src/main/java/com/gmail/bleedobsidian/itemcase/GenericLogger.java
new file mode 100644
index 0000000..64675f9
--- /dev/null
+++ b/src/main/java/com/gmail/bleedobsidian/itemcase/GenericLogger.java
@@ -0,0 +1,62 @@
+/*
+ * ItemCase is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/gpl.html>.
+ */
+package com.gmail.bleedobsidian.itemcase;
+
+import org.bukkit.command.CommandSender;
+
+/**
+ * Utility class used for communicating to command senders in different
+ * dialects.
+ * 
+ * @author Jesse Prescott (BleedObsidian).
+ */
+public final class GenericLogger {
+    
+    /**
+     * Language translator.
+     */
+    private final LanguageTranslator translator;
+    
+    /**
+     * Constructor.
+     * 
+     * @param translator language translator.
+     */
+    public GenericLogger(LanguageTranslator translator) {
+        
+        // Set translator.
+        this.translator = translator;
+    }
+    
+    /**
+     * Send given CommandSender a message.
+     * 
+     * @param sender CommandSender.
+     * @param string Message key or message.
+     */
+    public void message(CommandSender sender, String string) {
+        
+        // If string is a message key.
+        if(this.translator.isKey(string)) {
+            
+            // Translate.
+            string = this.translator.getTranslation(string);
+            
+        }
+        
+        // Send message.
+        sender.sendMessage(string);
+    }
+}
diff --git a/src/main/java/com/gmail/bleedobsidian/itemcase/ItemCaseCore.java b/src/main/java/com/gmail/bleedobsidian/itemcase/ItemCaseCore.java
index c8489b5..ab562e7 100644
--- a/src/main/java/com/gmail/bleedobsidian/itemcase/ItemCaseCore.java
+++ b/src/main/java/com/gmail/bleedobsidian/itemcase/ItemCaseCore.java
@@ -55,6 +55,11 @@ public final class ItemCaseCore extends JavaPlugin {
      */
     private final ChatLogger chatLogger = new ChatLogger(translator);
     
+    /**
+     * Generic Logger.
+     */
+    private final GenericLogger genericLogger = new GenericLogger(translator);
+    
     /**
      * ItemcaseManager.
      */
@@ -112,6 +117,9 @@ public final class ItemCaseCore extends JavaPlugin {
         // Load itemcases for already loaded worlds.
         this.itemcaseManager.initialize();
         
+        // Set command executor.
+        this.getCommand("itemcase").setExecutor(new CommandHandler());
+        
         // Set version placeholder and log.
         this.translator.setPlaceholder("%VERSION%",
                 this.getDescription().getVersion());
@@ -150,6 +158,20 @@ public final class ItemCaseCore extends JavaPlugin {
         return this.consoleLogger;
     }
     
+    /**
+     * @return Chat logger.
+     */
+    public ChatLogger getChatLogger() {
+        return this.chatLogger;
+    }
+    
+    /**
+     * @return Generic Logger.
+     */
+    public GenericLogger getGenericLogger() {
+        return this.genericLogger;
+    }
+    
     /**
      * @return Itemcase manager.
      */
diff --git a/src/main/java/com/gmail/bleedobsidian/itemcase/commands/Command.java b/src/main/java/com/gmail/bleedobsidian/itemcase/commands/Command.java
new file mode 100644
index 0000000..e65eac3
--- /dev/null
+++ b/src/main/java/com/gmail/bleedobsidian/itemcase/commands/Command.java
@@ -0,0 +1,34 @@
+/*
+ * ItemCase is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/gpl.html>.
+ */
+package com.gmail.bleedobsidian.itemcase.commands;
+
+import org.bukkit.command.CommandSender;
+
+/**
+ * An interface for all ItemCase commands.
+ * 
+ * @author Jesse Prescott (BleedObsidian)
+ */
+public interface Command {
+    
+    /**
+     * Execute this command with the given arguments.
+     * 
+     * @param sender The command sender.
+     * @param label The label used,
+     * @param args Any arguments.
+     */
+    public void execute(CommandSender sender, String label, String[] args);
+}
diff --git a/src/main/java/com/gmail/bleedobsidian/itemcase/commands/CreateCommand.java b/src/main/java/com/gmail/bleedobsidian/itemcase/commands/CreateCommand.java
new file mode 100644
index 0000000..7ca0864
--- /dev/null
+++ b/src/main/java/com/gmail/bleedobsidian/itemcase/commands/CreateCommand.java
@@ -0,0 +1,107 @@
+/*
+ * ItemCase is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/gpl.html>.
+ */
+package com.gmail.bleedobsidian.itemcase.commands;
+
+import com.gmail.bleedobsidian.itemcase.ChatLogger;
+import com.gmail.bleedobsidian.itemcase.ItemCaseCore;
+import java.util.ArrayList;
+import org.bukkit.Location;
+import org.bukkit.Material;
+import org.bukkit.command.CommandSender;
+import org.bukkit.entity.Player;
+import org.bukkit.inventory.ItemStack;
+
+/**
+ * A command handler for the 'create' command.
+ * 
+ * @author Jesse Prescott (BleedObsidian)
+ */
+public final class CreateCommand implements Command {
+
+    @Override
+    public void execute(CommandSender sender, String label,
+            String[] args) {
+        
+        // If sender is not a player.
+        if(!(sender instanceof Player)) {
+            
+            // Send message.
+            ItemCaseCore.instance.getGenericLogger().message(
+                    sender, "command.not-player");
+            
+            // Exit.
+            return;
+        }
+        
+         // Get chat logger.
+        ChatLogger chatLogger = ItemCaseCore.instance.getChatLogger();
+        
+        // Cast sender to player.
+        Player player = (Player) sender;
+        
+        // Check if player has permission.
+        if(!player.hasPermission("itemcase.create")) {
+            
+            // Send message.
+            chatLogger.message(player, "command.permission");
+            
+            // Exit.
+            return;
+        }
+        
+        // List of materials that can be used as itemcases.
+        ArrayList<Material> materials =
+                ItemCaseCore.instance.getConfigFile().getMaterials();
+        
+        // The target location.
+        Location target = null;
+        
+        // ItemStack to use.
+        ItemStack itemStack = null;
+
+        // Get the players target block.
+        target = player.getTargetBlock(null, 3).getLocation();
+
+        // If target block is not on the list of accepted materials.
+        if(!materials.contains(target.getBlock().getType())) {
+
+            // Show message.
+            chatLogger.message(player, "command.create.invalid-type");
+
+            // Exit.
+            return;
+        }
+
+        // Get item in players main hand to use as the Itemcase item.
+        itemStack = player.getInventory().getItemInMainHand();
+
+        // If the player is not holding anything...
+        if(itemStack == null || itemStack.getType() == Material.AIR) {
+
+            // Show message.
+            chatLogger.message(player, "command.create.main-hand");
+
+            // Exit.
+            return;
+        }
+        
+        // Create itemcase.
+        ItemCaseCore.instance.getItemcaseManager().createItemcase(
+                itemStack, target, player);
+        
+        // Show message.
+        chatLogger.message(player, "command.create.success");
+    }
+}
diff --git a/src/main/java/com/gmail/bleedobsidian/itemcase/managers/ItemcaseManager.java b/src/main/java/com/gmail/bleedobsidian/itemcase/managers/ItemcaseManager.java
index 8b9c807..8d610a4 100644
--- a/src/main/java/com/gmail/bleedobsidian/itemcase/managers/ItemcaseManager.java
+++ b/src/main/java/com/gmail/bleedobsidian/itemcase/managers/ItemcaseManager.java
@@ -22,6 +22,7 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import org.bukkit.Bukkit;
 import org.bukkit.Location;
+import org.bukkit.OfflinePlayer;
 import org.bukkit.World;
 import org.bukkit.entity.Player;
 import org.bukkit.event.EventHandler;
@@ -109,7 +110,7 @@ public final class ItemcaseManager {
      * @param owner The owner of this itemcase.
      */
     public void createItemcase(ItemStack itemStack, Location location,
-            Player owner) {
+            OfflinePlayer owner) {
         
         // Create new itemcase instance.
         Itemcase itemcase = new Itemcase(itemStack, location, owner);
diff --git a/src/main/resources/languages/EN.yml b/src/main/resources/languages/EN.yml
index d3d3cff..b15b638 100644
--- a/src/main/resources/languages/EN.yml
+++ b/src/main/resources/languages/EN.yml
@@ -12,9 +12,39 @@ console:
         #
         # %LANGUAGE% = The language as set in the main config.
         # %VERSION% = The version of ItemCase in use.
+        # %WORLD_NAME% = The name of the world.
         locale: "Using locale: %LANGUAGE%"
         config-loaded: "Successfully loaded main configuration file."
         listener-registered: "Main event listener registered."
         enabled: "ItemCase v%VERSION% enabled."
         unloaded: "Itemcases unloaded successfully."
-        loaded: "Loaded itemcases for world: %WORLD_NAME%"
\ No newline at end of file
+        loaded: "Loaded itemcases for world: %WORLD_NAME%"
+
+# Messages shown when using commands.
+command:
+    
+    # Shown when a user incorrectly uses the main itemcase command.
+    # %COMMAND% = The command usage syntax.
+    itemcase-help: "Usage: %COMMAND%"
+    
+    # Shown when a user tries to execute a command and they are not an in game
+    # player.
+    not-player: "You must be a player to use this command."
+    
+    # Shown when a user tries to execute a command and they do not have the
+    # required permission.
+    permission: "You do not have permission to use this command."
+    
+    # Messages shown when using the 'create' command.
+    create:
+        
+        # Shown to the player when they try to make an itemcase out of an
+        # invalid block type.
+        invalid-type: "This block cannot be used for an ItemCase."
+        
+        # Shown to the player when they use the create command but they are not
+        # holding any item in their main hand.
+        main-hand: "You must be holding something in your main hand."
+        
+        # Shown to the player upon successful creation.
+        success: "ItemCase created."
\ No newline at end of file
diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml
index c7e38a7..2daf948 100644
--- a/src/main/resources/plugin.yml
+++ b/src/main/resources/plugin.yml
@@ -7,4 +7,13 @@ website: http://dev.bukkit.org/bukkit-plugins/itemcase/
 database: false
 prefix: ItemCase
 
-main: com.gmail.bleedobsidian.itemcase.ItemCaseCore
\ No newline at end of file
+main: com.gmail.bleedobsidian.itemcase.ItemCaseCore
+
+commands:
+    itemcase:
+        aliases: [itemc, ic]
+        description: "Showcase items on slabs, that can also be used as shops."
+        
+permissions:
+    itemcase.create:
+        description: Create and destroy showcase itemcases.
\ No newline at end of file