Advertisement
Guest User

Untitled

a guest
May 6th, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.27 KB | None | 0 0
  1. package fr.creatruth.plugin.development;
  2.  
  3. import fr.creatruth.plugin.creativeblocks.BlocksMain;
  4. import fr.creatruth.plugin.development.custom.*;
  5. import net.minecraft.server.v1_7_R4.*;
  6.  
  7. import java.lang.reflect.Field;
  8. import java.lang.reflect.InvocationTargetException;
  9. import java.lang.reflect.Method;
  10. import java.lang.reflect.Modifier;
  11. import java.util.logging.Logger;
  12.  
  13. // TODO Rendre le placement des blocs partout possible
  14. public class CustomManager {
  15.  
  16.     private static Logger log = BlocksMain.instance.getLogger();
  17.  
  18.     public static void modifyBlocks() {
  19.         log.info("Modify blocks ...");
  20.         RegistryMaterials rm = Block.REGISTRY;
  21.  
  22.         Block leaves = new CustomLeaves1().d("leaves").c("leaves");
  23.         rm.a(18, "leaves", leaves);
  24.         reflexiveFinalField(Blocks.class, "LEAVES", leaves);
  25.  
  26.         Block glass = new CustomGlass(Material.SHATTERABLE, false).hard(.3F).sound(Block.j).c("glass");
  27.         rm.a(20, "glass", glass);
  28.         reflexiveFinalField(Blocks.class, "GLASS", glass);
  29.  
  30.         Block web = new CustomCobweb().g(1).hard(4).d("web").c("web");
  31.         rm.a(30, "web", web);
  32.         reflexiveFinalField(Blocks.class, "WEB", web);
  33.  
  34.         Block deadbush = new CustomDeadBush().hard(0).sound(Block.h).d("deadbush").c("deadbush");
  35.         rm.a(32, "deadbush", deadbush);
  36.         reflexiveFinalField(Blocks.class, "DEAD_BUSH", deadbush);
  37.  
  38.         Block yellow_flower = new CustomFlower(0).hard(0).sound(Block.h).d("flower_dandelion").c("flower1");
  39.         rm.a(37, "yellow_flower", yellow_flower);
  40.         reflexiveFinalField(Blocks.class, "YELLOW_FLOWER", yellow_flower);
  41.  
  42.         Block red_flower = new CustomFlower(1).hard(0).sound(Block.h).d("flower_rose").c("flower2");
  43.         rm.a(38, "red_flower", red_flower);
  44.         reflexiveFinalField(Blocks.class, "RED_FLOWER", red_flower);
  45.  
  46.         Block ice = new CustomIce().hard(.5F).sound(Block.k).g(3).d("ice").c("ice");
  47.         rm.a(79, "ice", ice);
  48.         reflexiveFinalField(Blocks.class, "ICE", ice);
  49.  
  50.         Block cactus = new CustomCactus().hard(.4F).sound(Block.l).d("cactus").c("cactus");
  51.         rm.a(81, "cactus", cactus);
  52.         reflexiveFinalField(Blocks.class, "CACTUS", cactus);
  53.  
  54.         Block fence = new CustomFence("planks_oak", Material.WOOD).hard(.7F).sound(Block.f).c("fence");
  55.         rm.a(85, "fence", fence);
  56.         reflexiveFinalField(Blocks.class, "FENCE", fence);
  57.  
  58.         Block glowstone = new CustomGlowstone(Material.SHATTERABLE).hard(.3F).sound(Block.k).a(1).d("lightgem").c("glowstone");
  59.         rm.a(89, "glowstone", glowstone);
  60.         reflexiveFinalField(Blocks.class, "GLOWSTONE", glowstone);
  61.  
  62.         Block nether_brick_fence = new CustomFence("nether_brick", Material.STONE).hard(.7F).sound(Block.i).c("netherFence");
  63.         rm.a(113, "nether_brick_fence", nether_brick_fence);
  64.         reflexiveFinalField(Blocks.class, "NETHER_FENCE", nether_brick_fence);
  65.  
  66.         Block leaves2 = new CustomLeaves2().d("leaves").c("leaves");
  67.         rm.a(161, "leaves2", leaves2);
  68.         reflexiveFinalField(Blocks.class, "LEAVES2", leaves2);
  69.  
  70.         shatterable();
  71.         ice();
  72.         plant();
  73.         leaves();
  74.         Item.l();
  75.  
  76.         System.out.print(Blocks.FENCE.stepSound.getStepSound());
  77.     }
  78.  
  79.     public static void restoreBlocks() {
  80.         log.info("To restore or remove a block modification, you must stop and restart your server.");
  81.     }
  82.  
  83.     private static void shatterable() {
  84.         reflexiveField(Material.SHATTERABLE, Material.class, "K", false); // Empêcher le placement de certains blocs ?
  85.         reflexiveField(Material.SHATTERABLE, Material.class, "O", false); // Cassable en GM 2 ?
  86.     }
  87.  
  88.     private static void ice() {
  89.         reflexiveField(Material.ICE, Material.class, "K", false);
  90.         reflexiveField(Material.ICE, Material.class, "O", false);
  91.     }
  92.  
  93.     private static void plant() {
  94.         reflexiveField(Material.PLANT, Material.class, "N", 0);
  95.     }
  96.  
  97.     private static void leaves() {
  98.         reflexiveField(Material.LEAVES, Material.class, "K", false);
  99.     }
  100.  
  101.  
  102.     /****************************************************************************************/
  103.  
  104.  
  105.     public static void reflexiveField(Object instance, String fieldName, Object value) {
  106.         reflexiveField(instance, instance.getClass(), fieldName, value);
  107.     }
  108.  
  109.     public static void reflexiveField(Object instance, Class clazz, String fieldName, Object value) {
  110.         try {
  111.             Field field = clazz.getDeclaredField(fieldName);
  112.             field.setAccessible(true);
  113.             field.set(instance, value);
  114.         } catch (NoSuchFieldException | SecurityException | IllegalAccessException e) {
  115.             log.info(e.toString());
  116.             log.warning("Error: " + clazz.getSimpleName() + " Field : " + fieldName + " Value : " + value.toString());
  117.         }
  118.     }
  119.  
  120.     public static void reflexiveFinalField(Class clazz, String fieldName, Object value) {
  121.         try {
  122.         Field field = clazz.getDeclaredField(fieldName);
  123.         field.setAccessible(true);
  124.  
  125.         Field modifiersField = Field.class.getDeclaredField("modifiers");
  126.         modifiersField.setAccessible(true);
  127.         modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
  128.  
  129.         field.set(null, value);
  130.         } catch (NoSuchFieldException | SecurityException | IllegalAccessException e) {
  131.             log.warning("Error: " + clazz.getSimpleName() + "Final field : " + fieldName + " Value : " + value.toString());
  132.         }
  133.     }
  134.  
  135.     public static void reflexiveFinalField(Object instance, Class clazz, String fieldName, Object value) {
  136.         try {
  137.             Field field = clazz.getDeclaredField(fieldName);
  138.             field.setAccessible(true);
  139.  
  140.             Field modifiersField = Field.class.getDeclaredField("modifiers");
  141.             modifiersField.setAccessible(true);
  142.             modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
  143.  
  144.             field.set(instance, value);
  145.         } catch (NoSuchFieldException | SecurityException | IllegalAccessException e) {
  146.             e.printStackTrace();
  147.         }
  148.     }
  149.  
  150.     public static Object reflectiveObject(Object instance, Class clazz, String fieldName) {
  151.         try {
  152.             Field field = clazz.getDeclaredField(fieldName);
  153.             field.setAccessible(true);
  154.             return field.get(instance);
  155.         } catch (Exception e) {
  156.             log.warning("Error: " + clazz.getSimpleName() + " Field : " + fieldName + " can't get object !");
  157.             return null;
  158.         }
  159.     }
  160.  
  161.     public static Method reflexiveMethod(Class<?> clazz, String methodName, Class... args) {
  162.         try {
  163.             return clazz.getDeclaredMethod(methodName, args);
  164.         } catch (NoSuchMethodException e) {
  165.             log.warning("Error: " + clazz.getSimpleName() + " Method : " + methodName + " not found !");
  166.             return null;
  167.         }
  168.     }
  169.  
  170.     public static void invoke(Object instance, Method method, Object... args) {
  171.         try {
  172.             method.setAccessible(true);
  173.             method.invoke(instance, args);
  174.         } catch (IllegalAccessException | InvocationTargetException e) {
  175.             log.warning("Error: " + instance.getClass().getSimpleName() + " Method : " + method.getName() + " can't be invoke !");
  176.         }
  177.     }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement