SPIGOT-6033: Bukkit.getUnsafe().getMaterial() won't match renamed block materials

This commit is contained in:
md_5 2020-07-28 18:22:34 +10:00
parent 29cce6714a
commit 52fd29c0dc
No known key found for this signature in database
GPG Key ID: E8E901AC7C617C11
2 changed files with 15 additions and 5 deletions

View File

@ -33,6 +33,7 @@ import net.minecraft.server.MinecraftServer;
import net.minecraft.server.MojangsonParser;
import net.minecraft.server.NBTBase;
import net.minecraft.server.NBTTagCompound;
import net.minecraft.server.NBTTagString;
import net.minecraft.server.SavedFile;
import net.minecraft.server.SharedConstants;
import org.bukkit.Bukkit;
@ -169,6 +170,7 @@ public final class CraftMagicNumbers implements UnsafeValues {
@Override
public Material getMaterial(String material, int version) {
Preconditions.checkArgument(material != null, "material == null");
Preconditions.checkArgument(version <= this.getDataVersion(), "Newer version! Server downgrades are not supported!");
// Fastpath up to date materials
@ -176,13 +178,14 @@ public final class CraftMagicNumbers implements UnsafeValues {
return Material.getMaterial(material);
}
NBTTagCompound stack = new NBTTagCompound();
stack.setString("id", "minecraft:" + material.toLowerCase(Locale.ROOT));
Dynamic<NBTBase> name = new Dynamic<>(DynamicOpsNBT.a, NBTTagString.a("minecraft:" + material.toLowerCase(Locale.ROOT)));
Dynamic<NBTBase> converted = DataConverterRegistry.a().update(DataConverterTypes.ITEM_NAME, name, version, this.getDataVersion());
Dynamic<NBTBase> converted = DataConverterRegistry.a().update(DataConverterTypes.ITEM_STACK, new Dynamic<>(DynamicOpsNBT.a, stack), version, this.getDataVersion());
String newId = converted.get("id").asString("");
if (name.equals(converted)) {
converted = DataConverterRegistry.a().update(DataConverterTypes.BLOCK_NAME, name, version, this.getDataVersion());
}
return Material.matchMaterial(newId);
return Material.matchMaterial(converted.asString(""));
}
/**

View File

@ -4,6 +4,7 @@ import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import org.bukkit.Material;
import org.bukkit.craftbukkit.util.CraftMagicNumbers;
import org.bukkit.material.MaterialData;
import org.bukkit.support.AbstractTestingBase;
import org.junit.Assert;
@ -116,4 +117,10 @@ public class LegacyTest extends AbstractTestingBase {
Assert.assertFalse("Must iterate only modern materials", material.isLegacy());
}
}
@Test
public void testManual() {
Assert.assertEquals(Material.YELLOW_DYE, CraftMagicNumbers.INSTANCE.getMaterial("dandelion_yellow", 1631));
Assert.assertEquals(Material.OAK_WALL_SIGN, CraftMagicNumbers.INSTANCE.getMaterial("wall_sign", 1631));
}
}