From 05ed0a3139e623501c3fad5d38edece4ff472f24 Mon Sep 17 00:00:00 2001 From: Wesley Wolfe Date: Tue, 14 Jan 2014 22:38:03 -0600 Subject: [PATCH] Implement UnsafeValues for give command. Addresses BUKKIT-5286 --- .../org/bukkit/craftbukkit/CraftServer.java | 8 ++++ .../craftbukkit/util/CraftMagicNumbers.java | 37 ++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java index f33681c5ca..f3e5200747 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java @@ -61,6 +61,7 @@ import org.bukkit.ChatColor; import org.bukkit.GameMode; import org.bukkit.OfflinePlayer; import org.bukkit.Server; +import org.bukkit.UnsafeValues; import org.bukkit.Warning.WarningState; import org.bukkit.World; import org.bukkit.World.Environment; @@ -93,6 +94,7 @@ import org.bukkit.craftbukkit.scoreboard.CraftScoreboardManager; import org.bukkit.craftbukkit.updater.AutoUpdater; import org.bukkit.craftbukkit.updater.BukkitDLUpdaterService; import org.bukkit.craftbukkit.util.CraftIconCache; +import org.bukkit.craftbukkit.util.CraftMagicNumbers; import org.bukkit.craftbukkit.util.DatFileFilter; import org.bukkit.craftbukkit.util.Versioning; import org.bukkit.entity.Player; @@ -1460,4 +1462,10 @@ public final class CraftServer implements Server { public int getIdleTimeout() { return console.aq(); // Should be getIdleTimeout } + + @Deprecated + @Override + public UnsafeValues getUnsafe() { + return CraftMagicNumbers.INSTANCE; + } } diff --git a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java index aea20a65de..89c599eebf 100644 --- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java +++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java @@ -1,11 +1,23 @@ package org.bukkit.craftbukkit.util; +import java.util.List; + import net.minecraft.server.Block; import net.minecraft.server.Blocks; import net.minecraft.server.Item; -import org.bukkit.Material; +import net.minecraft.server.MojangsonParser; +import net.minecraft.server.NBTTagCompound; + +import org.bukkit.Material; +import org.bukkit.UnsafeValues; +import org.bukkit.craftbukkit.inventory.CraftItemStack; +import org.bukkit.inventory.ItemStack; +import org.bukkit.util.StringUtil; + +@SuppressWarnings("deprecation") +public final class CraftMagicNumbers implements UnsafeValues { + public static final UnsafeValues INSTANCE = new CraftMagicNumbers(); -public final class CraftMagicNumbers { private CraftMagicNumbers() {} public static Block getBlock(org.bukkit.block.Block block) { @@ -67,4 +79,25 @@ public final class CraftMagicNumbers { return block; } + + @Override + public Material getMaterialFromInternalName(String name) { + return getMaterial((Item) Item.REGISTRY.a(name)); + } + + @Override + public List tabCompleteInternalMaterialName(String token, List completions) { + return StringUtil.copyPartialMatches(token, Item.REGISTRY.b(), completions); + } + + @Override + public ItemStack modifyItemStack(ItemStack stack, String arguments) { + net.minecraft.server.ItemStack nmsStack = CraftItemStack.asNMSCopy(stack); + + nmsStack.setTag((NBTTagCompound) MojangsonParser.a(arguments)); + + stack.setItemMeta(CraftItemStack.getItemMeta(nmsStack)); + + return stack; + } }