-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
92573c6
commit b87bed5
Showing
6 changed files
with
142 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
src/main/java/pl/craftserve/radiation/nms/V1_17_R1NmsBridge.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package pl.craftserve.radiation.nms; | ||
|
||
import org.bukkit.Material; | ||
import org.bukkit.NamespacedKey; | ||
import org.bukkit.World; | ||
import pl.craftserve.radiation.LugolsIodinePotion; | ||
|
||
import java.lang.reflect.Array; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.util.HashMap; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.UUID; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
public class V1_17_R1NmsBridge implements RadiationNmsBridge { | ||
static final Logger logger = Logger.getLogger(V1_17_R1NmsBridge.class.getName()); | ||
|
||
private final Class<?> itemClass; | ||
private final Class<?> iRegistryClass; | ||
private final Class<?> mobEffectClass; | ||
private final Class<?> potionRegistryClass; | ||
private final Class<?> potionBrewerClass; | ||
|
||
private final Method getItem; | ||
private final Method newMinecraftKey; | ||
private final Method getPotion; | ||
private final Method minHeightMethod; | ||
|
||
private final Object potionRegistry; | ||
|
||
private final Map<UUID, Integer> minWorldHeightMap = new HashMap<>(); | ||
|
||
public V1_17_R1NmsBridge(String version) { | ||
Objects.requireNonNull(version, "version"); | ||
|
||
try { | ||
this.itemClass = Class.forName("net.minecraft.world.item.Item"); | ||
this.iRegistryClass = Class.forName("net.minecraft.core.IRegistry"); | ||
this.mobEffectClass = Class.forName("net.minecraft.world.effect.MobEffect"); | ||
this.potionRegistryClass = Class.forName("net.minecraft.world.item.alchemy.PotionRegistry"); | ||
this.potionBrewerClass = Class.forName("net.minecraft.world.item.alchemy.PotionBrewer"); | ||
|
||
Class<?> craftMagicNumbers = Class.forName("org.bukkit.craftbukkit." + version + ".util.CraftMagicNumbers"); | ||
this.getItem = craftMagicNumbers.getMethod("getItem", Material.class); | ||
this.minHeightMethod = World.class.getMethod("getMinHeight"); | ||
|
||
Class<?> minecraftKey = Class.forName("net.minecraft.resources.MinecraftKey"); | ||
this.newMinecraftKey = minecraftKey.getMethod("a", String.class); | ||
this.potionRegistry = this.iRegistryClass.getDeclaredField("aa").get(null); | ||
this.getPotion = this.potionRegistry.getClass().getMethod("get", minecraftKey); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Failed to initialize 1.17.1 bridge", e); | ||
} | ||
} | ||
|
||
@Override | ||
public void registerLugolsIodinePotion(NamespacedKey potionKey, LugolsIodinePotion.Config.Recipe config) { | ||
Objects.requireNonNull(potionKey, "potionKey"); | ||
Objects.requireNonNull(config, "config"); | ||
|
||
try { | ||
String basePotionName = config.basePotion().name().toLowerCase(Locale.ROOT); | ||
Object basePotion = this.getPotion.invoke(this.potionRegistry, this.newMinecraftKey.invoke(null, basePotionName)); | ||
Object ingredient = this.getItem.invoke(null, config.ingredient()); | ||
|
||
Object mobEffectArray = Array.newInstance(this.mobEffectClass, 0); | ||
Object newPotion = this.potionRegistryClass.getConstructor(mobEffectArray.getClass()).newInstance(mobEffectArray); | ||
|
||
Method registerMethod = this.iRegistryClass.getDeclaredMethod("a", this.iRegistryClass, String.class, Object.class); | ||
Object potion = registerMethod.invoke(null, this.potionRegistry, potionKey.getKey(), newPotion); | ||
|
||
Method registerBrewingRecipe = this.potionBrewerClass.getDeclaredMethod("a", this.potionRegistryClass, this.itemClass, this.potionRegistryClass); | ||
registerBrewingRecipe.setAccessible(true); | ||
registerBrewingRecipe.invoke(null, basePotion, ingredient, potion); | ||
} catch (Exception e) { | ||
logger.log(Level.SEVERE, "Could not handle reflective operation.", e); | ||
} | ||
} | ||
|
||
@Override | ||
public void unregisterLugolsIodinePotion(NamespacedKey potionKey) { | ||
// todo unregister potion and brewing recipe | ||
} | ||
|
||
@Override | ||
public int getMinWorldHeight(World bukkitWorld) { | ||
Objects.requireNonNull(bukkitWorld, "bukkitWorld"); | ||
|
||
return this.minWorldHeightMap.computeIfAbsent(bukkitWorld.getUID(), worldId -> { | ||
try { | ||
return (int) this.minHeightMethod.invoke(bukkitWorld); | ||
} catch (IllegalAccessException | InvocationTargetException e) { | ||
logger.log(Level.SEVERE, "Could not handle min world height on world '" + bukkitWorld.getName() + "' ('" + worldId + "').", e); | ||
return 0; | ||
} | ||
}); | ||
} | ||
} |