Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fr.creatruth.plugin.development;
- import fr.creatruth.plugin.creativeblocks.BlocksMain;
- import fr.creatruth.plugin.development.custom.*;
- import net.minecraft.server.v1_7_R4.*;
- import java.lang.reflect.Field;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.lang.reflect.Modifier;
- import java.util.logging.Logger;
- // TODO Rendre le placement des blocs partout possible
- public class CustomManager {
- private static Logger log = BlocksMain.instance.getLogger();
- public static void modifyBlocks() {
- log.info("Modify blocks ...");
- RegistryMaterials rm = Block.REGISTRY;
- Block leaves = new CustomLeaves1().d("leaves").c("leaves");
- rm.a(18, "leaves", leaves);
- reflexiveFinalField(Blocks.class, "LEAVES", leaves);
- Block glass = new CustomGlass(Material.SHATTERABLE, false).hard(.3F).sound(Block.j).c("glass");
- rm.a(20, "glass", glass);
- reflexiveFinalField(Blocks.class, "GLASS", glass);
- Block web = new CustomCobweb().g(1).hard(4).d("web").c("web");
- rm.a(30, "web", web);
- reflexiveFinalField(Blocks.class, "WEB", web);
- Block deadbush = new CustomDeadBush().hard(0).sound(Block.h).d("deadbush").c("deadbush");
- rm.a(32, "deadbush", deadbush);
- reflexiveFinalField(Blocks.class, "DEAD_BUSH", deadbush);
- Block yellow_flower = new CustomFlower(0).hard(0).sound(Block.h).d("flower_dandelion").c("flower1");
- rm.a(37, "yellow_flower", yellow_flower);
- reflexiveFinalField(Blocks.class, "YELLOW_FLOWER", yellow_flower);
- Block red_flower = new CustomFlower(1).hard(0).sound(Block.h).d("flower_rose").c("flower2");
- rm.a(38, "red_flower", red_flower);
- reflexiveFinalField(Blocks.class, "RED_FLOWER", red_flower);
- Block ice = new CustomIce().hard(.5F).sound(Block.k).g(3).d("ice").c("ice");
- rm.a(79, "ice", ice);
- reflexiveFinalField(Blocks.class, "ICE", ice);
- Block cactus = new CustomCactus().hard(.4F).sound(Block.l).d("cactus").c("cactus");
- rm.a(81, "cactus", cactus);
- reflexiveFinalField(Blocks.class, "CACTUS", cactus);
- Block fence = new CustomFence("planks_oak", Material.WOOD).hard(.7F).sound(Block.f).c("fence");
- rm.a(85, "fence", fence);
- reflexiveFinalField(Blocks.class, "FENCE", fence);
- Block glowstone = new CustomGlowstone(Material.SHATTERABLE).hard(.3F).sound(Block.k).a(1).d("lightgem").c("glowstone");
- rm.a(89, "glowstone", glowstone);
- reflexiveFinalField(Blocks.class, "GLOWSTONE", glowstone);
- Block nether_brick_fence = new CustomFence("nether_brick", Material.STONE).hard(.7F).sound(Block.i).c("netherFence");
- rm.a(113, "nether_brick_fence", nether_brick_fence);
- reflexiveFinalField(Blocks.class, "NETHER_FENCE", nether_brick_fence);
- Block leaves2 = new CustomLeaves2().d("leaves").c("leaves");
- rm.a(161, "leaves2", leaves2);
- reflexiveFinalField(Blocks.class, "LEAVES2", leaves2);
- shatterable();
- ice();
- plant();
- leaves();
- Item.l();
- System.out.print(Blocks.FENCE.stepSound.getStepSound());
- }
- public static void restoreBlocks() {
- log.info("To restore or remove a block modification, you must stop and restart your server.");
- }
- private static void shatterable() {
- reflexiveField(Material.SHATTERABLE, Material.class, "K", false); // Empêcher le placement de certains blocs ?
- reflexiveField(Material.SHATTERABLE, Material.class, "O", false); // Cassable en GM 2 ?
- }
- private static void ice() {
- reflexiveField(Material.ICE, Material.class, "K", false);
- reflexiveField(Material.ICE, Material.class, "O", false);
- }
- private static void plant() {
- reflexiveField(Material.PLANT, Material.class, "N", 0);
- }
- private static void leaves() {
- reflexiveField(Material.LEAVES, Material.class, "K", false);
- }
- /****************************************************************************************/
- public static void reflexiveField(Object instance, String fieldName, Object value) {
- reflexiveField(instance, instance.getClass(), fieldName, value);
- }
- public static void reflexiveField(Object instance, Class clazz, String fieldName, Object value) {
- try {
- Field field = clazz.getDeclaredField(fieldName);
- field.setAccessible(true);
- field.set(instance, value);
- } catch (NoSuchFieldException | SecurityException | IllegalAccessException e) {
- log.info(e.toString());
- log.warning("Error: " + clazz.getSimpleName() + " Field : " + fieldName + " Value : " + value.toString());
- }
- }
- public static void reflexiveFinalField(Class clazz, String fieldName, Object value) {
- try {
- Field field = clazz.getDeclaredField(fieldName);
- field.setAccessible(true);
- Field modifiersField = Field.class.getDeclaredField("modifiers");
- modifiersField.setAccessible(true);
- modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
- field.set(null, value);
- } catch (NoSuchFieldException | SecurityException | IllegalAccessException e) {
- log.warning("Error: " + clazz.getSimpleName() + "Final field : " + fieldName + " Value : " + value.toString());
- }
- }
- public static void reflexiveFinalField(Object instance, Class clazz, String fieldName, Object value) {
- try {
- Field field = clazz.getDeclaredField(fieldName);
- field.setAccessible(true);
- Field modifiersField = Field.class.getDeclaredField("modifiers");
- modifiersField.setAccessible(true);
- modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
- field.set(instance, value);
- } catch (NoSuchFieldException | SecurityException | IllegalAccessException e) {
- e.printStackTrace();
- }
- }
- public static Object reflectiveObject(Object instance, Class clazz, String fieldName) {
- try {
- Field field = clazz.getDeclaredField(fieldName);
- field.setAccessible(true);
- return field.get(instance);
- } catch (Exception e) {
- log.warning("Error: " + clazz.getSimpleName() + " Field : " + fieldName + " can't get object !");
- return null;
- }
- }
- public static Method reflexiveMethod(Class<?> clazz, String methodName, Class... args) {
- try {
- return clazz.getDeclaredMethod(methodName, args);
- } catch (NoSuchMethodException e) {
- log.warning("Error: " + clazz.getSimpleName() + " Method : " + methodName + " not found !");
- return null;
- }
- }
- public static void invoke(Object instance, Method method, Object... args) {
- try {
- method.setAccessible(true);
- method.invoke(instance, args);
- } catch (IllegalAccessException | InvocationTargetException e) {
- log.warning("Error: " + instance.getClass().getSimpleName() + " Method : " + method.getName() + " can't be invoke !");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement