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 0000000000000000000000000000000000000000..2512eddd8b4bba1e5c1319179f866fd4c40370a6 --- /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 ea4aff802ae538e74d9e5ceaf5b0c152e95878e0..82ef77440f8825fc8e671e00fb7b83b114e7e6ba 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 82de27fccb8ff5341dfe9237e7f352fcf7a0ed69..e12702d7ac3e1c53e53506ffce7de84da67c89ae 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)); } }