From 0f9a8c2e168c0f55c613eac96e76b6e2c21e4783 Mon Sep 17 00:00:00 2001 From: insanj Date: Sat, 9 Mar 2019 01:54:31 -0500 Subject: [PATCH] use block state to properly adjust metadata --- docs/README.md | 1 + makefile | 2 +- plugin/me/insanj/contractors/Contractors.java | 42 ++++++++++++++++--- .../ContractorsSchematicHandler.java | 14 +++++-- plugin/plugin.yml | 9 +++- 5 files changed, 56 insertions(+), 12 deletions(-) diff --git a/docs/README.md b/docs/README.md index c5dfac8..2d5cc19 100644 --- a/docs/README.md +++ b/docs/README.md @@ -60,6 +60,7 @@ A lot of important work parsing and building structures from .schematic/NBT file - https://www.spigotmc.org/threads/load-schematic-file-and-paste-at-custom-location.167277/ - https://bukkit.org/threads/pasting-loading-schematics.87129/ - https://www.spigotmc.org/threads/converting-item-id-and-damage-value-to-new-material-type.329614/ +- https://bukkit.org/threads/so-block-setdata-is-deprecated-whats-the-new-way-to-change-the-data-of-an-existing-block.189076/ ## Authors diff --git a/makefile b/makefile index 59bcb15..c6c0c19 100644 --- a/makefile +++ b/makefile @@ -39,6 +39,6 @@ clean: .PHONY: server server: # step 6 copy the JAR file into the server to run it! - -rm -r -f $(SERVER_PATH)/plugins && mkdir $(SERVER_PATH)/plugins + -rm -r -f $(SERVER_PATH)/plugins/*.jar && mkdir $(SERVER_PATH)/plugins cp -R $(BUILD_PATH)/$(OUTPUT_VERSIONED_NAME).jar $(SERVER_PATH)/plugins/$(OUTPUT_VERSIONED_NAME).jar cd $(SERVER_PATH) && java -Xms1G -Xmx1G -jar -DIReallyKnowWhatIAmDoingISwear $(CRAFTBUKKIT_JAR_FILENAME) diff --git a/plugin/me/insanj/contractors/Contractors.java b/plugin/me/insanj/contractors/Contractors.java index 1915fb7..efd23de 100644 --- a/plugin/me/insanj/contractors/Contractors.java +++ b/plugin/me/insanj/contractors/Contractors.java @@ -25,24 +25,56 @@ import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.ShapedRecipe; -public class Contractors extends JavaPlugin { +public class Contractors extends JavaPlugin implements CommandExecutor { public ContractorsConfig config = new ContractorsConfig(this); public ContractorsSchematicHandler handler = new ContractorsSchematicHandler(this); - + public HashMap schematics; + @Override public void onEnable() { // (1) load all schematics from disk ArrayList schematicFiles = config.getSchematicFiles(); // (2) loop thru and parse each NBT/.schematic file - HashMap schematics = new HashMap(); + HashMap readSchematics = new HashMap(); for (File file : schematicFiles) { String fileName = file.getName(); ContractorsSchematic readFile = handler.readSchematicFile(fileName, file); - schematics.put(fileName, readFile); + readSchematics.put(fileName, readFile); } // (3) print out all the files so we can see what we got - System.out.println(schematics.toString()); + System.out.println(readSchematics.toString()); + schematics = readSchematics; + + // (4) testing commands to generate schematics/structures + getCommand("contractors").setExecutor(this); + } + + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (args.length != 1) { + sender.sendMessage(ChatColor.RED + "No schematic name provided"); + return false; + } + + else if (!(sender instanceof Player)) { + sender.sendMessage(ChatColor.RED + "Only players can execute this command"); + return false; + } + + else if (schematics == null || schematics.size() <= 0) { + sender.sendMessage(ChatColor.RED + "No schematics found in /plugins/contractors/"); + return false; + } + + Player player = (Player) sender; + ContractorsSchematic schematic = schematics.get(args[0]); + if (schematic == null) { + sender.sendMessage(ChatColor.RED + "Could not find schematic with name: " + args[0]); + return false; + } + + handler.pasteSchematic(player.getWorld(), player.getLocation(), schematic); + return true; } } \ No newline at end of file diff --git a/plugin/me/insanj/contractors/ContractorsSchematicHandler.java b/plugin/me/insanj/contractors/ContractorsSchematicHandler.java index bfead7c..5d91ee0 100644 --- a/plugin/me/insanj/contractors/ContractorsSchematicHandler.java +++ b/plugin/me/insanj/contractors/ContractorsSchematicHandler.java @@ -17,8 +17,10 @@ import org.bukkit.block.data.BlockData; import org.bukkit.Material; import org.bukkit.material.MaterialData; +import org.bukkit.block.BlockState; -import net.minecraft.server.v1_13_R2.*; +import net.minecraft.server.v1_13_R2.NBTTagCompound; +import net.minecraft.server.v1_13_R2.NBTCompressedStreamTools; public class ContractorsSchematicHandler { public final Contractors plugin; @@ -91,17 +93,21 @@ public void pasteSchematic(World world, Location loc, ContractorsSchematic schem for (int y = 0; y < height; ++y) { for (int z = 0; z < length; ++z) { int index = y * width * length + z * width + x; - Block block = new Location(world, x + loc.getX(), y + loc.getY(), z + loc.getZ()).getBlock(); + 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 Material material = convertMaterial((int)blocks[index], blockData[index]); block.setType(material); - // block.setTypeIdAndData(blocks[index], blockData[index], true); + + BlockState bs = block.getState(); + bs.setRawData(blockData[index]); + bs.update(true); } } } } - @SuppressWarnings("deprecation") public Material convertMaterial(int ID, byte Data) { for(Material i : EnumSet.allOf(Material.class)) { if(i.getId() == ID) { diff --git a/plugin/plugin.yml b/plugin/plugin.yml index fef932a..0b6273a 100644 --- a/plugin/plugin.yml +++ b/plugin/plugin.yml @@ -1,5 +1,10 @@ name: contractors main: me.insanj.contractors.Contractors -version: 0.1.0 +version: 0.1.1 description: turn villagers into powerful city builders -api-version: 1.13 \ No newline at end of file +api-version: 1.13 +commands: + contractors: + description: For testing purposes + usage: | + /contractors \ No newline at end of file