Skip to content

Commit

Permalink
finally arrive at ideal arg pattern, schematic legacy fix
Browse files Browse the repository at this point in the history
  • Loading branch information
insanj committed Mar 19, 2019
1 parent a35bfc8 commit 213107b
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 31 deletions.
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</a>

<a href="https://getbukkit.org/download/craftbukkit">
<img src="https://img.shields.io/badge/minecraft-1.13-purple.svg" />
<img src="https://img.shields.io/badge/minecraft-1.13.2-purple.svg" />
</a>

<a href="https://getbukkit.org/download/craftbukkit">
Expand Down
64 changes: 40 additions & 24 deletions plugin/java/me/insanj/mayor/MayorCommandExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,49 +42,65 @@ public MayorCommandExecutor(MayorSchematicHandler handler, HashMap<String, Mayor
}

public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (args.length != 2) {
sender.sendMessage(ChatColor.RED + "No schematic name provided");
if (args.length != 1) {
sender.sendMessage(ChatColor.RED + "No schematic or structure name provided!");
return false;
}

else if (!(sender instanceof Player) || !sender.isOp()) {
sender.sendMessage(ChatColor.RED + "Only operator players can execute this command");
if (!(sender instanceof Player) || !sender.isOp()) {
sender.sendMessage(ChatColor.RED + "Only operator players can execute this command :(");
return false;
}

String argumentString = args[0];
Player player = (Player) sender;
if (args[0].equals("structure")) {
String structureName = args[1];
Location target = player.getLocation();
// getTargetBlockExact(100, FluidCollisionMode.NEVER).getLocation();

if (target == null) {
sender.sendMessage(ChatColor.RED + "No target block found, make sure you're looking somewhere I can build!");
return false;
}

sender.sendMessage(ChatColor.BLUE + "Generating " + argumentString + "...");

if (argumentString.indexOf(".nbt") >= 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;
}
}
4 changes: 4 additions & 0 deletions plugin/java/me/insanj/mayor/MayorPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,8 @@ private HashMap<String, DefinedStructure> readStructures() {

return readStructures;
}

public void logError(Throwable e) {
getLogger().info(ExceptionUtils.getStackTrace(e));
}
}
15 changes: 12 additions & 3 deletions plugin/java/me/insanj/mayor/MayorSchematicHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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));
}
Expand Down
6 changes: 3 additions & 3 deletions plugin/java/plugin.yml
Original file line number Diff line number Diff line change
@@ -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 <schematic_name>
/mayor structure <structure_name>
/mayor <schematic_name>
/mayor <structure_name>

0 comments on commit 213107b

Please sign in to comment.