From 965e9774116aa4d984f86ef945368ccd3d63803e Mon Sep 17 00:00:00 2001 From: Matthew Miller Date: Tue, 24 Jul 2018 17:12:50 +1000 Subject: [PATCH] Fixed a few more errors, and added legacy support to the item parser. --- .../com/sk89q/worldedit/blocks/LazyBlock.java | 2 +- .../com/sk89q/worldedit/blocks/BaseBlock.java | 3 +- .../com/sk89q/worldedit/blocks/BaseItem.java | 28 +-------------- .../sk89q/worldedit/blocks/BaseItemStack.java | 35 ------------------- .../extension/factory/DefaultItemParser.java | 24 +++++++++++-- .../extent/clipboard/io/SchematicReader.java | 16 +++++---- .../legacycompat/NBTCompatibilityHandler.java | 5 +-- .../SignCompatibilityHandler.java | 5 +-- .../com/sk89q/worldedit/world/NbtValued.java | 2 +- .../worldedit/world/chunk/AnvilChunk.java | 15 ++++---- .../sk89q/worldedit/world/chunk/OldChunk.java | 10 +++--- .../world/registry/BundledItemRegistry.java | 4 ++- .../worldedit/world/storage/ChunkStore.java | 8 ++--- 13 files changed, 56 insertions(+), 101 deletions(-) diff --git a/worldedit-core/src/legacy/java/com/sk89q/worldedit/blocks/LazyBlock.java b/worldedit-core/src/legacy/java/com/sk89q/worldedit/blocks/LazyBlock.java index feb85e932..a26d50026 100644 --- a/worldedit-core/src/legacy/java/com/sk89q/worldedit/blocks/LazyBlock.java +++ b/worldedit-core/src/legacy/java/com/sk89q/worldedit/blocks/LazyBlock.java @@ -78,7 +78,7 @@ public class LazyBlock extends BaseBlock { public CompoundTag getNbtData() { if (!loaded) { BaseBlock loadedBlock = extent.getFullBlock(position); - super.setNbtData(loadedBlock.getNbtData()); + this.nbtData = loadedBlock.getNbtData(); loaded = true; } return super.getNbtData(); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/blocks/BaseBlock.java b/worldedit-core/src/main/java/com/sk89q/worldedit/blocks/BaseBlock.java index d9c976c65..f29a2d5ef 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/blocks/BaseBlock.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/blocks/BaseBlock.java @@ -52,8 +52,7 @@ import javax.annotation.Nullable; public class BaseBlock implements BlockStateHolder, TileEntityBlock { private BlockState blockState; - @Nullable - private CompoundTag nbtData; + @Nullable CompoundTag nbtData; /** * Construct a block with a state. diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/blocks/BaseItem.java b/worldedit-core/src/main/java/com/sk89q/worldedit/blocks/BaseItem.java index 6a25186ac..114609e63 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/blocks/BaseItem.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/blocks/BaseItem.java @@ -20,10 +20,8 @@ package com.sk89q.worldedit.blocks; import com.sk89q.jnbt.CompoundTag; -import com.sk89q.worldedit.world.item.ItemType; -import com.sk89q.worldedit.world.item.ItemTypes; import com.sk89q.worldedit.world.NbtValued; -import com.sk89q.worldedit.world.registry.LegacyMapper; +import com.sk89q.worldedit.world.item.ItemType; import javax.annotation.Nullable; @@ -39,20 +37,6 @@ public class BaseItem implements NbtValued { @Nullable private CompoundTag nbtData; - /** - * Construct the object. - * - * @param id ID of the item - */ - @Deprecated - public BaseItem(int id) { - ItemType type = LegacyMapper.getInstance().getItemFromLegacy(id); - if (type == null) { - type = ItemTypes.AIR; - } - this.itemType = type; - } - /** * Construct the object. * @@ -73,16 +57,6 @@ public class BaseItem implements NbtValued { this.nbtData = tag; } - /** - * Get the type of item. - * - * @return the id - */ - @Deprecated - public int getLegacyId() { - return this.itemType.getLegacyId(); - } - /** * Get the type of item. * diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/blocks/BaseItemStack.java b/worldedit-core/src/main/java/com/sk89q/worldedit/blocks/BaseItemStack.java index c9e69732b..6a1a0cbd5 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/blocks/BaseItemStack.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/blocks/BaseItemStack.java @@ -31,16 +31,6 @@ public class BaseItemStack extends BaseItem { private int amount = 1; - /** - * Construct the object with default stack size of one, with data value of 0. - * - * @param id with data value of 0. - */ - @Deprecated - public BaseItemStack(int id) { - super(id); - } - /** * Construct the object with default stack size of one, with damage value of 0. * @@ -50,18 +40,6 @@ public class BaseItemStack extends BaseItem { super(itemType); } - /** - * Construct the object. - * - * @param id type ID - * @param amount amount in the stack - */ - @Deprecated - public BaseItemStack(int id, int amount) { - super(id); - this.amount = amount; - } - /** * Construct the object. * @@ -73,19 +51,6 @@ public class BaseItemStack extends BaseItem { this.amount = amount; } - /** - * Construct the object. - * - * @param id type ID - * @param amount amount in the stack - * @param data data value - */ - @Deprecated - public BaseItemStack(int id, int amount, short data) { - super(id); - this.amount = amount; - } - /** * Construct the object. * diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/DefaultItemParser.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/DefaultItemParser.java index d39356cbe..48923a4bf 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/DefaultItemParser.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/DefaultItemParser.java @@ -25,6 +25,8 @@ import com.sk89q.worldedit.extension.input.InputParseException; import com.sk89q.worldedit.extension.input.ParserContext; import com.sk89q.worldedit.extension.platform.Capability; import com.sk89q.worldedit.internal.registry.InputParser; +import com.sk89q.worldedit.world.item.ItemType; +import com.sk89q.worldedit.world.registry.LegacyMapper; public class DefaultItemParser extends InputParser { @@ -34,8 +36,26 @@ public class DefaultItemParser extends InputParser { @Override public BaseItem parseFromInput(String input, ParserContext context) throws InputParseException { - BaseItem item = WorldEdit.getInstance().getPlatformManager() - .queryCapability(Capability.GAME_HOOKS).getRegistries().getItemRegistry().createFromId(input); + BaseItem item = null; + // Legacy matcher + if (context.isTryingLegacy()) { + try { + String[] split = input.split(":"); + ItemType type; + if (split.length == 1) { + type = LegacyMapper.getInstance().getItemFromLegacy(Integer.parseInt(split[0])); + } else { + type = LegacyMapper.getInstance().getItemFromLegacy(Integer.parseInt(split[0]), Integer.parseInt(split[1])); + } + item = new BaseItem(type); + } catch (NumberFormatException e) { + } + } + + if (item == null) { + item = WorldEdit.getInstance().getPlatformManager() + .queryCapability(Capability.GAME_HOOKS).getRegistries().getItemRegistry().createFromId(input); + } if (item == null) { throw new InputParseException("'" + input + "' did not match any item"); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/SchematicReader.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/SchematicReader.java index 89d88c1cc..a1f32ee01 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/SchematicReader.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/SchematicReader.java @@ -42,7 +42,9 @@ import com.sk89q.worldedit.extent.clipboard.io.legacycompat.SignCompatibilityHan import com.sk89q.worldedit.regions.CuboidRegion; import com.sk89q.worldedit.regions.Region; import com.sk89q.worldedit.util.Location; +import com.sk89q.worldedit.world.block.BlockState; import com.sk89q.worldedit.world.entity.EntityTypes; +import com.sk89q.worldedit.world.registry.LegacyMapper; import com.sk89q.worldedit.world.storage.NBTConversions; import java.io.IOException; @@ -196,7 +198,7 @@ public class SchematicReader implements ClipboardReader { } int index = y * width * length + z * width + x; - BaseBlock block = new BaseBlock(blocks[index], blockData[index]); + BlockState block = LegacyMapper.getInstance().getBlockFromLegacy(blocks[index], blockData[index]); for (NBTCompatibilityHandler handler : COMPATIBILITY_HANDLERS) { if (handler.isAffectedBlock(block)) { handler.updateNBT(block, values); @@ -218,14 +220,14 @@ public class SchematicReader implements ClipboardReader { for (int z = 0; z < length; ++z) { int index = y * width * length + z * width + x; BlockVector pt = new BlockVector(x, y, z); - BaseBlock block = new BaseBlock(blocks[index], blockData[index]); - - if (tileEntitiesMap.containsKey(pt)) { - block.setNbtData(new CompoundTag(tileEntitiesMap.get(pt))); - } + BlockState state = LegacyMapper.getInstance().getBlockFromLegacy(blocks[index], blockData[index]); try { - clipboard.setBlock(region.getMinimumPoint().add(pt), block); + if (tileEntitiesMap.containsKey(pt)) { + clipboard.setBlock(region.getMinimumPoint().add(pt), new BaseBlock(state, new CompoundTag(tileEntitiesMap.get(pt)))); + } else { + clipboard.setBlock(region.getMinimumPoint().add(pt), state); + } } catch (WorldEditException e) { switch (failedBlockSets) { case 0: diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/legacycompat/NBTCompatibilityHandler.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/legacycompat/NBTCompatibilityHandler.java index 83f981a6f..cfe6007c9 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/legacycompat/NBTCompatibilityHandler.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/legacycompat/NBTCompatibilityHandler.java @@ -21,10 +21,11 @@ package com.sk89q.worldedit.extent.clipboard.io.legacycompat; import com.sk89q.jnbt.Tag; import com.sk89q.worldedit.blocks.BaseBlock; +import com.sk89q.worldedit.world.block.BlockStateHolder; import java.util.Map; public interface NBTCompatibilityHandler { - boolean isAffectedBlock(BaseBlock block); - void updateNBT(BaseBlock block, Map values); + boolean isAffectedBlock(BlockStateHolder block); + void updateNBT(BlockStateHolder block, Map values); } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/legacycompat/SignCompatibilityHandler.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/legacycompat/SignCompatibilityHandler.java index 4cff21c0c..566f34812 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/legacycompat/SignCompatibilityHandler.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/legacycompat/SignCompatibilityHandler.java @@ -27,18 +27,19 @@ import com.google.gson.JsonSyntaxException; import com.sk89q.jnbt.StringTag; import com.sk89q.jnbt.Tag; import com.sk89q.worldedit.blocks.BaseBlock; +import com.sk89q.worldedit.world.block.BlockStateHolder; import com.sk89q.worldedit.world.block.BlockTypes; import java.util.Map; public class SignCompatibilityHandler implements NBTCompatibilityHandler { @Override - public boolean isAffectedBlock(BaseBlock block) { + public boolean isAffectedBlock(BlockStateHolder block) { return block.getBlockType() == BlockTypes.SIGN || block.getBlockType() == BlockTypes.WALL_SIGN; } @Override - public void updateNBT(BaseBlock block, Map values) { + public void updateNBT(BlockStateHolder block, Map values) { for (int i = 0; i < 4; ++i) { String key = "Text" + (i + 1); Tag value = values.get(key); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/NbtValued.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/NbtValued.java index 50262730d..458a16957 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/NbtValued.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/NbtValued.java @@ -36,7 +36,7 @@ public interface NbtValued { * * @return true if there is NBT data */ - public boolean hasNbtData(); + boolean hasNbtData(); /** * Get the object's NBT data (tile entity data). The returned tag, if diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/chunk/AnvilChunk.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/chunk/AnvilChunk.java index 36046193f..ffc3f6791 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/chunk/AnvilChunk.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/chunk/AnvilChunk.java @@ -29,16 +29,18 @@ import com.sk89q.jnbt.Tag; import com.sk89q.worldedit.BlockVector; import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.blocks.BaseBlock; -import com.sk89q.worldedit.blocks.TileEntityBlock; import com.sk89q.worldedit.world.DataException; import com.sk89q.worldedit.world.World; +import com.sk89q.worldedit.world.block.BlockState; +import com.sk89q.worldedit.world.registry.LegacyMapper; import com.sk89q.worldedit.world.storage.InvalidFormatException; -import javax.annotation.Nullable; import java.util.HashMap; import java.util.List; import java.util.Map; +import javax.annotation.Nullable; + public class AnvilChunk implements Chunk { private CompoundTag rootTag; @@ -256,16 +258,11 @@ public class AnvilChunk implements Chunk { public BaseBlock getBlock(Vector position) throws DataException { int id = getBlockID(position); int data = getBlockData(position); - BaseBlock block; - - block = new BaseBlock(id, data); + BlockState state = LegacyMapper.getInstance().getBlockFromLegacy(id, data); CompoundTag tileEntity = getBlockTileEntity(position); - if (tileEntity != null) { - ((TileEntityBlock) block).setNbtData(tileEntity); - } - return block; + return new BaseBlock(state, tileEntity); } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/chunk/OldChunk.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/chunk/OldChunk.java index 64df0dd88..a78ea896f 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/chunk/OldChunk.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/chunk/OldChunk.java @@ -28,9 +28,11 @@ import com.sk89q.jnbt.Tag; import com.sk89q.worldedit.BlockVector; import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.blocks.BaseBlock; +import com.sk89q.worldedit.world.block.BlockState; import com.sk89q.worldedit.world.block.BlockTypes; import com.sk89q.worldedit.world.DataException; import com.sk89q.worldedit.world.World; +import com.sk89q.worldedit.world.registry.LegacyMapper; import com.sk89q.worldedit.world.storage.InvalidFormatException; import java.util.HashMap; @@ -178,14 +180,10 @@ public class OldChunk implements Chunk { throw new DataException("Chunk does not contain position " + position); } - BaseBlock block = new BaseBlock(id, dataVal); - + BlockState state = LegacyMapper.getInstance().getBlockFromLegacy(id, dataVal); CompoundTag tileEntity = getBlockTileEntity(position); - if (tileEntity != null) { - block.setNbtData(tileEntity); - } - return block; + return new BaseBlock(state, tileEntity); } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/BundledItemRegistry.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/BundledItemRegistry.java index c40ce80ec..11e723f21 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/BundledItemRegistry.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/BundledItemRegistry.java @@ -20,6 +20,7 @@ package com.sk89q.worldedit.world.registry; import com.sk89q.worldedit.blocks.BaseItem; +import com.sk89q.worldedit.world.item.ItemType; import com.sk89q.worldedit.world.item.ItemTypes; import javax.annotation.Nullable; @@ -33,6 +34,7 @@ public class BundledItemRegistry implements ItemRegistry { @Nullable @Override public BaseItem createFromId(String id) { - return new BaseItem(ItemTypes.get(id)); + ItemType itemType = ItemTypes.get(id); + return itemType == null ? null : new BaseItem(itemType); } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/storage/ChunkStore.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/storage/ChunkStore.java index 57296cc3e..f096496f7 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/storage/ChunkStore.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/storage/ChunkStore.java @@ -30,13 +30,14 @@ import com.sk89q.worldedit.world.chunk.AnvilChunk; import com.sk89q.worldedit.world.chunk.Chunk; import com.sk89q.worldedit.world.chunk.OldChunk; +import java.io.Closeable; import java.io.IOException; import java.util.Map; /** * Represents chunk storage mechanisms. */ -public abstract class ChunkStore { +public abstract class ChunkStore implements Closeable { /** * >> to chunk @@ -86,11 +87,6 @@ public abstract class ChunkStore { return new OldChunk(world, tag); } - /** - * Close resources. - * - * @throws IOException on I/O error - */ public void close() throws IOException { }