-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for Minecraft 1.21.4, Added support for the new Registr…
…y class, Fixed misspells in system messages, Improved registry handling
- Loading branch information
Showing
16 changed files
with
295 additions
and
95 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
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
86 changes: 86 additions & 0 deletions
86
bukkit/src/main/java/com/gmail/picono435/picojobs/bukkit/utils/NamespacedLegacyUtils.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,86 @@ | ||
package com.gmail.picono435.picojobs.bukkit.utils; | ||
|
||
import com.gmail.picono435.picojobs.bukkit.PicoJobsBukkit; | ||
import org.apache.commons.lang3.Validate; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.Material; | ||
import org.bukkit.block.Biome; | ||
import org.bukkit.entity.EntityType; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Locale; | ||
|
||
/** | ||
* This class is the implementation for {@link NamespacedUtils} that works for | ||
* minecraft versions prior to 1.14.4 | ||
* | ||
* @see NamespacedUtils | ||
* @see NamespacedRegistryUtils | ||
*/ | ||
public class NamespacedLegacyUtils extends NamespacedUtils { | ||
|
||
public String getKeyFromObject(final Object value) { | ||
try { | ||
Method method = value.getClass().getMethod("getKey"); | ||
Object key = method.invoke(value); | ||
return key.toString(); | ||
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { | ||
return "minecraft:" + ((Enum<?>)value).name().toLowerCase(Locale.ROOT); // value is always an enum in legacy versions | ||
} | ||
} | ||
|
||
public Material matchMaterial(final String name) { | ||
return matchObject(name, Material.class); | ||
} | ||
|
||
public EntityType matchEntityType(final String name) { | ||
return matchObject(name, EntityType.class); | ||
} | ||
|
||
// Biome is no longer an enum so this was all converted into reflection so it compiles in 1.21.4 | ||
public Biome matchBiome(final String name) { | ||
try { | ||
Method matchObjectMethod = this.getClass().getMethod("matchObject", String.class, Class.class); | ||
return (Biome) matchObjectMethod.invoke(null, name, Biome.class); | ||
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public List<Material> getMaterials() { | ||
return Arrays.asList(Material.values()); | ||
} | ||
|
||
@Override | ||
public List<EntityType> getEntityTypes() { | ||
return PicoJobsBukkit.getNamespacedUtils().getEntityTypes(); | ||
} | ||
|
||
@Override | ||
public List<Biome> getBiomes() { | ||
return Arrays.asList(Biome.values()); | ||
} | ||
|
||
public static <E extends Enum<E>> E matchObject(final String name, final Class<E> type) { | ||
Validate.notNull(name, "Name cannot be null"); | ||
|
||
String filtered = name; | ||
if (filtered.startsWith("minecraft:")) { | ||
filtered = filtered.substring(("minecraft:").length()); | ||
} | ||
|
||
filtered = filtered.toUpperCase(Locale.ENGLISH); | ||
|
||
filtered = filtered.replaceAll("\\s+", "_").replaceAll("\\W", ""); | ||
|
||
if(type == Material.class && !Bukkit.getServer().getName().equalsIgnoreCase("Mohist")) { | ||
return (E) Material.getMaterial(filtered); | ||
} | ||
|
||
return Enum.valueOf(type, filtered); | ||
} | ||
} |
Oops, something went wrong.