-
Notifications
You must be signed in to change notification settings - Fork 549
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor versioned classes to use the Registry api
- Loading branch information
Showing
5 changed files
with
66 additions
and
174 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
28 changes: 7 additions & 21 deletions
28
...main/java/io/github/thebusybiscuit/slimefun4/utils/compatibility/VersionedEntityType.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 |
---|---|---|
@@ -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)); | ||
} | ||
} |
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
48 changes: 14 additions & 34 deletions
48
...main/java/io/github/thebusybiscuit/slimefun4/utils/compatibility/VersionedPotionType.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 |
---|---|---|
@@ -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)); | ||
} | ||
} |