From 3bee2d4c021a9c70f274228688cbeaaae6cd389a Mon Sep 17 00:00:00 2001 From: sk89q Date: Fri, 14 Nov 2014 17:45:30 -0800 Subject: [PATCH] Handle mod:name as a way to specify blocks in the Forge mod. --- .../extension/factory/DefaultBlockParser.java | 38 ++++++++++++++----- .../sk89q/worldedit/forge/ForgePlatform.java | 11 ++++++ 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/DefaultBlockParser.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/DefaultBlockParser.java index dd1dde444..beb9bc5d7 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/DefaultBlockParser.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/DefaultBlockParser.java @@ -56,12 +56,24 @@ class DefaultBlockParser extends InputParser { @Override public BaseBlock parseFromInput(String input, ParserContext context) throws InputParseException { + // TODO: Rewrite this entire method to use BaseBlocks and ignore BlockType, as well as to properly handle mod:name IDs + BlockType blockType; input = input.replace("_", " "); input = input.replace(";", "|"); String[] blockAndExtraData = input.split("\\|"); - String[] typeAndData = blockAndExtraData[0].split(":", 2); - String testID = typeAndData[0]; + String[] blockLocator = blockAndExtraData[0].split(":", 3); + String[] typeAndData; + switch (blockLocator.length) { + case 3: + typeAndData = new String[] { + blockLocator[0] + ":" + blockLocator[1], + blockLocator[2] }; + break; + default: + typeAndData = blockLocator; + } + String testId = typeAndData[0]; int blockId = -1; @@ -69,7 +81,7 @@ class DefaultBlockParser extends InputParser { boolean parseDataValue = true; - if ("hand".equalsIgnoreCase(testID)) { + if ("hand".equalsIgnoreCase(testId)) { // Get the block type from the item in the user's hand. final BaseBlock blockInHand = getBlockInHand(context.requireActor()); if (blockInHand.getClass() != BaseBlock.class) { @@ -79,7 +91,7 @@ class DefaultBlockParser extends InputParser { blockId = blockInHand.getId(); blockType = BlockType.fromID(blockId); data = blockInHand.getData(); - } else if ("pos1".equalsIgnoreCase(testID)) { + } else if ("pos1".equalsIgnoreCase(testId)) { // Get the block type from the "primary position" final World world = context.requireWorld(); final BlockVector primaryPosition; @@ -100,24 +112,32 @@ class DefaultBlockParser extends InputParser { // Attempt to parse the item ID or otherwise resolve an item/block // name to its numeric ID try { - blockId = Integer.parseInt(testID); + blockId = Integer.parseInt(testId); blockType = BlockType.fromID(blockId); } catch (NumberFormatException e) { - blockType = BlockType.lookup(testID); + blockType = BlockType.lookup(testId); if (blockType == null) { - int t = worldEdit.getServer().resolveItem(testID); + int t = worldEdit.getServer().resolveItem(testId); if (t > 0) { blockType = BlockType.fromID(t); // Could be null blockId = t; + } else if (blockLocator.length == 2) { // Block IDs in MC 1.7 and above use mod:name + t = worldEdit.getServer().resolveItem(blockAndExtraData[0]); + if (t > 0) { + blockType = BlockType.fromID(t); // Could be null + blockId = t; + typeAndData = new String[] { blockAndExtraData[0] }; + testId = blockAndExtraData[0]; + } } } } if (blockId == -1 && blockType == null) { // Maybe it's a cloth - ClothColor col = ClothColor.lookup(testID); + ClothColor col = ClothColor.lookup(testId); if (col == null) { - throw new NoMatchException("Unknown wool color '" + input + "'"); + throw new NoMatchException("Can't figure out what block '" + input + "' refers to"); } blockType = BlockType.CLOTH; diff --git a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgePlatform.java b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgePlatform.java index bcc7f7bef..65a26a6d4 100644 --- a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgePlatform.java +++ b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgePlatform.java @@ -31,6 +31,7 @@ import com.sk89q.worldedit.util.command.Description; import com.sk89q.worldedit.util.command.Dispatcher; import com.sk89q.worldedit.world.World; import cpw.mods.fml.common.FMLCommonHandler; +import net.minecraft.block.Block; import net.minecraft.command.CommandBase; import net.minecraft.command.ICommand; import net.minecraft.command.ICommandSender; @@ -69,6 +70,16 @@ class ForgePlatform extends AbstractPlatform implements MultiUserPlatform { @Override public int resolveItem(String name) { if (name == null) return 0; + + int index = name.indexOf(':'); + + if (index != -1 && index != 0 && index != name.length() - 1) { + Block block = Block.getBlockFromName(name); + if (block != null) { + return Block.getIdFromBlock(block); + } + } + for (Object itemObj : Item.itemRegistry) { Item item = (Item) itemObj; if (item == null) continue;