From 137ab9ef6fabf76eaa6a0f3eaa1ee0c367323b8f Mon Sep 17 00:00:00 2001
From: Chris Kearney <chris@kearneymail.com>
Date: Sat, 13 May 2017 15:34:25 -0700
Subject: [PATCH] load item command

---
 .../commands/admin/LoadItemCommand.java       | 99 +++++++++++++++++++
 .../commands/admin/LoadNpcCommand.java        |  4 +-
 .../configuration/ConfigureCommands.java      |  1 +
 3 files changed, 102 insertions(+), 2 deletions(-)
 create mode 100644 src/main/java/com/comandante/creeper/command/commands/admin/LoadItemCommand.java

diff --git a/src/main/java/com/comandante/creeper/command/commands/admin/LoadItemCommand.java b/src/main/java/com/comandante/creeper/command/commands/admin/LoadItemCommand.java
new file mode 100644
index 00000000..2512eddd
--- /dev/null
+++ b/src/main/java/com/comandante/creeper/command/commands/admin/LoadItemCommand.java
@@ -0,0 +1,99 @@
+package com.comandante.creeper.command.commands.admin;
+
+import com.comandante.creeper.command.commands.Command;
+import com.comandante.creeper.core_game.GameManager;
+import com.comandante.creeper.items.ItemMetadata;
+import com.comandante.creeper.player.PlayerRole;
+import com.google.common.collect.Sets;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.util.EntityUtils;
+import org.jboss.netty.channel.ChannelHandlerContext;
+import org.jboss.netty.channel.MessageEvent;
+
+import java.net.MalformedURLException;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Set;
+
+public class LoadItemCommand extends Command {
+
+    final static List<String> validTriggers = Arrays.asList("loaditem");
+    final static String description = "Load an Item using JSON over http.";
+    final static String correctUsage = "loaditem <http url with json for item>";
+    final static Set<PlayerRole> roles = Sets.newHashSet(PlayerRole.ADMIN);
+
+    public LoadItemCommand(GameManager gameManager) {
+        super(gameManager, validTriggers, description, correctUsage, roles);
+    }
+
+    @Override
+    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
+        execCommand(ctx, e, () -> {
+
+            if (originalMessageParts.size() <= 1) {
+                write("Please specify a http url." + "\r\n");
+                return;
+            }
+
+            originalMessageParts.remove(0);
+
+
+            String itemJsonUrl = originalMessageParts.get(0);
+            if (!isValidURL(itemJsonUrl)) {
+                write("Inavlid HTTP address." + "\r\n");
+                return;
+            }
+
+            HttpGet httpGet = new HttpGet(itemJsonUrl);
+
+            HttpClient httpclient = gameManager.getHttpclient();
+
+            HttpResponse httpResponse = httpclient.execute(httpGet);
+
+            HttpEntity entity = httpResponse.getEntity();
+
+            if (entity == null) {
+                write("Error retrieving JSON url." + "\r\n");
+                return;
+            }
+
+            String npcJson = EntityUtils.toString(entity);
+
+            ItemMetadata itemMetadata = null;
+            try {
+                itemMetadata = gameManager.getGson().fromJson(npcJson, ItemMetadata.class);
+            } catch (Exception ex) {
+                write("Retrieved JSON file is malformed. " + ex.getLocalizedMessage() + "\r\n");
+                return;
+            }
+            httpGet.reset();
+
+            gameManager.getItemStorage().saveItemMetaData(itemMetadata);
+            write("Item Saved. - " + itemMetadata.getInternalItemName() + "\r\n");
+
+        });
+    }
+
+    public boolean isValidURL(String url) {
+        URL u = null;
+        try {
+            u = new URL(url);
+        } catch (MalformedURLException e) {
+            return false;
+        }
+        try {
+            u.toURI();
+        } catch (URISyntaxException e) {
+            return false;
+        }
+        return true;
+    }
+
+}
+
+
diff --git a/src/main/java/com/comandante/creeper/command/commands/admin/LoadNpcCommand.java b/src/main/java/com/comandante/creeper/command/commands/admin/LoadNpcCommand.java
index ea4aff80..82ef7744 100644
--- a/src/main/java/com/comandante/creeper/command/commands/admin/LoadNpcCommand.java
+++ b/src/main/java/com/comandante/creeper/command/commands/admin/LoadNpcCommand.java
@@ -23,8 +23,8 @@ import java.util.Set;
 public class LoadNpcCommand extends Command {
 
     final static List<String> validTriggers = Arrays.asList("loadnpc");
-    final static String description = "Load an NPC using JSON.";
-    final static String correctUsage = "loadjpc <http url with json for npc>";
+    final static String description = "Load a NPC using JSON over http";
+    final static String correctUsage = "loadnpc <http url with json for npc>";
     final static Set<PlayerRole> roles = Sets.newHashSet(PlayerRole.ADMIN);
 
     public LoadNpcCommand(GameManager gameManager) {
diff --git a/src/main/java/com/comandante/creeper/configuration/ConfigureCommands.java b/src/main/java/com/comandante/creeper/configuration/ConfigureCommands.java
index 82de27fc..e12702d7 100644
--- a/src/main/java/com/comandante/creeper/configuration/ConfigureCommands.java
+++ b/src/main/java/com/comandante/creeper/configuration/ConfigureCommands.java
@@ -102,5 +102,6 @@ public class ConfigureCommands {
         creeperCommandRegistry.addCommand(new RecallCommand(gameManager));
         creeperCommandRegistry.addCommand(new ToggleChatCommand(gameManager));
         creeperCommandRegistry.addCommand(new LoadNpcCommand(gameManager));
+        creeperCommandRegistry.addCommand(new LoadItemCommand(gameManager));
     }
 }
-- 
GitLab