65 lines
2.2 KiB
Java
65 lines
2.2 KiB
Java
/*
|
|
* Copyright (c) 2017 univento.eu - All rights reserved
|
|
* You are not allowed to use, distribute or modify this code
|
|
*/
|
|
|
|
package eu.univento.core.api.utils;
|
|
|
|
import eu.univento.core.Core;
|
|
import org.bukkit.Material;
|
|
import org.bukkit.block.Block;
|
|
|
|
import java.lang.reflect.Field;
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
/**
|
|
* @author joethei
|
|
* @version 1.0
|
|
*/
|
|
public class Lamps {
|
|
|
|
private static Object getNMCWorld(Object cW) throws ClassNotFoundException {
|
|
return Class.forName("net.minecraft.server." + Core.getNMSVersion() + ".World", false, Core.class.getClassLoader()).cast(cW);
|
|
}
|
|
|
|
private static Object getCraftWorld(Object worldInstance) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
|
|
return Class.forName("org.bukkit.craftbukkit." + Core.getNMSVersion() + ".CraftWorld", false, Core.class.getClassLoader()).cast(worldInstance);
|
|
}
|
|
|
|
private static Object getInstanceOfCW(Object cW) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
|
|
return cW.getClass().getDeclaredMethod("getHandle").invoke(cW);
|
|
}
|
|
|
|
public static void switchLight(Block block, boolean light) {
|
|
try {
|
|
Object cW = getNMCWorld(getInstanceOfCW(getCraftWorld(block.getWorld())));
|
|
if (light) {
|
|
setWorldStatic(cW, true);
|
|
block.setType(Material.REDSTONE_LAMP_ON);
|
|
setWorldStatic(cW, false);
|
|
} else {
|
|
block.setType(Material.REDSTONE_LAMP_OFF);
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
private static void setWorldStatic(Object cW, boolean static_boolean) throws IllegalAccessException {
|
|
Field field = null;
|
|
try {
|
|
field = cW.getClass().getField("isClientSide");
|
|
} catch (NoSuchFieldException e) {
|
|
try {
|
|
field = cW.getClass().getField("isStatic");
|
|
} catch (NoSuchFieldException e1) {
|
|
e1.printStackTrace();
|
|
}
|
|
}
|
|
|
|
assert field != null;
|
|
field.setAccessible(true);
|
|
field.set(cW, static_boolean);
|
|
}
|
|
|
|
} |