Skip to content

Commit

Permalink
use block state to properly adjust metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
insanj committed Mar 9, 2019
1 parent e65c06f commit 0f9a8c2
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 12 deletions.
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
42 changes: 37 additions & 5 deletions plugin/me/insanj/contractors/Contractors.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, ContractorsSchematic> schematics;

@Override
public void onEnable() {
// (1) load all schematics from disk
ArrayList<File> schematicFiles = config.getSchematicFiles();

// (2) loop thru and parse each NBT/.schematic file
HashMap<String, ContractorsSchematic> schematics = new HashMap<String, ContractorsSchematic>();
HashMap<String, ContractorsSchematic> readSchematics = new HashMap<String, ContractorsSchematic>();
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;
}
}
14 changes: 10 additions & 4 deletions plugin/me/insanj/contractors/ContractorsSchematicHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
9 changes: 7 additions & 2 deletions plugin/plugin.yml
Original file line number Diff line number Diff line change
@@ -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
api-version: 1.13
commands:
contractors:
description: For testing purposes
usage: |
/contractors <schematic_name>

0 comments on commit 0f9a8c2

Please sign in to comment.