From 4b843638ea8f10752363eb97e455174dfef19d46 Mon Sep 17 00:00:00 2001 From: Parker Hawke Date: Sun, 16 Dec 2018 18:21:29 -0500 Subject: [PATCH] Add BlockData#getAsString(boolean) to hide unspecified states --- nms-patches/ArgumentBlock.patch | 40 ++++++++++++++++++ nms-patches/Block.patch | 9 ---- .../block/data/CraftBlockData.java | 42 ++++++++++++++++--- src/test/java/org/bukkit/BlockDataTest.java | 17 ++++++++ 4 files changed, 93 insertions(+), 15 deletions(-) create mode 100644 nms-patches/ArgumentBlock.patch diff --git a/nms-patches/ArgumentBlock.patch b/nms-patches/ArgumentBlock.patch new file mode 100644 index 0000000000..1c33861f66 --- /dev/null +++ b/nms-patches/ArgumentBlock.patch @@ -0,0 +1,40 @@ +--- a/net/minecraft/server/ArgumentBlock.java ++++ b/net/minecraft/server/ArgumentBlock.java +@@ -41,7 +41,7 @@ + private static final Function> h = SuggestionsBuilder::buildFuture; + private final StringReader i; + private final boolean j; +- private final Map, Comparable> k = Maps.newHashMap(); ++ private final Map, Comparable> k = Maps.newLinkedHashMap(); // CraftBukkit - stable + private final Map l = Maps.newHashMap(); + private MinecraftKey m = new MinecraftKey(""); + private BlockStateList n; +@@ -222,7 +222,7 @@ + if (comparable instanceof Integer) { + suggestionsbuilder.suggest((Integer) comparable); + } else { +- suggestionsbuilder.suggest(iblockstate.a(comparable)); ++ suggestionsbuilder.suggest(iblockstate.a((T) comparable)); // CraftBukkit - decompile error + } + } + +@@ -488,8 +488,8 @@ + Optional optional = iblockstate.b(s); + + if (optional.isPresent()) { +- this.o = (IBlockData) this.o.set(iblockstate, (Comparable) optional.get()); +- this.k.put(iblockstate, optional.get()); ++ this.o = (IBlockData) this.o.set(iblockstate, (T) optional.get()); // CraftBukkit - decompile error ++ this.k.put(iblockstate, (Comparable) optional.get()); // CraftBukkit - decompile error + } else { + this.i.setCursor(i); + throw ArgumentBlock.e.createWithContext(this.i, this.m.toString(), iblockstate.a(), s); +@@ -526,7 +526,7 @@ + private static > void a(StringBuilder stringbuilder, IBlockState iblockstate, Comparable comparable) { + stringbuilder.append(iblockstate.a()); + stringbuilder.append('='); +- stringbuilder.append(iblockstate.a(comparable)); ++ stringbuilder.append(iblockstate.a((T) comparable)); // CraftBukkit - decompile error + } + + public CompletableFuture a(SuggestionsBuilder suggestionsbuilder) { diff --git a/nms-patches/Block.patch b/nms-patches/Block.patch index ee800c520b..cbf7866531 100644 --- a/nms-patches/Block.patch +++ b/nms-patches/Block.patch @@ -43,15 +43,6 @@ } } -@@ -646,7 +653,7 @@ - } - - public String toString() { -- return "Block{" + IRegistry.BLOCK.getKey(this) + "}"; -+ return IRegistry.BLOCK.getKey(this).toString(); // CraftBukkit - cheap hack - } - - public static boolean c(Block block) { @@ -1395,8 +1402,14 @@ } diff --git a/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java b/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java index d470e2b34a..57f4833677 100644 --- a/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java +++ b/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java @@ -1,5 +1,6 @@ package org.bukkit.craftbukkit.block.data; +import java.util.stream.Collectors; import com.google.common.base.Function; import com.google.common.base.Preconditions; import com.google.common.collect.BiMap; @@ -12,6 +13,7 @@ import java.util.Map; import java.util.Set; import net.minecraft.server.ArgumentBlock; import net.minecraft.server.Block; +import net.minecraft.server.BlockDataAbstract; import net.minecraft.server.BlockStateBoolean; import net.minecraft.server.BlockStateEnum; import net.minecraft.server.BlockStateInteger; @@ -29,7 +31,7 @@ import org.bukkit.craftbukkit.util.CraftMagicNumbers; public class CraftBlockData implements BlockData { private IBlockData state; - private Set> parsedStates; + private Map, Comparable> parsedStates; protected CraftBlockData() { throw new AssertionError("Template Constructor"); @@ -102,7 +104,7 @@ public class CraftBlockData implements BlockData { CraftBlockData clone = (CraftBlockData) this.clone(); clone.parsedStates = null; - for (IBlockState parsed : craft.parsedStates) { + for (IBlockState parsed : craft.parsedStates.keySet()) { clone.state = clone.state.set(parsed, craft.state.get(parsed)); } @@ -238,7 +240,12 @@ public class CraftBlockData implements BlockData { @Override public String getAsString() { - return state.toString(); + return toString(((BlockDataAbstract) state).b()); + } + + @Override + public String getAsString(boolean hideUnspecified) { + return (hideUnspecified && parsedStates != null) ? toString(parsedStates) : getAsString(); } @Override @@ -255,6 +262,29 @@ public class CraftBlockData implements BlockData { return "CraftBlockData{" + state.toString() + "}"; } + // Mimicked from BlockDataAbstract#toString() + public String toString(Map, Comparable> states) { + StringBuilder stateString = new StringBuilder(IRegistry.BLOCK.getKey(state.getBlock()).toString()); + + if (!states.isEmpty()) { + stateString.append('['); + stateString.append(states.entrySet().stream().map(STATE_TO_VALUE).collect(Collectors.joining(","))); + stateString.append(']'); + } + + return stateString.toString(); + } + + // BlockDataAbstract#b. Should PAIL public in future release but is mimicked for now to avoid a decompile error patch + private static final Function, Comparable>, String> STATE_TO_VALUE = (entry) -> { + if (entry == null) { + return ""; + } + + IBlockState state = entry.getKey(); + return state.a() + "=" + state.a(entry.getValue()); + }; + @Override public boolean equals(Object obj) { return obj instanceof CraftBlockData && state.equals(((CraftBlockData) obj).state); @@ -473,7 +503,7 @@ public class CraftBlockData implements BlockData { IBlockData blockData; Block block = CraftMagicNumbers.getBlock(material); - Set> parsed = null; + Map, Comparable> parsed = null; // Data provided, use it if (data != null) { @@ -487,8 +517,8 @@ public class CraftBlockData implements BlockData { ArgumentBlock arg = new ArgumentBlock(reader, false).a(false); Preconditions.checkArgument(!reader.canRead(), "Spurious trailing data"); - blockData = arg.b(); - parsed = arg.a().keySet(); + blockData = arg.b(); // PAIL rename getBlockData + parsed = arg.a(); // PAIL rename getStateMap } catch (CommandSyntaxException ex) { throw new IllegalArgumentException("Could not parse data: " + data, ex); } diff --git a/src/test/java/org/bukkit/BlockDataTest.java b/src/test/java/org/bukkit/BlockDataTest.java index fb30a9e238..11b6006960 100644 --- a/src/test/java/org/bukkit/BlockDataTest.java +++ b/src/test/java/org/bukkit/BlockDataTest.java @@ -152,4 +152,21 @@ public class BlockDataTest extends AbstractTestingBase { Assert.assertTrue(one.matches(two)); Assert.assertFalse(two.matches(one)); } + + @Test + public void testGetAsString() { + String dataString = "minecraft:chest[facing=east,waterlogged=true]"; + BlockData data = CraftBlockData.newData(null, dataString); + + Assert.assertThat(data.getAsString(true), is(dataString)); + Assert.assertThat(data.getAsString(false), is("minecraft:chest[facing=east,type=single,waterlogged=true]")); + } + + @Test + public void testGetAsString2() { + Chest data = (Chest) CraftBlockData.fromData(Blocks.CHEST.getBlockData().set(BlockChest.FACING, EnumDirection.EAST)); + + Assert.assertThat(data.getAsString(true), is("minecraft:chest[facing=east,type=single,waterlogged=false]")); + Assert.assertThat(data.getAsString(false), is("minecraft:chest[facing=east,type=single,waterlogged=false]")); + } }