From 213107bb90ffd499b7369c87b47ec26d30a0caac Mon Sep 17 00:00:00 2001 From: insanj Date: Tue, 19 Mar 2019 00:46:29 -0400 Subject: [PATCH] finally arrive at ideal arg pattern, schematic legacy fix --- docs/README.md | 2 +- .../me/insanj/mayor/MayorCommandExecutor.java | 64 ++++++++++++------- plugin/java/me/insanj/mayor/MayorPlugin.java | 4 ++ .../insanj/mayor/MayorSchematicHandler.java | 15 ++++- plugin/java/plugin.yml | 6 +- 5 files changed, 60 insertions(+), 31 deletions(-) diff --git a/docs/README.md b/docs/README.md index e8d2272..63a5f50 100644 --- a/docs/README.md +++ b/docs/README.md @@ -22,7 +22,7 @@ - + diff --git a/plugin/java/me/insanj/mayor/MayorCommandExecutor.java b/plugin/java/me/insanj/mayor/MayorCommandExecutor.java index 1f9f6be..5c6cb5a 100644 --- a/plugin/java/me/insanj/mayor/MayorCommandExecutor.java +++ b/plugin/java/me/insanj/mayor/MayorCommandExecutor.java @@ -42,49 +42,65 @@ public MayorCommandExecutor(MayorSchematicHandler handler, HashMap= 0) { + if (structures == null || structures.size() <= 0) { + sender.sendMessage(ChatColor.RED + "No structures found in /plugins/mayor/structures/"); + return false; + } + + String structureName = argumentString; DefinedStructure structure = structures.get(structureName); if (structure == null) { sender.sendMessage(ChatColor.RED + "No structure found with the name: " + structureName); return false; } - Location location = player.getLocation(); - MayorStructureHandler.insertSingleStructure(structure, location, EnumBlockRotation.NONE); + MayorStructureHandler.insertSingleStructure(structure, target, EnumBlockRotation.NONE); + sender.sendMessage(ChatColor.GREEN + String.format("Done building structure!")); return true; } - else if (schematics == null || schematics.size() <= 0) { - sender.sendMessage(ChatColor.RED + "No schematics found in /plugins/mayor/schematics/"); - return false; - } + else if (argumentString.indexOf(".schematic") >= 0) { + if (schematics == null || schematics.size() <= 0) { + sender.sendMessage(ChatColor.RED + "No schematics found in /plugins/mayor/schematics/"); + return false; + } - String schematicName = args[1]; - MayorSchematic schematic = schematics.get(schematicName); - if (schematic == null) { - sender.sendMessage(ChatColor.RED + "Could not find schematic with name: " + schematicName); - return false; - } + String schematicName = argumentString; + MayorSchematic schematic = schematics.get(schematicName); + if (schematic == null) { + sender.sendMessage(ChatColor.RED + "Could not find schematic with name: " + schematicName); + return false; + } - Location lookingLocation = player.getTargetBlockExact(100, FluidCollisionMode.NEVER).getLocation(); // player.getLocation(); - if (lookingLocation == null) { - sender.sendMessage(ChatColor.RED + "No target block found, make sure you're looking somewhere I can build!"); - return false; + handler.pasteSchematic(player.getWorld(), target, schematic); + sender.sendMessage(ChatColor.GREEN + String.format("Done building schematic!")); + return true; } - handler.pasteSchematic(player.getWorld(), lookingLocation, schematic); - return true; + sender.sendMessage(ChatColor.RED + "Provided file must end with .schematic or .nbt, these are the only formats current supported."); + return false; } } diff --git a/plugin/java/me/insanj/mayor/MayorPlugin.java b/plugin/java/me/insanj/mayor/MayorPlugin.java index e1d5463..7977e85 100644 --- a/plugin/java/me/insanj/mayor/MayorPlugin.java +++ b/plugin/java/me/insanj/mayor/MayorPlugin.java @@ -87,4 +87,8 @@ private HashMap readStructures() { return readStructures; } + + public void logError(Throwable e) { + getLogger().info(ExceptionUtils.getStackTrace(e)); + } } diff --git a/plugin/java/me/insanj/mayor/MayorSchematicHandler.java b/plugin/java/me/insanj/mayor/MayorSchematicHandler.java index 82be591..206aff5 100644 --- a/plugin/java/me/insanj/mayor/MayorSchematicHandler.java +++ b/plugin/java/me/insanj/mayor/MayorSchematicHandler.java @@ -63,7 +63,7 @@ public MayorSchematic readSchematicFile(String name, File file) { stream.close(); return new MayorSchematic(name, sblocks, data, width, length, height); } catch (Exception e) { - e.printStackTrace(); + plugin.logError(e); } return null; @@ -84,8 +84,17 @@ public void pasteSchematic(World world, Location loc, MayorSchematic schematic) Location l = new Location(loc.getWorld(), x + loc.getX(), y + loc.getY(), z + loc.getZ()); Block block = l.getBlock(); - int b = blocks[index] & 0xFF;//make the block unsigned, so that blocks with an id over 127, like quartz and emerald, can be pasted + // ?? + // int b = blocks[index] & 0xFF; <-- make the block unsigned, so that blocks with an id over 127, like quartz and emerald, can be pasted + + int b = (int)blocks[index]; Material material = convertMaterial(b, blockData[index]); + if (material == null) { + // incompatible material!! + plugin.logError(new Exception("Unable to convert Material bytes from schematic file! Block integer: " + Integer.toString(b))); + continue; + } + block.setType(material); BlockState bs = block.getState(); @@ -97,7 +106,7 @@ public void pasteSchematic(World world, Location loc, MayorSchematic schematic) } public Material convertMaterial(int legacyMaterialId, byte legacyDataByte) { - for (Material material : Material.values()) { // or EnumSet.allOf(Material.class); + for (Material material : EnumSet.allOf(Material.class)) { if (material.getId() == legacyMaterialId) { return Bukkit.getUnsafe().fromLegacy(new MaterialData(material, legacyDataByte)); } diff --git a/plugin/java/plugin.yml b/plugin/java/plugin.yml index 9ba7acc..d2f869e 100644 --- a/plugin/java/plugin.yml +++ b/plugin/java/plugin.yml @@ -1,11 +1,11 @@ name: mayor main: me.insanj.mayor.MayorPlugin -version: 0.2.0 +version: 0.2.1 description: hire minecraft villagers to build structures api-version: 1.13 commands: mayor: description: Trigger building sequence starting at the position where you are looking usage: | - /mayor schematic - /mayor structure + /mayor + /mayor