Reflective api additions

This commit is contained in:
DanMB 2022-04-21 10:57:19 -07:00
parent 6e1c4f682e
commit 74172e3123
3 changed files with 55 additions and 41 deletions

View File

@ -0,0 +1,40 @@
package com.volmit.iris.core.tools;
import com.volmit.iris.util.data.B;
import org.bukkit.World;
import org.bukkit.block.data.BlockData;
/**
* This class is used by an external IrisLib for other plugins to interact with Iris. Do not change
* existing methods or their parameters as it will break the library that uses these methods
* feel free to add more methods so long as you also add the reflective methods to the library
*/
public class IrisReflectiveAPI {
public static boolean isIrisWorld(World world) {
return IrisToolbelt.isIrisWorld(world);
}
public static boolean isIrisStudioWorld(World world) {
return IrisToolbelt.isIrisStudioWorld(world);
}
public static void registerCustomBlockData(String namespace, String key, BlockData blockData) {
B.registerCustomBlockData(namespace, key, blockData);
}
public static void retainMantleData(String classname) {
IrisToolbelt.retainMantleDataForSlice(classname);
}
public static void setMantleData(World world, int x, int y, int z, Object data) {
IrisToolbelt.access(world).getEngine().getMantle().getMantle().set(x, y, z, data);
}
public static void deleteMantleData(World world, int x, int y, int z, Class c) {
IrisToolbelt.access(world).getEngine().getMantle().getMantle().remove(x, y, z, c);
}
public static Object getMantleData(World world, int x, int y, int z, Class c) {
return IrisToolbelt.access(world).getEngine().getMantle().getMantle().get(x, y, z, c);
}
}

View File

@ -235,45 +235,4 @@ public class IrisToolbelt {
if(e == null) {return;}
e.getEngine().getMantle().getMantle().remove(x, y - world.getMinHeight(), z, of);
}
/////////////////// REFLECTIVE METHODS //////////////////////////
// Copy this stuff to your project, it's safe to use with a
// bukkit api only.
/////////////////////////////////////////////////////////////////
public static void setup() throws Throwable {
Method m = Class.forName("com.volmit.iris.core.tools.IrisToolbelt").getDeclaredMethod("retainMantleDataForSlice", String.class);
m.invoke(null, String.class.getCanonicalName());
m.invoke(null, BlockData.class.getCanonicalName());
}
public static boolean hasMantleObject(World world, int x, int y, int z) {
return getMantleIdentity(world, x, y, z) != -1;
}
public static void deleteMantleBlock(World world, int x, int y, int z) {
try {
Method m = Class.forName("com.volmit.iris.core.tools.IrisToolbelt").getDeclaredMethod("deleteMantleData", World.class, int.class, int.class, int.class, Class.class);
m.invoke(null, world, x, y, z, BlockData.class);
m.invoke(null, world, x, y, z, String.class);
} catch(Throwable ignored) {}
}
public static BlockData getMantleBlock(World world, int x, int y, int z) {
try {
Method m = Class.forName("com.volmit.iris.core.tools.IrisToolbelt").getDeclaredMethod("getMantleData", World.class, int.class, int.class, int.class, Class.class);
BlockData s = (BlockData) m.invoke(null, world, x, y, z, BlockData.class);
if(s != null) {return s;}
} catch(Throwable ignored) {}
return null;
}
public static int getMantleIdentity(World world, int x, int y, int z) {
try {
Method m = Class.forName("com.volmit.iris.core.tools.IrisToolbelt").getDeclaredMethod("getMantleData", World.class, int.class, int.class, int.class, Class.class);
String s = (String) m.invoke(null, world, x, y, z, String.class);
if(s != null) {return Integer.parseInt(s.split("\\Q@\\E")[1]);}
} catch(Throwable ignored) {}
return -1;
}
}

View File

@ -22,6 +22,7 @@ import com.volmit.iris.Iris;
import com.volmit.iris.core.IrisSettings;
import com.volmit.iris.core.service.RegistrySVC;
import com.volmit.iris.util.collection.KList;
import com.volmit.iris.util.collection.KMap;
import com.volmit.iris.util.scheduling.ChronoLatch;
import it.unimi.dsi.fastutil.ints.Int2IntMap;
import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap;
@ -34,6 +35,7 @@ import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.Waterlogged;
import org.bukkit.block.data.type.Leaves;
import org.bukkit.block.data.type.PointedDripstone;
import org.checkerframework.checker.units.qual.K;
import java.util.Arrays;
import java.util.HashMap;
@ -44,6 +46,8 @@ import java.util.stream.Collectors;
import static org.bukkit.Material.*;
public class B {
private static final KMap<String, BlockData> custom = new KMap<>();
private static final Material AIR_MATERIAL = Material.AIR;
private static final BlockData AIR = AIR_MATERIAL.createBlockData();
private static final IntSet foliageCache = buildFoliageCache();
@ -405,6 +409,11 @@ public class B {
try {
String bd = bdxf.trim();
if(!custom.isEmpty() && custom.containsKey(bd))
{
return custom.get(bd);
}
if(bd.startsWith("minecraft:cauldron[level=")) {
bd = bd.replaceAll("\\Q:cauldron[\\E", ":water_cauldron[");
}
@ -660,6 +669,8 @@ public class B {
e.printStackTrace();
}
bt.addAll(custom.k());
try {
bt.addAll(Iris.service(RegistrySVC.class).getCustomBlockRegistry().compile());
} catch(Throwable e) {
@ -683,4 +694,8 @@ public class B {
public static boolean isWaterLogged(BlockData b) {
return (b instanceof Waterlogged) && ((Waterlogged) b).isWaterlogged();
}
public static void registerCustomBlockData(String namespace, String key, BlockData blockData) {
custom.put(namespace + ":" + key, blockData);
}
}