From 212fd0f230cb8eebe43e977f171e147c9d85ac71 Mon Sep 17 00:00:00 2001 From: md_5 Date: Sat, 28 Jul 2018 14:39:35 +1000 Subject: [PATCH] SPIGOT-4166: Fix Tags being corrupted by early access (and not reflecting reloads) --- nms-patches/TagRegistry.patch | 14 +++++++++ nms-patches/TagsServer.patch | 28 ++++++++++++++++++ .../org/bukkit/craftbukkit/CraftServer.java | 6 ++-- .../bukkit/craftbukkit/tag/CraftBlockTag.java | 15 +++++----- .../bukkit/craftbukkit/tag/CraftItemTag.java | 15 +++++----- .../org/bukkit/craftbukkit/tag/CraftTag.java | 29 +++++++++++++++++++ 6 files changed, 89 insertions(+), 18 deletions(-) create mode 100644 nms-patches/TagRegistry.patch create mode 100644 nms-patches/TagsServer.patch create mode 100644 src/main/java/org/bukkit/craftbukkit/tag/CraftTag.java diff --git a/nms-patches/TagRegistry.patch b/nms-patches/TagRegistry.patch new file mode 100644 index 0000000000..e4dafa6a59 --- /dev/null +++ b/nms-patches/TagRegistry.patch @@ -0,0 +1,14 @@ +--- a/net/minecraft/server/TagRegistry.java ++++ b/net/minecraft/server/TagRegistry.java +@@ -38,6 +38,11 @@ + TagsBlock.a((Tags) this.a); + TagsItem.a((Tags) this.b); + TagsFluid.a((Tags) this.c); ++ // CraftBukkit start ++ this.a.version++; ++ this.b.version++; ++ this.c.version++; ++ // CraftBukkit end + } + + public void a(PacketDataSerializer packetdataserializer) { diff --git a/nms-patches/TagsServer.patch b/nms-patches/TagsServer.patch new file mode 100644 index 0000000000..b5ed85d12b --- /dev/null +++ b/nms-patches/TagsServer.patch @@ -0,0 +1,28 @@ +--- a/net/minecraft/server/TagsServer.java ++++ b/net/minecraft/server/TagsServer.java +@@ -11,6 +11,7 @@ + public class TagsServer extends Tags { + + private final RegistryMaterials a; ++ public int version; // CraftBukkit + + public TagsServer(RegistryMaterials registrymaterials, String s, String s1) { + super(registrymaterials::d, registrymaterials::get, s, false, s1); +@@ -31,7 +32,7 @@ + while (iterator1.hasNext()) { + Object object = iterator1.next(); + +- packetdataserializer.d(this.a.a(object)); ++ packetdataserializer.d(this.a.a((T) object)); // CraftBukkit - decompile error + } + } + +@@ -49,7 +50,7 @@ + arraylist.add(this.a.getId(packetdataserializer.g())); + } + +- this.c().put(minecraftkey, Tag.a.a().a((Collection) arraylist).b(minecraftkey)); ++ this.c().put(minecraftkey, (Tag) Tag.a.a().a((Collection) arraylist).b(minecraftkey)); // CraftBukkit - decompile error + } + + } diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java index 61d9fa33c3..abc08ed39f 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java @@ -1749,15 +1749,17 @@ public final class CraftServer implements Server { @Override @SuppressWarnings("unchecked") public org.bukkit.Tag getTag(String registry, NamespacedKey tag, Class clazz) { + MinecraftKey key = CraftNamespacedKey.toMinecraft(tag); + switch (registry) { case org.bukkit.Tag.REGISTRY_BLOCKS: Preconditions.checkArgument(clazz == org.bukkit.Material.class, "Block namespace must have material type"); - return (org.bukkit.Tag) new CraftBlockTag(console.getTagRegistry().a().b(CraftNamespacedKey.toMinecraft(tag))); + return (org.bukkit.Tag) new CraftBlockTag(console.getTagRegistry().a(), key); case org.bukkit.Tag.REGISTRY_ITEMS: Preconditions.checkArgument(clazz == org.bukkit.Material.class, "Item namespace must have material type"); - return (org.bukkit.Tag) new CraftItemTag(console.getTagRegistry().b().b(CraftNamespacedKey.toMinecraft(tag))); + return (org.bukkit.Tag) new CraftItemTag(console.getTagRegistry().b(), key); default: throw new IllegalArgumentException(); } diff --git a/src/main/java/org/bukkit/craftbukkit/tag/CraftBlockTag.java b/src/main/java/org/bukkit/craftbukkit/tag/CraftBlockTag.java index 4b3ac51a39..2fe308d91b 100644 --- a/src/main/java/org/bukkit/craftbukkit/tag/CraftBlockTag.java +++ b/src/main/java/org/bukkit/craftbukkit/tag/CraftBlockTag.java @@ -4,25 +4,24 @@ import java.util.Collections; import java.util.Set; import java.util.stream.Collectors; import net.minecraft.server.Block; +import net.minecraft.server.MinecraftKey; +import net.minecraft.server.TagsServer; import org.bukkit.Material; -import org.bukkit.Tag; import org.bukkit.craftbukkit.util.CraftMagicNumbers; -public class CraftBlockTag implements Tag { +public class CraftBlockTag extends CraftTag { - private final net.minecraft.server.Tag handle; - - public CraftBlockTag(net.minecraft.server.Tag handle) { - this.handle = handle; + public CraftBlockTag(TagsServer registry, MinecraftKey tag) { + super(registry, tag); } @Override public boolean isTagged(Material item) { - return handle.isTagged(CraftMagicNumbers.getBlock(item)); + return getHandle().isTagged(CraftMagicNumbers.getBlock(item)); } @Override public Set getValues() { - return Collections.unmodifiableSet(handle.a().stream().map((block) -> CraftMagicNumbers.getMaterial(block)).collect(Collectors.toSet())); + return Collections.unmodifiableSet(getHandle().a().stream().map((block) -> CraftMagicNumbers.getMaterial(block)).collect(Collectors.toSet())); } } diff --git a/src/main/java/org/bukkit/craftbukkit/tag/CraftItemTag.java b/src/main/java/org/bukkit/craftbukkit/tag/CraftItemTag.java index b6b90e3e56..4a1a45257f 100644 --- a/src/main/java/org/bukkit/craftbukkit/tag/CraftItemTag.java +++ b/src/main/java/org/bukkit/craftbukkit/tag/CraftItemTag.java @@ -4,25 +4,24 @@ import java.util.Collections; import java.util.Set; import java.util.stream.Collectors; import net.minecraft.server.Item; +import net.minecraft.server.MinecraftKey; +import net.minecraft.server.TagsServer; import org.bukkit.Material; -import org.bukkit.Tag; import org.bukkit.craftbukkit.util.CraftMagicNumbers; -public class CraftItemTag implements Tag { +public class CraftItemTag extends CraftTag { - private final net.minecraft.server.Tag handle; - - public CraftItemTag(net.minecraft.server.Tag handle) { - this.handle = handle; + public CraftItemTag(TagsServer registry, MinecraftKey tag) { + super(registry, tag); } @Override public boolean isTagged(Material item) { - return handle.isTagged(CraftMagicNumbers.getItem(item)); + return getHandle().isTagged(CraftMagicNumbers.getItem(item)); } @Override public Set getValues() { - return Collections.unmodifiableSet(handle.a().stream().map((item) -> CraftMagicNumbers.getMaterial(item)).collect(Collectors.toSet())); + return Collections.unmodifiableSet(getHandle().a().stream().map((item) -> CraftMagicNumbers.getMaterial(item)).collect(Collectors.toSet())); } } diff --git a/src/main/java/org/bukkit/craftbukkit/tag/CraftTag.java b/src/main/java/org/bukkit/craftbukkit/tag/CraftTag.java new file mode 100644 index 0000000000..39f157ffb8 --- /dev/null +++ b/src/main/java/org/bukkit/craftbukkit/tag/CraftTag.java @@ -0,0 +1,29 @@ +package org.bukkit.craftbukkit.tag; + +import net.minecraft.server.MinecraftKey; +import net.minecraft.server.TagsServer; +import org.bukkit.Keyed; +import org.bukkit.Tag; + +public abstract class CraftTag implements Tag { + + private final net.minecraft.server.TagsServer registry; + private final MinecraftKey tag; + // + private int version = -1; + private net.minecraft.server.Tag handle; + + public CraftTag(TagsServer registry, MinecraftKey tag) { + this.registry = registry; + this.tag = tag; + } + + protected net.minecraft.server.Tag getHandle() { + if (version != registry.version) { + handle = registry.b(tag); + version = registry.version; + } + + return handle; + } +}