Skip to content

Commit

Permalink
Added support for Minecraft 1.21.4, Added support for the new Registr…
Browse files Browse the repository at this point in the history
…y class, Fixed misspells in system messages, Improved registry handling
  • Loading branch information
Picono435 committed Feb 9, 2025
1 parent eb89628 commit 4d4c880
Show file tree
Hide file tree
Showing 16 changed files with 295 additions and 95 deletions.
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ plugins {
id 'signing'
}

sourceCompatibility = '17'
targetCompatibility = '17'
sourceCompatibility = '21'
targetCompatibility = '21'

if (System.getenv("STABLE_BUILD") == '0') {
rootProject.version = versioning.info.build + "-DEV"
Expand Down Expand Up @@ -96,7 +96,7 @@ allprojects {
}
}

configure([project(':common-mod'), project(':fabric'), project(':forge')]) {
configure([/*project(':common-mod'), project(':fabric'), project(':forge')*/]) {
apply plugin: "architectury-plugin"
apply plugin: "dev.architectury.loom"

Expand All @@ -115,7 +115,7 @@ configure([project(':common-mod'), project(':fabric'), project(':forge')]) {

tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
options.release = 17
options.release = 21
}

java {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import com.gmail.picono435.picojobs.bukkit.platform.*;
import com.gmail.picono435.picojobs.bukkit.platform.BukkitLoggerAdapter;
import com.gmail.picono435.picojobs.bukkit.utils.NamespacedLegacyUtils;
import com.gmail.picono435.picojobs.bukkit.utils.NamespacedRegistryUtils;
import com.gmail.picono435.picojobs.bukkit.utils.NamespacedUtils;
import com.gmail.picono435.picojobs.common.PicoJobsCommon;
import com.gmail.picono435.picojobs.common.platform.Platform;
import org.bstats.MetricsBase;
Expand All @@ -14,6 +17,7 @@
public class PicoJobsBukkit extends JavaPlugin {

private static PicoJobsBukkit instance;
private static NamespacedUtils namespacedUtils;

@Override
public void onLoad() {
Expand All @@ -32,6 +36,12 @@ public void onLoad() {
new BukkitSoftwareHooker(),
new BukkitRegistryCollector()
);

if(PicoJobsCommon.isMoreThan("1.20.1")) { // Why 1.20.1? Because it's when Registry#stream was implemented
namespacedUtils = new NamespacedRegistryUtils();
} else {
namespacedUtils = new NamespacedLegacyUtils();
}
}

@Override
Expand All @@ -56,4 +66,8 @@ public void onDisable() {
public static PicoJobsBukkit getInstance() {
return instance;
}

public static NamespacedUtils getNamespacedUtils() {
return namespacedUtils;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.gmail.picono435.picojobs.api.PicoJobsAPI;
import com.gmail.picono435.picojobs.api.field.RequiredField;
import com.gmail.picono435.picojobs.api.field.RequiredFieldType;
import com.gmail.picono435.picojobs.bukkit.utils.NamespacedLegegacyUtils;
import com.gmail.picono435.picojobs.bukkit.PicoJobsBukkit;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
Expand All @@ -29,19 +29,19 @@ public void onRegister() {
this.requiredField = new RequiredField<>("items", new RequiredFieldType<String, Material>(String.class, Material.class) {
@Override
public Material toValue(@Nonnull String primitive) {
return NamespacedLegegacyUtils.matchMaterial(primitive);
return PicoJobsBukkit.getNamespacedUtils().matchMaterial(primitive);
}

@Nonnull
@Override
public String toPrimitive(Material value) {
return NamespacedLegegacyUtils.getKeyByEnum(value);
return PicoJobsBukkit.getNamespacedUtils().getKeyFromObject(value);
}

@Nonnull
@Override
public List<Material> getSuggestions() {
return Arrays.asList(Material.values());
return PicoJobsBukkit.getNamespacedUtils().getMaterials();
}
}, true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.gmail.picono435.picojobs.api.WorkZoneImplementation;
import com.gmail.picono435.picojobs.api.field.RequiredField;
import com.gmail.picono435.picojobs.api.field.RequiredFieldType;
import com.gmail.picono435.picojobs.bukkit.utils.NamespacedLegegacyUtils;
import com.gmail.picono435.picojobs.bukkit.PicoJobsBukkit;
import org.bukkit.Bukkit;
import org.bukkit.block.Biome;
import org.bukkit.entity.Player;
Expand All @@ -15,6 +15,7 @@
import java.util.List;
import java.util.UUID;

// This is a LEGACY Biome implementation that only works for minecraft versions 1.21.3 and later
public class BiomeImplementation extends WorkZoneImplementation {

protected RequiredField<String, Biome> requiredField;
Expand All @@ -29,19 +30,19 @@ public void onRegister() {
this.requiredField = new RequiredField<>("biomes", new RequiredFieldType<String, Biome>(String.class, Biome.class) {
@Override
public Biome toValue(@Nonnull String primitive) {
return NamespacedLegegacyUtils.matchBiome(primitive);
return PicoJobsBukkit.getNamespacedUtils().matchBiome(primitive);
}

@Nonnull
@Override
public String toPrimitive(Biome value) {
return NamespacedLegegacyUtils.getKeyByEnum(value);
return PicoJobsBukkit.getNamespacedUtils().getKeyFromObject(value);
}

@Nonnull
@Override
public List<Biome> getSuggestions() {
return Arrays.asList(Biome.values());
return PicoJobsBukkit.getNamespacedUtils().getBiomes();
}
}, true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,39 @@
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.InventoryView;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class BukkitInventoryMenuListener implements Listener {

@EventHandler()
public void onInventoryClick(InventoryClickEvent event) {
if(event.getCurrentItem() == null || !event.getCurrentItem().hasItemMeta()) return;
if(InventoryMenuListener.onBasicClick(new BukkitSender(event.getWhoClicked()),
new BukkitInventoryAdapter(event.getInventory(), event.getView().getTitle()),
new BukkitInventoryAdapter(event.getInventory(), getInventoryTitle(event.getView())),
event.getCurrentItem())) {
event.setCancelled(true);
}
}

/**
* This exists only for a single reason. InventoryView became an interface in modern spigot API versions
* because of this the plugin stopped working in older ones. This method uses reflection to fix a
* {@link IncompatibleClassChangeError}.
*
* @param view the inventory view
* @return the inventory title
*/
private String getInventoryTitle(Object view) {
try {
Method method = view.getClass().getMethod("getTitle");
method.setAccessible(true);
return (String) method.invoke(view);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
return "Unknown Title";
}
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package com.gmail.picono435.picojobs.bukkit.platform;

import com.gmail.picono435.picojobs.bukkit.utils.NamespacedLegegacyUtils;
import com.gmail.picono435.picojobs.bukkit.PicoJobsBukkit;
import com.gmail.picono435.picojobs.common.PicoJobsCommon;
import com.gmail.picono435.picojobs.common.platform.inventory.InventoryAdapter;
import com.gmail.picono435.picojobs.common.platform.inventory.ItemAdapter;
import com.google.common.collect.MultimapBuilder;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.EquipmentSlotGroup;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.Locale;
import java.util.stream.Collectors;

public class BukkitInventoryAdapter implements InventoryAdapter {

Expand Down Expand Up @@ -78,7 +78,7 @@ public ItemAdapter toItemAdapter(Object object) {
}

public ItemStack toItemStack(ItemAdapter itemAdapter) {
Material material = NamespacedLegegacyUtils.matchMaterial(itemAdapter.getMaterial());
Material material = PicoJobsBukkit.getNamespacedUtils().matchMaterial(itemAdapter.getMaterial());
if(material == null) material = Material.STONE;
ItemStack itemStack;
if(itemAdapter.getDurability() != null) {
Expand All @@ -99,7 +99,17 @@ public ItemStack toItemStack(ItemAdapter itemAdapter) {
if(itemAdapter.isEnchanted()) itemMeta.addEnchant(Enchantment.FIRE_ASPECT, 1, true);

if(PicoJobsCommon.isMoreThan("1.20.5")) {
itemMeta.addAttributeModifier(Attribute.GENERIC_MOVEMENT_SPEED, new AttributeModifier("dummy", 0, AttributeModifier.Operation.ADD_SCALAR));
Attribute attribute;
if(PicoJobsCommon.isMoreThan("1.21.3")) {
attribute = Attribute.MOVEMENT_SPEED;
} else {
attribute = Attribute.valueOf("GENERIC_MOVEMENT_SPEED");
}
if(PicoJobsCommon.isMoreThan("1.21")) {
itemMeta.addAttributeModifier(attribute, new AttributeModifier(NamespacedKey.fromString("hide_attributes"), 0, AttributeModifier.Operation.ADD_SCALAR, EquipmentSlotGroup.ANY));
} else {
itemMeta.addAttributeModifier(attribute, new AttributeModifier("dummy", 0, AttributeModifier.Operation.ADD_SCALAR));
}
}

itemMeta.addItemFlags(ItemFlag.values());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.gmail.picono435.picojobs.bukkit.platform;

import com.gmail.picono435.picojobs.bukkit.PicoJobsBukkit;
import com.gmail.picono435.picojobs.common.platform.RegistryCollector;
import org.bukkit.Material;
import org.bukkit.entity.EntityType;
Expand All @@ -13,11 +14,11 @@ public class BukkitRegistryCollector implements RegistryCollector {

@Override
public List<String> getItemList() {
return Arrays.stream(Material.values()).map(material -> "minecraft:" + material.name().toLowerCase(Locale.ROOT)).collect(Collectors.toList());
return PicoJobsBukkit.getNamespacedUtils().getMaterialKeys();
}

@Override
public List<String> getEntityList() {
return Arrays.stream(EntityType.values()).map(material -> "minecraft:" + (material.getName() == null ? "unknown" : material.getName().toLowerCase(Locale.ROOT))).collect(Collectors.toList());
return PicoJobsBukkit.getNamespacedUtils().getEntityTypeKeys();
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.gmail.picono435.picojobs.bukkit.platform;

import com.gmail.picono435.picojobs.api.Type;
import com.gmail.picono435.picojobs.bukkit.utils.NamespacedLegegacyUtils;
import com.gmail.picono435.picojobs.bukkit.PicoJobsBukkit;
import com.gmail.picono435.picojobs.common.platform.WhitelistConverter;
import org.bukkit.DyeColor;
import org.bukkit.Material;
import org.bukkit.entity.EntityType;

import java.util.*;

Expand All @@ -14,11 +12,9 @@ public class BukkitWhitelistConverter implements WhitelistConverter {
public boolean inStringList(Object object, Type type, List<String> whitelist) {
switch(type.getWhitelistType()) {
case ITEM:
case BLOCK: {
return whitelist.contains(NamespacedLegegacyUtils.getKeyByEnum((Material) object));
}
case BLOCK:
case ENTITY: {
return whitelist.contains(NamespacedLegegacyUtils.getKeyByEnum((EntityType) object));
return whitelist.contains(PicoJobsBukkit.getNamespacedUtils().getKeyFromObject(object));
}
case DYE: {
return whitelist.contains(((DyeColor) object).name().toLowerCase(Locale.ROOT).toString());
Expand Down
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);
}
}
Loading

0 comments on commit 4d4c880

Please sign in to comment.