Handle mod:name as a way to specify blocks in the Forge mod.

This commit is contained in:
sk89q 2014-11-14 17:45:30 -08:00
parent a41ba326af
commit 3bee2d4c02
2 changed files with 40 additions and 9 deletions

View File

@ -56,12 +56,24 @@ class DefaultBlockParser extends InputParser<BaseBlock> {
@Override @Override
public BaseBlock parseFromInput(String input, ParserContext context) throws InputParseException { 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; BlockType blockType;
input = input.replace("_", " "); input = input.replace("_", " ");
input = input.replace(";", "|"); input = input.replace(";", "|");
String[] blockAndExtraData = input.split("\\|"); String[] blockAndExtraData = input.split("\\|");
String[] typeAndData = blockAndExtraData[0].split(":", 2); String[] blockLocator = blockAndExtraData[0].split(":", 3);
String testID = typeAndData[0]; 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; int blockId = -1;
@ -69,7 +81,7 @@ class DefaultBlockParser extends InputParser<BaseBlock> {
boolean parseDataValue = true; boolean parseDataValue = true;
if ("hand".equalsIgnoreCase(testID)) { if ("hand".equalsIgnoreCase(testId)) {
// Get the block type from the item in the user's hand. // Get the block type from the item in the user's hand.
final BaseBlock blockInHand = getBlockInHand(context.requireActor()); final BaseBlock blockInHand = getBlockInHand(context.requireActor());
if (blockInHand.getClass() != BaseBlock.class) { if (blockInHand.getClass() != BaseBlock.class) {
@ -79,7 +91,7 @@ class DefaultBlockParser extends InputParser<BaseBlock> {
blockId = blockInHand.getId(); blockId = blockInHand.getId();
blockType = BlockType.fromID(blockId); blockType = BlockType.fromID(blockId);
data = blockInHand.getData(); data = blockInHand.getData();
} else if ("pos1".equalsIgnoreCase(testID)) { } else if ("pos1".equalsIgnoreCase(testId)) {
// Get the block type from the "primary position" // Get the block type from the "primary position"
final World world = context.requireWorld(); final World world = context.requireWorld();
final BlockVector primaryPosition; final BlockVector primaryPosition;
@ -100,24 +112,32 @@ class DefaultBlockParser extends InputParser<BaseBlock> {
// Attempt to parse the item ID or otherwise resolve an item/block // Attempt to parse the item ID or otherwise resolve an item/block
// name to its numeric ID // name to its numeric ID
try { try {
blockId = Integer.parseInt(testID); blockId = Integer.parseInt(testId);
blockType = BlockType.fromID(blockId); blockType = BlockType.fromID(blockId);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
blockType = BlockType.lookup(testID); blockType = BlockType.lookup(testId);
if (blockType == null) { if (blockType == null) {
int t = worldEdit.getServer().resolveItem(testID); int t = worldEdit.getServer().resolveItem(testId);
if (t > 0) { if (t > 0) {
blockType = BlockType.fromID(t); // Could be null blockType = BlockType.fromID(t); // Could be null
blockId = t; 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) { if (blockId == -1 && blockType == null) {
// Maybe it's a cloth // Maybe it's a cloth
ClothColor col = ClothColor.lookup(testID); ClothColor col = ClothColor.lookup(testId);
if (col == null) { 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; blockType = BlockType.CLOTH;

View File

@ -31,6 +31,7 @@ import com.sk89q.worldedit.util.command.Description;
import com.sk89q.worldedit.util.command.Dispatcher; import com.sk89q.worldedit.util.command.Dispatcher;
import com.sk89q.worldedit.world.World; import com.sk89q.worldedit.world.World;
import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.FMLCommonHandler;
import net.minecraft.block.Block;
import net.minecraft.command.CommandBase; import net.minecraft.command.CommandBase;
import net.minecraft.command.ICommand; import net.minecraft.command.ICommand;
import net.minecraft.command.ICommandSender; import net.minecraft.command.ICommandSender;
@ -69,6 +70,16 @@ class ForgePlatform extends AbstractPlatform implements MultiUserPlatform {
@Override @Override
public int resolveItem(String name) { public int resolveItem(String name) {
if (name == null) return 0; 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) { for (Object itemObj : Item.itemRegistry) {
Item item = (Item) itemObj; Item item = (Item) itemObj;
if (item == null) continue; if (item == null) continue;