まだチェストにしか対応してないです。
かまどとかの対応はまたいつか…
ロックが無い状態でgetLockの返り値は空文字です。
なので、setLockで空文字を指定すればロックを外す事ができます。
今のところの問題としてラージチェストでのLock変更がいまいち分からないこと。
こんなの面倒なのでさっさとBukkitAPIで対応して欲しいですね。
import java.lang.reflect.Constructor; | |
import java.lang.reflect.Method; | |
import org.bukkit.Bukkit; | |
import org.bukkit.block.Block; | |
public class ChestLockReflect { | |
private static String version; | |
private static boolean v1_8 = false; | |
static { | |
version = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3]; | |
if(version.startsWith("v1_8")) { | |
v1_8 = true; | |
} | |
} | |
private static Class<?> getNMSClass(String s) throws Exception { | |
if(!v1_8) throw new NotSupportException("This server version '"+version+"' is not suppoer."); | |
Class<?> craftclass = Class.forName("net.minecraft.server."+version+"."+s); | |
return craftclass; | |
} | |
private static Class<?> getCraftClass(String s) throws Exception { | |
if(!v1_8) throw new NotSupportException("This server version '"+version+"' is not suppoer."); | |
Class<?> craftclass = Class.forName("org.bukkit.craftbukkit."+version+"."+s); | |
return craftclass; | |
} | |
private static class NotSupportException extends Exception{ | |
private static final long serialVersionUID = -4183390338320843307L; | |
public NotSupportException(String arg){ | |
super(arg); | |
} | |
} | |
private static Object getTEC(String path, String method, Block block) throws Exception { | |
Class<?> a = getCraftClass(path); | |
Constructor<?> b = a.getConstructor(new Class<?>[]{Block.class}); | |
Object CraftChest = b.newInstance(new Object[]{block}); | |
Method c = CraftChest.getClass().getMethod(method); | |
return c.invoke(CraftChest); | |
} | |
private static String getLock(Object tec) throws Exception { | |
Method d = tec.getClass().getMethod("i"); | |
Object nmsChestLock = d.invoke(tec); | |
Method e = nmsChestLock.getClass().getMethod("b"); | |
return (String)e.invoke(nmsChestLock); | |
} | |
public static class ChestLock { | |
private Object nmsTileEntityContainer; | |
private String lock = ""; | |
public ChestLock(Block block) { | |
try { | |
switch(block.getType()) { | |
case CHEST: { | |
nmsTileEntityContainer = ChestLockReflect.getTEC("block.CraftChest", "getTileEntity", block); | |
lock = ChestLockReflect.getLock(nmsTileEntityContainer); | |
} | |
default: | |
} | |
} catch(Throwable ex) { | |
ex.printStackTrace(); | |
} | |
} | |
public String getLock() { | |
return this.lock; | |
} | |
public void setLock(String lock) { | |
try { | |
Class<?> a = ChestLockReflect.getNMSClass("ChestLock"); | |
Constructor<?> b = a.getConstructor(new Class<?>[]{String.class}); | |
Object nmsChestLock = b.newInstance(new Object[]{lock}); | |
for(Method m : nmsTileEntityContainer.getClass().getMethods()) { | |
if(m.getName().equals("a")) { | |
try { | |
m.invoke(nmsTileEntityContainer, new Object[]{nmsChestLock}); | |
return; | |
} catch(Throwable ex2) { | |
continue; | |
} | |
} | |
} | |
} catch(Throwable ex1) { | |
ex1.printStackTrace(); | |
} | |
return; | |
} | |
public void setName(String name) { | |
try { | |
Method a = nmsTileEntityContainer.getClass().getMethod("a", new Class<?>[]{String.class}); | |
a.invoke(nmsTileEntityContainer, new Object[]{name}); | |
} catch(Throwable ex1) { | |
ex1.printStackTrace(); | |
} | |
return; | |
} | |
} | |
} |