Advertisement
BillyGalbreath

Untitled

Oct 17th, 2022
1,232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.07 KB | None | 0 0
  1. package net.pl3x.plugin;
  2.  
  3. import com.google.gson.Gson;
  4. import com.google.gson.GsonBuilder;
  5. import net.minecraft.world.entity.ai.Brain;
  6. import net.minecraft.world.entity.ai.sensing.Sensor;
  7. import net.minecraft.world.entity.ai.sensing.SensorType;
  8. import org.bukkit.Bukkit;
  9. import org.bukkit.Location;
  10. import org.bukkit.World;
  11. import org.bukkit.craftbukkit.v1_19_R1.entity.CraftLivingEntity;
  12. import org.bukkit.entity.Entity;
  13. import org.bukkit.entity.EntityType;
  14. import org.bukkit.entity.LivingEntity;
  15. import org.bukkit.event.EventHandler;
  16. import org.bukkit.event.Listener;
  17. import org.bukkit.event.server.ServerLoadEvent;
  18. import org.bukkit.plugin.java.JavaPlugin;
  19. import org.bukkit.scheduler.BukkitRunnable;
  20.  
  21. import java.io.IOException;
  22. import java.io.RandomAccessFile;
  23. import java.lang.reflect.Field;
  24. import java.nio.ByteBuffer;
  25. import java.nio.channels.FileChannel;
  26. import java.nio.file.Path;
  27. import java.util.Arrays;
  28. import java.util.Locale;
  29. import java.util.Map;
  30. import java.util.TreeMap;
  31.  
  32. public class TestPlugin extends JavaPlugin {
  33.     @Override
  34.     public void onEnable() {
  35.         getServer().getPluginManager().registerEvents(new Listener() {
  36.             @EventHandler
  37.             public void on(ServerLoadEvent event) {
  38.                 // wait 5 seconds, then scan mobs
  39.                 new ScanMobs().runTaskLater(TestPlugin.this, 20);
  40.             }
  41.         }, this);
  42.     }
  43.  
  44.     public void start() {
  45.         //
  46.     }
  47.  
  48.     public static class ScanMobs extends BukkitRunnable {
  49.         @Override
  50.         public void run() {
  51.             System.out.println("Scanning mobs...");
  52.  
  53.             World world = Bukkit.getWorlds().get(0);
  54.             Location loc = new Location(world, 0, 0, 0);
  55.  
  56.             Map<String, Map<String, Integer>> map = new TreeMap<>();
  57.  
  58.             Arrays.stream(EntityType.values())
  59.                     .forEach(type -> {
  60.                         Class<? extends Entity> clazz = type.getEntityClass();
  61.                         if (clazz != null && LivingEntity.class.isAssignableFrom(clazz) && type != EntityType.PLAYER) {
  62.                             System.out.println("Type: " + type.name());
  63.                             Entity entity = world.spawn(loc, clazz);
  64.                             Brain<?> brain = ((CraftLivingEntity) entity).getHandle().getBrain();
  65.  
  66.                             Map<String, Integer> entityMap = map.computeIfAbsent(type.name().toLowerCase(Locale.ROOT), k -> new TreeMap<>());
  67.  
  68.                             try {
  69.                                 Field f = Brain.class.getDeclaredField("e"); // sensors
  70.                                 f.setAccessible(true);
  71.                                 var sensors = (Map<SensorType<?>, Sensor<?>>) f.get(brain);
  72.                                 for (Map.Entry<SensorType<?>, Sensor<?>> entry : sensors.entrySet()) {
  73.                                     Sensor<?> sensor = entry.getValue();
  74.                                     try {
  75.                                         Field i = Sensor.class.getDeclaredField("j"); // scanRate
  76.                                         i.setAccessible(true);
  77.                                         int scanRate = i.getInt(sensor);
  78.                                         String key = io.papermc.paper.util.ObfHelper.INSTANCE.deobfClassName(sensor.getClass().getName());
  79.                                         int lastSeparator = key.lastIndexOf('.');
  80.                                         if (lastSeparator != -1) {
  81.                                             key = key.substring(lastSeparator + 1);
  82.                                         }
  83.                                         key = key.toLowerCase(java.util.Locale.ROOT);
  84.  
  85.                                         entityMap.put(key, scanRate);
  86.  
  87.                                         System.out.println(key + ": " + scanRate);
  88.                                     } catch (NoSuchFieldException | IllegalAccessException e) {
  89.                                         throw new RuntimeException(e);
  90.                                     }
  91.                                 }
  92.                             } catch (NoSuchFieldException | IllegalAccessException e) {
  93.                                 throw new RuntimeException(e);
  94.                             }
  95.                         }
  96.                     });
  97.             System.out.println("Done.");
  98.  
  99.             Gson gson = new GsonBuilder()
  100.                     .setPrettyPrinting()
  101.                     .create();
  102.             String json = gson.toJson(map);
  103.  
  104.             System.out.println(json);
  105.  
  106.             write(json, Path.of("sensors.json"));
  107.         }
  108.  
  109.         public static void write(String str, Path path) {
  110.             try (
  111.                     RandomAccessFile raf = new RandomAccessFile(path.toFile(), "rw");
  112.                     FileChannel rw = raf.getChannel().truncate(0)
  113.             ) {
  114.                 ByteBuffer buf = rw.map(FileChannel.MapMode.READ_WRITE, 0, str.length());
  115.                 buf.put(str.getBytes());
  116.             } catch (IOException e) {
  117.                 throw new RuntimeException(e);
  118.             }
  119.         }
  120.     }
  121. }
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement