Further work on items

This commit is contained in:
Matthew Miller 2018-06-14 11:55:02 +10:00
parent 001a3544fb
commit 1cc735e359
11 changed files with 36 additions and 57 deletions

View File

@ -290,7 +290,7 @@ public class BaseBlock extends Block implements TileEntityBlock {
return "";
}
Tag idTag = nbtData.getValue().get("id");
if (idTag != null && idTag instanceof StringTag) {
if (idTag instanceof StringTag) {
return ((StringTag) idTag).getValue();
} else {
return "";
@ -413,7 +413,7 @@ public class BaseBlock extends Block implements TileEntityBlock {
* @return true if equal
*/
public boolean equalsFuzzy(BaseBlock o) {
return (getType() == o.getType()) && (getData() == o.getData() || getData() == -1 || o.getData() == -1);
return (getType().equals(o.getType())) && (getData() == o.getData() || getData() == -1 || o.getData() == -1);
}
/**

View File

@ -26,6 +26,10 @@ public class BlockType {
private String id;
public BlockType(String id) {
// If it has no namespace, assume minecraft.
if (!id.contains(":")) {
id = "minecraft:" + id;
}
this.id = id;
}

View File

@ -587,6 +587,10 @@ public class BlockTypes {
@Nullable
public static BlockType getBlockType(String id) {
// If it has no namespace, assume minecraft.
if (id != null && !id.contains(":")) {
id = "minecraft:" + id;
}
return blockMapping.get(id);
}
}

View File

@ -26,6 +26,10 @@ public class ItemType {
private String id;
public ItemType(String id) {
// If it has no namespace, assume minecraft.
if (!id.contains(":")) {
id = "minecraft:" + id;
}
this.id = id;
}

View File

@ -766,6 +766,10 @@ public class ItemTypes {
@Nullable
public static ItemType getItemType(String id) {
// If it has no namespace, assume minecraft.
if (id != null && !id.contains(":")) {
id = "minecraft:" + id;
}
return itemMapping.get(id);
}
}

View File

@ -27,7 +27,6 @@ import com.sk89q.worldedit.blocks.BaseItem;
import com.sk89q.worldedit.util.command.composition.SimpleCommand;
import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.extension.input.InputParseException;
import com.sk89q.worldedit.extension.input.NoMatchException;
import com.sk89q.worldedit.extension.input.ParserContext;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.extent.Extent;
@ -65,8 +64,6 @@ public class ItemParser extends SimpleCommand<BaseItem> {
try {
return WorldEdit.getInstance().getItemFactory().parseFromInput(itemString, parserContext);
} catch (NoMatchException e) {
throw new CommandException(e.getMessage(), e);
} catch (InputParseException e) {
throw new CommandException(e.getMessage(), e);
}

View File

@ -35,7 +35,7 @@ public class DefaultItemParser extends InputParser<BaseItem> {
public BaseItem parseFromInput(String input, ParserContext context) throws InputParseException {
String[] tokens = input.split(":", 3);
BaseItem item;
short meta = 0;
short damage = 0;
try {
int id = Integer.parseInt(tokens[0]);
@ -43,24 +43,23 @@ public class DefaultItemParser extends InputParser<BaseItem> {
// Parse metadata
if (tokens.length == 2) {
try {
meta = Short.parseShort(tokens[1]);
damage = Short.parseShort(tokens[1]);
} catch (NumberFormatException ignored) {
throw new InputParseException("Expected '" + tokens[1] + "' to be a metadata value but it's not a number");
throw new InputParseException("Expected '" + tokens[1] + "' to be a damage value but it's not a number");
}
}
item = context.requireWorld().getWorldData().getItemRegistry().createFromId(id);
} catch (NumberFormatException e) {
if (input.length() < 2) {
throw new InputParseException("'" + input + "' isn't a known item name format");
String name = tokens[0];
if (input.length() >= 2) {
name += ":" + tokens[1];
}
String name = tokens[0] + ":" + tokens[1];
// Parse metadata
if (tokens.length == 3) {
try {
meta = Short.parseShort(tokens[2]);
damage = Short.parseShort(tokens[2]);
} catch (NumberFormatException ignored) {
throw new InputParseException("Expected '" + tokens[2] + "' to be a metadata value but it's not a number");
}
@ -72,7 +71,7 @@ public class DefaultItemParser extends InputParser<BaseItem> {
if (item == null) {
throw new InputParseException("'" + input + "' did not match any item");
} else {
item.setData(meta);
item.setDamage(damage);
return item;
}
}

View File

@ -98,6 +98,10 @@ public class BundledBlockData {
*/
@Nullable
private BlockEntry findById(String id) {
// If it has no namespace, assume minecraft.
if (!id.contains(":")) {
id = "minecraft:" + id;
}
return idMap.get(id);
}
@ -190,7 +194,7 @@ public class BundledBlockData {
private String id;
private String unlocalizedName;
private List<String> aliases;
private Map<String, SimpleState> states = new HashMap<String, SimpleState>();
private Map<String, SimpleState> states = new HashMap<>();
private SimpleBlockMaterial material = new SimpleBlockMaterial();
void postDeserialization() {

View File

@ -99,6 +99,10 @@ public class BundledItemData {
*/
@Nullable
private ItemEntry findById(String id) {
// If it has no namespace, assume minecraft.
if (!id.contains(":")) {
id = "minecraft:" + id;
}
return idMap.get(id);
}

View File

@ -33,8 +33,7 @@ public class BundledItemRegistry implements ItemRegistry {
@Nullable
@Override
public BaseItem createFromId(String id) {
// TODO Fix legacy ID usage
return new BaseItem(ItemTypes.getItemType(id).getLegacyId());
return new BaseItem(ItemTypes.getItemType(id));
}
@Nullable

View File

@ -1,40 +0,0 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.world.registry;
import com.sk89q.worldedit.blocks.BaseItem;
import javax.annotation.Nullable;
public class NullItemRegistry implements ItemRegistry {
@Nullable
@Override
public BaseItem createFromId(String id) {
return null;
}
@Nullable
@Override
public BaseItem createFromId(int id) {
return null;
}
}