Skip to content

Commit

Permalink
#1424: Trial changing a small number of inner enums to classes/interf…
Browse files Browse the repository at this point in the history
…aces to better support custom values

This PR is a subset of the enum PR #931 and is designed as a low impact
trial run of the design and backwards compatibility to inform
subsequent development.

Additional plugin compatibility features may be available by setting
`settings.compatibility.enum-compatibility-mode` to `true` in
`bukkit.yml`.
  • Loading branch information
DerFrZocker authored and md-5 committed Jul 6, 2024
1 parent 66e78a9 commit 082aa51
Show file tree
Hide file tree
Showing 27 changed files with 1,419 additions and 112 deletions.
26 changes: 24 additions & 2 deletions src/main/java/org/bukkit/craftbukkit/CraftRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import net.minecraft.core.Holder;
import net.minecraft.core.IRegistry;
import net.minecraft.core.IRegistryCustom;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import org.bukkit.GameEvent;
Expand All @@ -25,26 +24,34 @@
import org.bukkit.craftbukkit.block.CraftBlockType;
import org.bukkit.craftbukkit.damage.CraftDamageType;
import org.bukkit.craftbukkit.enchantments.CraftEnchantment;
import org.bukkit.craftbukkit.entity.CraftCat;
import org.bukkit.craftbukkit.entity.CraftFrog;
import org.bukkit.craftbukkit.entity.CraftVillager;
import org.bukkit.craftbukkit.entity.CraftWolf;
import org.bukkit.craftbukkit.generator.structure.CraftStructure;
import org.bukkit.craftbukkit.generator.structure.CraftStructureType;
import org.bukkit.craftbukkit.inventory.CraftItemType;
import org.bukkit.craftbukkit.inventory.trim.CraftTrimMaterial;
import org.bukkit.craftbukkit.inventory.trim.CraftTrimPattern;
import org.bukkit.craftbukkit.legacy.FieldRename;
import org.bukkit.craftbukkit.map.CraftMapCursor;
import org.bukkit.craftbukkit.potion.CraftPotionEffectType;
import org.bukkit.craftbukkit.util.ApiVersion;
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
import org.bukkit.craftbukkit.util.Handleable;
import org.bukkit.damage.DamageType;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Cat;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Frog;
import org.bukkit.entity.Villager;
import org.bukkit.entity.Wolf;
import org.bukkit.generator.structure.Structure;
import org.bukkit.generator.structure.StructureType;
import org.bukkit.inventory.ItemType;
import org.bukkit.inventory.meta.trim.TrimMaterial;
import org.bukkit.inventory.meta.trim.TrimPattern;
import org.bukkit.map.MapCursor;
import org.bukkit.potion.PotionEffectType;
import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -136,7 +143,13 @@ public static <B extends Keyed> Registry<?> createRegistry(Class<? super B> bukk
return new CraftRegistry<>(Structure.class, registryHolder.registryOrThrow(Registries.STRUCTURE), CraftStructure::new, FieldRename.NONE);
}
if (bukkitClass == StructureType.class) {
return new CraftRegistry<>(StructureType.class, BuiltInRegistries.STRUCTURE_TYPE, CraftStructureType::new, FieldRename.NONE);
return new CraftRegistry<>(StructureType.class, registryHolder.registryOrThrow(Registries.STRUCTURE_TYPE), CraftStructureType::new, FieldRename.NONE);
}
if (bukkitClass == Villager.Type.class) {
return new CraftRegistry<>(Villager.Type.class, registryHolder.registryOrThrow(Registries.VILLAGER_TYPE), CraftVillager.CraftType::new, FieldRename.NONE);
}
if (bukkitClass == Villager.Profession.class) {
return new CraftRegistry<>(Villager.Profession.class, registryHolder.registryOrThrow(Registries.VILLAGER_PROFESSION), CraftVillager.CraftProfession::new, FieldRename.NONE);
}
if (bukkitClass == TrimMaterial.class) {
return new CraftRegistry<>(TrimMaterial.class, registryHolder.registryOrThrow(Registries.TRIM_MATERIAL), CraftTrimMaterial::new, FieldRename.NONE);
Expand All @@ -159,6 +172,15 @@ public static <B extends Keyed> Registry<?> createRegistry(Class<? super B> bukk
if (bukkitClass == ItemType.class) {
return new CraftRegistry<>(ItemType.class, registryHolder.registryOrThrow(Registries.ITEM), CraftItemType::new, FieldRename.NONE);
}
if (bukkitClass == Frog.Variant.class) {
return new CraftRegistry<>(Frog.Variant.class, registryHolder.registryOrThrow(Registries.FROG_VARIANT), CraftFrog.CraftVariant::new, FieldRename.NONE);
}
if (bukkitClass == Cat.Type.class) {
return new CraftRegistry<>(Cat.Type.class, registryHolder.registryOrThrow(Registries.CAT_VARIANT), CraftCat.CraftType::new, FieldRename.NONE);
}
if (bukkitClass == MapCursor.Type.class) {
return new CraftRegistry<>(MapCursor.Type.class, registryHolder.registryOrThrow(Registries.MAP_DECORATION_TYPE), CraftMapCursor.CraftType::new, FieldRename.NONE);
}

return null;
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/org/bukkit/craftbukkit/CraftServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,14 @@ private void loadCompatibilities() {
logger.info("Using following compatibilities: `" + Joiner.on("`, `").join(activeCompatibilities) + "`, this will affect performance and other plugins behavior.");
logger.info("Only use when necessary and prefer updating plugins if possible.");
}

if (activeCompatibilities.contains("enum-compatibility-mode")) {
getLogger().warning("Loading plugins in enum compatibility mode. This will affect plugin performance. Use only as a transition period or when absolutely necessary.");
} else if (System.getProperty("RemoveEnumBanner") == null) {
// TODO 2024-06-16: Remove in newer version
getLogger().info("*** This version of Spigot contains changes to some enums. If you notice that plugins no longer work after updating, please report this to the developers of those plugins first. ***");
getLogger().info("*** If you cannot update those plugins, you can try setting `settings.compatibility.enum-compatibility-mode` to `true` in `bukkit.yml`. ***");
}
}

public void loadPlugins() {
Expand Down
90 changes: 72 additions & 18 deletions src/main/java/org/bukkit/craftbukkit/entity/CraftCat.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package org.bukkit.craftbukkit.entity;

import com.google.common.base.Preconditions;
import java.util.Locale;
import net.minecraft.core.Holder;
import net.minecraft.core.IRegistry;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.entity.animal.CatVariant;
import net.minecraft.world.entity.animal.EntityCat;
import net.minecraft.world.item.EnumColor;
import org.bukkit.DyeColor;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.craftbukkit.CraftRegistry;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
import org.bukkit.craftbukkit.util.Handleable;
import org.bukkit.entity.Cat;

public class CraftCat extends CraftTameableAnimal implements Cat {
Expand Down Expand Up @@ -52,39 +53,92 @@ public void setCollarColor(DyeColor color) {
getHandle().setCollarColor(EnumColor.byId(color.getWoolData()));
}

public static class CraftType {
public static class CraftType implements Type, Handleable<CatVariant> {
private static int count = 0;

public static Type minecraftToBukkit(CatVariant minecraft) {
Preconditions.checkArgument(minecraft != null);

IRegistry<CatVariant> registry = CraftRegistry.getMinecraftRegistry(Registries.CAT_VARIANT);

return Registry.CAT_VARIANT.get(CraftNamespacedKey.fromMinecraft(registry.getKey(minecraft)));
return CraftRegistry.minecraftToBukkit(minecraft, Registries.CAT_VARIANT, Registry.CAT_VARIANT);
}

public static Type minecraftHolderToBukkit(Holder<CatVariant> minecraft) {
return minecraftToBukkit(minecraft.value());
}

public static CatVariant bukkitToMinecraft(Type bukkit) {
Preconditions.checkArgument(bukkit != null);
return CraftRegistry.bukkitToMinecraft(bukkit);
}

IRegistry<CatVariant> registry = CraftRegistry.getMinecraftRegistry(Registries.CAT_VARIANT);
public static Holder<CatVariant> bukkitToMinecraftHolder(Type bukkit) {
return CraftRegistry.bukkitToMinecraftHolder(bukkit, Registries.CAT_VARIANT);
}

return registry.get(CraftNamespacedKey.toMinecraft(bukkit.getKey()));
private final NamespacedKey key;
private final CatVariant catVariant;
private final String name;
private final int ordinal;

public CraftType(NamespacedKey key, CatVariant catVariant) {
this.key = key;
this.catVariant = catVariant;
// For backwards compatibility, minecraft values will still return the uppercase name without the namespace,
// in case plugins use for example the name as key in a config file to receive type specific values.
// Custom types will return the key with namespace. For a plugin this should look than like a new type
// (which can always be added in new minecraft versions and the plugin should therefore handle it accordingly).
if (NamespacedKey.MINECRAFT.equals(key.getNamespace())) {
this.name = key.getKey().toUpperCase(Locale.ROOT);
} else {
this.name = key.toString();
}
this.ordinal = count++;
}

public static Holder<CatVariant> bukkitToMinecraftHolder(Type bukkit) {
Preconditions.checkArgument(bukkit != null);
@Override
public CatVariant getHandle() {
return catVariant;
}

IRegistry<CatVariant> registry = CraftRegistry.getMinecraftRegistry(Registries.CAT_VARIANT);
@Override
public NamespacedKey getKey() {
return key;
}

@Override
public int compareTo(Type variant) {
return ordinal - variant.ordinal();
}

if (registry.wrapAsHolder(bukkitToMinecraft(bukkit)) instanceof Holder.c<CatVariant> holder) {
return holder;
@Override
public String name() {
return name;
}

@Override
public int ordinal() {
return ordinal;
}

@Override
public String toString() {
// For backwards compatibility
return name();
}

@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}

if (!(other instanceof CraftType)) {
return false;
}

throw new IllegalArgumentException("No Reference holder found for " + bukkit
+ ", this can happen if a plugin creates its own cat variant with out properly registering it.");
return getKey().equals(((CraftType) other).getKey());
}

@Override
public int hashCode() {
return getKey().hashCode();
}
}
}
94 changes: 73 additions & 21 deletions src/main/java/org/bukkit/craftbukkit/entity/CraftFrog.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package org.bukkit.craftbukkit.entity;

import com.google.common.base.Preconditions;
import java.util.Locale;
import net.minecraft.core.Holder;
import net.minecraft.core.IRegistry;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.entity.animal.FrogVariant;
import net.minecraft.world.entity.animal.frog.Frog;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.craftbukkit.CraftRegistry;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
import org.bukkit.craftbukkit.util.Handleable;
import org.bukkit.entity.Entity;

public class CraftFrog extends CraftAnimals implements org.bukkit.entity.Frog {
Expand Down Expand Up @@ -54,41 +55,92 @@ public void setVariant(Variant variant) {
getHandle().setVariant(CraftVariant.bukkitToMinecraftHolder(variant));
}

public static class CraftVariant {
public static class CraftVariant implements Variant, Handleable<FrogVariant> {
private static int count = 0;

public static Variant minecraftToBukkit(FrogVariant minecraft) {
Preconditions.checkArgument(minecraft != null);

IRegistry<FrogVariant> registry = CraftRegistry.getMinecraftRegistry(Registries.FROG_VARIANT);
Variant bukkit = Registry.FROG_VARIANT.get(CraftNamespacedKey.fromMinecraft(registry.getResourceKey(minecraft).orElseThrow().location()));

Preconditions.checkArgument(bukkit != null);

return bukkit;
return CraftRegistry.minecraftToBukkit(minecraft, Registries.FROG_VARIANT, Registry.FROG_VARIANT);
}

public static Variant minecraftHolderToBukkit(Holder<FrogVariant> minecraft) {
return minecraftToBukkit(minecraft.value());
}

public static FrogVariant bukkitToMinecraft(Variant bukkit) {
Preconditions.checkArgument(bukkit != null);

return CraftRegistry.getMinecraftRegistry(Registries.FROG_VARIANT)
.getOptional(CraftNamespacedKey.toMinecraft(bukkit.getKey())).orElseThrow();
return CraftRegistry.bukkitToMinecraft(bukkit);
}

public static Holder<FrogVariant> bukkitToMinecraftHolder(Variant bukkit) {
Preconditions.checkArgument(bukkit != null);
return CraftRegistry.bukkitToMinecraftHolder(bukkit, Registries.FROG_VARIANT);
}

private final NamespacedKey key;
private final FrogVariant frogVariant;
private final String name;
private final int ordinal;

public CraftVariant(NamespacedKey key, FrogVariant frogVariant) {
this.key = key;
this.frogVariant = frogVariant;
// For backwards compatibility, minecraft values will still return the uppercase name without the namespace,
// in case plugins use for example the name as key in a config file to receive variant specific values.
// Custom variants will return the key with namespace. For a plugin this should look than like a new variant
// (which can always be added in new minecraft versions and the plugin should therefore handle it accordingly).
if (NamespacedKey.MINECRAFT.equals(key.getNamespace())) {
this.name = key.getKey().toUpperCase(Locale.ROOT);
} else {
this.name = key.toString();
}
this.ordinal = count++;
}

@Override
public FrogVariant getHandle() {
return frogVariant;
}

IRegistry<FrogVariant> registry = CraftRegistry.getMinecraftRegistry(Registries.FROG_VARIANT);
@Override
public NamespacedKey getKey() {
return key;
}

if (registry.wrapAsHolder(bukkitToMinecraft(bukkit)) instanceof Holder.c<FrogVariant> holder) {
return holder;
@Override
public int compareTo(Variant variant) {
return ordinal - variant.ordinal();
}

@Override
public String name() {
return name;
}

@Override
public int ordinal() {
return ordinal;
}

@Override
public String toString() {
// For backwards compatibility
return name();
}

@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}

if (!(other instanceof CraftVariant)) {
return false;
}

throw new IllegalArgumentException("No Reference holder found for " + bukkit
+ ", this can happen if a plugin creates its own frog variant with out properly registering it.");
return getKey().equals(((Variant) other).getKey());
}

@Override
public int hashCode() {
return getKey().hashCode();
}
}
}
Loading

0 comments on commit 082aa51

Please sign in to comment.