Skip to content

Commit

Permalink
Refactor versioned classes to use the Registry api
Browse files Browse the repository at this point in the history
  • Loading branch information
md5sha256 committed Aug 11, 2024
1 parent d02dff9 commit b529c34
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 174 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package io.github.thebusybiscuit.slimefun4.utils.compatibility;

import java.lang.reflect.Field;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.enchantments.Enchantment;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.bukkit.enchantments.Enchantment;

import io.github.thebusybiscuit.slimefun4.api.MinecraftVersion;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;

// https://hub.spigotmc.org/stash/projects/SPIGOT/repos/craftbukkit/browse/src/main/java/org/bukkit/craftbukkit/legacy/FieldRename.java?until=2a6207fe150b6165722fce94c83cc1f206620ab5&untilPath=src%2Fmain%2Fjava%2Forg%2Fbukkit%2Fcraftbukkit%2Flegacy%2FFieldRename.java#86-110
public class VersionedEnchantment {

public static final Enchantment EFFICIENCY;
public static final Enchantment UNBREAKING;
public static final Enchantment PROTECTION;
Expand All @@ -22,51 +19,30 @@ public class VersionedEnchantment {
public static final Enchantment FORTUNE;

static {
MinecraftVersion version = Slimefun.getMinecraftVersion();

// DIG_SPEED is renamed to EFFICIENCY in 1.20.5
EFFICIENCY = version.isAtLeast(MinecraftVersion.MINECRAFT_1_20_5)
? Enchantment.EFFICIENCY
: getKey("DIG_SPEED");
EFFICIENCY = getKey("efficiency");

// DURABILITY is renamed to UNBREAKING in 1.20.5
UNBREAKING = version.isAtLeast(MinecraftVersion.MINECRAFT_1_20_5)
? Enchantment.UNBREAKING
: getKey("DURABILITY");
UNBREAKING = getKey("unbreaking");

// PROTECTION_ENVIRONMENTAL is renamed to PROTECTION in 1.20.5
PROTECTION = version.isAtLeast(MinecraftVersion.MINECRAFT_1_20_5)
? Enchantment.PROTECTION
: getKey("PROTECTION_ENVIRONMENTAL");
PROTECTION = getKey("protection");

// DAMAGE_ALL is renamed to SHARPNESS in 1.20.5
SHARPNESS = version.isAtLeast(MinecraftVersion.MINECRAFT_1_20_5)
? Enchantment.SHARPNESS
: getKey("DAMAGE_ALL");
SHARPNESS = getKey("sharpness");

// LUCK is renamed to LUCK_OF_THE_SEA in 1.20.5
LUCK_OF_THE_SEA = version.isAtLeast(MinecraftVersion.MINECRAFT_1_20_5)
? Enchantment.LUCK_OF_THE_SEA
: getKey("LUCK");
LUCK_OF_THE_SEA = getKey("luck_of_the_sea");

// WATER_WORKER is renamed to AQUA_AFFINITY in 1.20.5
AQUA_AFFINITY = version.isAtLeast(MinecraftVersion.MINECRAFT_1_20_5)
? Enchantment.AQUA_AFFINITY
: getKey("WATER_WORKER");
AQUA_AFFINITY = getKey("aqua_affinity");

// LOOT_BONUS_BLOCKS is renamed to FORTUNE in 1.20.5
FORTUNE = version.isAtLeast(MinecraftVersion.MINECRAFT_1_20_5)
? Enchantment.FORTUNE
: getKey("LOOT_BONUS_BLOCKS");
FORTUNE = getKey("fortune");
}

@Nullable
private static Enchantment getKey(@Nonnull String key) {
try {
Field field = Enchantment.class.getDeclaredField(key);
return (Enchantment) field.get(null);
} catch(Exception e) {
return null;
}
return Registry.ENCHANTMENT.get(NamespacedKey.minecraft(key));
}
}
Original file line number Diff line number Diff line change
@@ -1,42 +1,28 @@
package io.github.thebusybiscuit.slimefun4.utils.compatibility;

import java.lang.reflect.Field;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.entity.EntityType;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.bukkit.entity.EntityType;

import io.github.thebusybiscuit.slimefun4.api.MinecraftVersion;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;

// https://hub.spigotmc.org/stash/projects/SPIGOT/repos/craftbukkit/browse/src/main/java/org/bukkit/craftbukkit/legacy/FieldRename.java?until=2a6207fe150b6165722fce94c83cc1f206620ab5&untilPath=src%2Fmain%2Fjava%2Forg%2Fbukkit%2Fcraftbukkit%2Flegacy%2FFieldRename.java#158-193
public class VersionedEntityType {

public static final EntityType MOOSHROOM;
public static final EntityType SNOW_GOLEM;

static {
MinecraftVersion version = Slimefun.getMinecraftVersion();

// MUSHROOM_COW is renamed to MOOSHROOM in 1.20.5
MOOSHROOM = version.isAtLeast(MinecraftVersion.MINECRAFT_1_20_5)
? EntityType.MOOSHROOM
: getKey("MUSHROOM_COW");
MOOSHROOM = getKey("mooshroom");

// SNOWMAN is renamed to SNOW_GOLEM in 1.20.5
SNOW_GOLEM = version.isAtLeast(MinecraftVersion.MINECRAFT_1_20_5)
? EntityType.SNOW_GOLEM
: getKey("SNOWMAN");
SNOW_GOLEM = getKey("snow_golem");
}

@Nullable
private static EntityType getKey(@Nonnull String key) {
try {
Field field = EntityType.class.getDeclaredField(key);
return (EntityType) field.get(null);
} catch(Exception e) {
return null;
}
return Registry.ENTITY_TYPE.get(NamespacedKey.minecraft(key));
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package io.github.thebusybiscuit.slimefun4.utils.compatibility;

import java.lang.reflect.Field;
import io.github.thebusybiscuit.slimefun4.api.MinecraftVersion;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
import org.bukkit.NamespacedKey;
import org.bukkit.Particle;
import org.bukkit.Registry;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.bukkit.Particle;

import io.github.thebusybiscuit.slimefun4.api.MinecraftVersion;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;

// https://hub.spigotmc.org/stash/projects/SPIGOT/repos/craftbukkit/browse/src/main/java/org/bukkit/craftbukkit/legacy/FieldRename.java?until=2a6207fe150b6165722fce94c83cc1f206620ab5&untilPath=src%2Fmain%2Fjava%2Forg%2Fbukkit%2Fcraftbukkit%2Flegacy%2FFieldRename.java#281-318
public class VersionedParticle {

public static final Particle DUST;
public static final Particle SMOKE;
public static final Particle HAPPY_VILLAGER;
Expand All @@ -26,53 +25,32 @@ public class VersionedParticle {
MinecraftVersion version = Slimefun.getMinecraftVersion();

// REDSTONE is renamed to DUST in 1.20.5
DUST = version.isAtLeast(MinecraftVersion.MINECRAFT_1_20_5)
? Particle.DUST
: getKey("REDSTONE");
DUST = getKey("dust");

// SMOKE_NORMAL is renamed to SMOKE in 1.20.5
SMOKE = version.isAtLeast(MinecraftVersion.MINECRAFT_1_20_5)
? Particle.SMOKE
: getKey("SMOKE_NORMAL");

SMOKE = getKey("smoke");

// VILLAGER_HAPPY is renamed to HAPPY_VILLAGER in 1.20.5
HAPPY_VILLAGER = version.isAtLeast(MinecraftVersion.MINECRAFT_1_20_5)
? Particle.HAPPY_VILLAGER
: getKey("VILLAGER_HAPPY");
HAPPY_VILLAGER = getKey("happy_villager");

// CRIT_MAGIC is renamed to ENCHANTED_HIT in 1.20.5
ENCHANTED_HIT = version.isAtLeast(MinecraftVersion.MINECRAFT_1_20_5)
? Particle.ENCHANTED_HIT
: getKey("CRIT_MAGIC");

ENCHANTED_HIT = getKey("enchanted_hit");

// EXPLOSION_LARGE is renamed to EXPLOSION in 1.20.5
EXPLOSION = version.isAtLeast(MinecraftVersion.MINECRAFT_1_20_5)
? Particle.EXPLOSION
: getKey("EXPLOSION_LARGE");

EXPLOSION = getKey("explosion");

// SPELL_WITCH is renamed to WITCH in 1.20.5
WITCH = version.isAtLeast(MinecraftVersion.MINECRAFT_1_20_5)
? Particle.WITCH
: getKey("SPELL_WITCH");

WITCH = getKey("witch");

// FIREWORKS_SPARK is renamed to FIREWORK in 1.20.5
FIREWORK = version.isAtLeast(MinecraftVersion.MINECRAFT_1_20_5)
? Particle.FIREWORK
: getKey("FIREWORKS_SPARK");

FIREWORK = getKey("firework");

// ENCHANTMENT_TABLE is renamed to ENCHANT in 1.20.5
ENCHANT = version.isAtLeast(MinecraftVersion.MINECRAFT_1_20_5)
? Particle.ENCHANT
: getKey("ENCHANTMENT_TABLE");
ENCHANT = getKey("enchant");
}

@Nullable
private static Particle getKey(@Nonnull String key) {
try {
Field field = Particle.class.getDeclaredField(key);
return (Particle) field.get(null);
} catch(Exception e) {
return null;
}
return Registry.PARTICLE_TYPE.get(NamespacedKey.minecraft(key));
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package io.github.thebusybiscuit.slimefun4.utils.compatibility;

import java.lang.reflect.Field;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.potion.PotionEffectType;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.bukkit.potion.PotionEffectType;

import io.github.thebusybiscuit.slimefun4.api.MinecraftVersion;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;

// https://hub.spigotmc.org/stash/projects/SPIGOT/repos/craftbukkit/browse/src/main/java/org/bukkit/craftbukkit/legacy/FieldRename.java?until=2a6207fe150b6165722fce94c83cc1f206620ab5&untilPath=src%2Fmain%2Fjava%2Forg%2Fbukkit%2Fcraftbukkit%2Flegacy%2FFieldRename.java#216-228
public class VersionedPotionEffectType {

Expand All @@ -24,52 +21,27 @@ public class VersionedPotionEffectType {
public static final PotionEffectType RESISTANCE;

static {
MinecraftVersion version = Slimefun.getMinecraftVersion();

SLOWNESS = version.isAtLeast(MinecraftVersion.MINECRAFT_1_20_5)
? PotionEffectType.SLOWNESS
: getKey("SLOW");
SLOWNESS = getKey("slowness");

HASTE = version.isAtLeast(MinecraftVersion.MINECRAFT_1_20_5)
? PotionEffectType.HASTE
: getKey("FAST_DIGGING");
HASTE = getKey("haste");

MINING_FATIGUE = version.isAtLeast(MinecraftVersion.MINECRAFT_1_20_5)
? PotionEffectType.MINING_FATIGUE
: getKey("SLOW_DIGGING");
MINING_FATIGUE = getKey("mining_fatigue");

STRENGTH = version.isAtLeast(MinecraftVersion.MINECRAFT_1_20_5)
? PotionEffectType.STRENGTH
: getKey("INCREASE_DAMAGE");
STRENGTH = getKey("strength");

INSTANT_HEALTH = version.isAtLeast(MinecraftVersion.MINECRAFT_1_20_5)
? PotionEffectType.INSTANT_HEALTH
: getKey("HEAL");
INSTANT_HEALTH = getKey("instant_health");

INSTANT_DAMAGE = version.isAtLeast(MinecraftVersion.MINECRAFT_1_20_5)
? PotionEffectType.INSTANT_DAMAGE
: getKey("HARM");
INSTANT_DAMAGE = getKey("instant_damage");

JUMP_BOOST = version.isAtLeast(MinecraftVersion.MINECRAFT_1_20_5)
? PotionEffectType.JUMP_BOOST
: getKey("JUMP");
JUMP_BOOST = getKey("jump_boost");

NAUSEA = version.isAtLeast(MinecraftVersion.MINECRAFT_1_20_5)
? PotionEffectType.NAUSEA
: getKey("CONFUSION");
NAUSEA = getKey("nausea");

RESISTANCE = version.isAtLeast(MinecraftVersion.MINECRAFT_1_20_5)
? PotionEffectType.RESISTANCE
: getKey("DAMAGE_RESISTANCE");
RESISTANCE = getKey("resistance");
}

@Nullable
private static PotionEffectType getKey(@Nonnull String key) {
try {
Field field = PotionEffectType.class.getDeclaredField(key);
return (PotionEffectType) field.get(null);
} catch(Exception e) {
return null;
}
return Registry.POTION_EFFECT_TYPE.get(NamespacedKey.minecraft(key));
}
}
Original file line number Diff line number Diff line change
@@ -1,55 +1,35 @@
package io.github.thebusybiscuit.slimefun4.utils.compatibility;

import java.lang.reflect.Field;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.potion.PotionType;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.bukkit.potion.PotionType;

import io.github.thebusybiscuit.slimefun4.api.MinecraftVersion;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;

// https://hub.spigotmc.org/stash/projects/SPIGOT/repos/craftbukkit/browse/src/main/java/org/bukkit/craftbukkit/legacy/FieldRename.java?until=2a6207fe150b6165722fce94c83cc1f206620ab5&untilPath=src%2Fmain%2Fjava%2Forg%2Fbukkit%2Fcraftbukkit%2Flegacy%2FFieldRename.java#242-250
public class VersionedPotionType {

public static final PotionType LEAPING;
public static final PotionType SWIFTNESS;
public static final PotionType HEALING;
public static final PotionType HARMING;
public static final PotionType REGENERATION;

static {
MinecraftVersion version = Slimefun.getMinecraftVersion();

LEAPING = version.isAtLeast(MinecraftVersion.MINECRAFT_1_20_5)
? PotionType.LEAPING
: getKey("JUMP");

SWIFTNESS = version.isAtLeast(MinecraftVersion.MINECRAFT_1_20_5)
? PotionType.SWIFTNESS
: getKey("SPEED");

HEALING = version.isAtLeast(MinecraftVersion.MINECRAFT_1_20_5)
? PotionType.HEALING
: getKey("INSTANT_HEAL");

HARMING = version.isAtLeast(MinecraftVersion.MINECRAFT_1_20_5)
? PotionType.HARMING
: getKey("INSTANT_DAMAGE");

REGENERATION = version.isAtLeast(MinecraftVersion.MINECRAFT_1_20_5)
? PotionType.REGENERATION
: getKey("REGEN");
LEAPING = getKey("leaping");

SWIFTNESS = getKey("swiftness");

HEALING = getKey("healing");

HARMING = getKey("harming");

REGENERATION = getKey("regeneration");
}

@Nullable
private static PotionType getKey(@Nonnull String key) {
try {
Field field = PotionType.class.getDeclaredField(key);
return (PotionType) field.get(null);
} catch(Exception e) {
return null;
}
return Registry.POTION.get(NamespacedKey.minecraft(key));
}
}

0 comments on commit b529c34

Please sign in to comment.