Skip to content

Commit

Permalink
Further work on items
Browse files Browse the repository at this point in the history
  • Loading branch information
me4502 committed Aug 4, 2018
1 parent 001a354 commit 1cc735e
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 57 deletions.
Expand Up @@ -290,7 +290,7 @@ public String getNbtId() {
return "";
}
Tag idTag = nbtData.getValue().get("id");
if (idTag != null && idTag instanceof StringTag) {
if (idTag instanceof StringTag) {
return ((StringTag) idTag).getValue();
} else {
return "";
Expand Down Expand Up @@ -413,7 +413,7 @@ public boolean equals(Object o) {
* @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);
}

/**
Expand Down
Expand Up @@ -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;
}

Expand Down
Expand Up @@ -587,6 +587,10 @@ public static void registerBlock(BlockType blockType) {

@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);
}
}
Expand Up @@ -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;
}

Expand Down
Expand Up @@ -766,6 +766,10 @@ public static void registerItem(ItemType itemType) {

@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);
}
}
Expand Up @@ -27,7 +27,6 @@
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;
Expand Down Expand Up @@ -65,8 +64,6 @@ public BaseItem call(CommandArgs args, CommandLocals locals) throws CommandExcep

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);
}
Expand Down
Expand Up @@ -35,32 +35,31 @@ protected DefaultItemParser(WorldEdit worldEdit) {
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]);

// 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");
}
Expand All @@ -72,7 +71,7 @@ public BaseItem parseFromInput(String input, ParserContext context) throws Input
if (item == null) {
throw new InputParseException("'" + input + "' did not match any item");
} else {
item.setData(meta);
item.setDamage(damage);
return item;
}
}
Expand Down
Expand Up @@ -98,6 +98,10 @@ private void loadFromResource() throws IOException {
*/
@Nullable
private BlockEntry findById(String id) {
// If it has no namespace, assume minecraft.
if (!id.contains(":")) {
id = "minecraft:" + id;
}
return idMap.get(id);
}

Expand Down Expand Up @@ -190,7 +194,7 @@ private static class BlockEntry {
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() {
Expand Down
Expand Up @@ -99,6 +99,10 @@ private void loadFromResource() throws IOException {
*/
@Nullable
private ItemEntry findById(String id) {
// If it has no namespace, assume minecraft.
if (!id.contains(":")) {
id = "minecraft:" + id;
}
return idMap.get(id);
}

Expand Down
Expand Up @@ -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
Expand Down

This file was deleted.

0 comments on commit 1cc735e

Please sign in to comment.