SPIGOT-7837: Support data pack banner patterns

By: Doc <nachito94@msn.com>
This commit is contained in:
CraftBukkit/Spigot 2024-07-27 10:14:41 +10:00
parent bfad1aa117
commit d2745c6491
6 changed files with 142 additions and 17 deletions

View File

@ -21,7 +21,9 @@ import org.bukkit.Registry;
import org.bukkit.attribute.Attribute;
import org.bukkit.block.Biome;
import org.bukkit.block.BlockType;
import org.bukkit.block.banner.PatternType;
import org.bukkit.craftbukkit.block.CraftBlockType;
import org.bukkit.craftbukkit.block.banner.CraftPatternType;
import org.bukkit.craftbukkit.damage.CraftDamageType;
import org.bukkit.craftbukkit.enchantments.CraftEnchantment;
import org.bukkit.craftbukkit.entity.CraftCat;
@ -181,6 +183,9 @@ public class CraftRegistry<B extends Keyed, M> implements Registry<B> {
if (bukkitClass == MapCursor.Type.class) {
return new CraftRegistry<>(MapCursor.Type.class, registryHolder.registryOrThrow(Registries.MAP_DECORATION_TYPE), CraftMapCursor.CraftType::new, FieldRename.NONE);
}
if (bukkitClass == PatternType.class) {
return new CraftRegistry<>(PatternType.class, registryHolder.registryOrThrow(Registries.BANNER_PATTERN), CraftPatternType::new, FieldRename.NONE);
}
return null;
}

View File

@ -1,26 +1,23 @@
package org.bukkit.craftbukkit.block.banner;
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.level.block.entity.EnumBannerPatternType;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.block.banner.PatternType;
import org.bukkit.craftbukkit.CraftRegistry;
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
import org.bukkit.craftbukkit.util.Handleable;
public class CraftPatternType {
public class CraftPatternType implements PatternType, Handleable<EnumBannerPatternType> {
private static int count = 0;
public static PatternType minecraftToBukkit(EnumBannerPatternType minecraft) {
Preconditions.checkArgument(minecraft != null);
IRegistry<EnumBannerPatternType> registry = CraftRegistry.getMinecraftRegistry(Registries.BANNER_PATTERN);
PatternType bukkit = Registry.BANNER_PATTERN.get(CraftNamespacedKey.fromMinecraft(registry.getResourceKey(minecraft).orElseThrow().location()));
Preconditions.checkArgument(bukkit != null);
return bukkit;
return CraftRegistry.minecraftToBukkit(minecraft, Registries.BANNER_PATTERN, Registry.BANNER_PATTERN);
}
public static PatternType minecraftHolderToBukkit(Holder<EnumBannerPatternType> minecraft) {
@ -28,10 +25,7 @@ public class CraftPatternType {
}
public static EnumBannerPatternType bukkitToMinecraft(PatternType bukkit) {
Preconditions.checkArgument(bukkit != null);
return CraftRegistry.getMinecraftRegistry(Registries.BANNER_PATTERN)
.getOptional(CraftNamespacedKey.toMinecraft(bukkit.getKey())).orElseThrow();
return CraftRegistry.bukkitToMinecraft(bukkit);
}
public static Holder<EnumBannerPatternType> bukkitToMinecraftHolder(PatternType bukkit) {
@ -46,4 +40,123 @@ public class CraftPatternType {
throw new IllegalArgumentException("No Reference holder found for " + bukkit
+ ", this can happen if a plugin creates its own banner pattern without properly registering it.");
}
private final NamespacedKey key;
private final EnumBannerPatternType bannerPatternType;
private final String name;
private final int ordinal;
public CraftPatternType(NamespacedKey key, EnumBannerPatternType bannerPatternType) {
this.key = key;
this.bannerPatternType = bannerPatternType;
// For backwards compatibility, minecraft values will stile return the uppercase name without the namespace,
// in case plugins use for example the name as key in a config file to receive pattern type specific values.
// Custom pattern types will return the key with namespace. For a plugin this should look than like a new pattern 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++;
}
@Override
public EnumBannerPatternType getHandle() {
return bannerPatternType;
}
@Override
public NamespacedKey getKey() {
return key;
}
@Override
public int compareTo(PatternType patternType) {
return ordinal - patternType.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 CraftPatternType)) {
return false;
}
return getKey().equals(((PatternType) other).getKey());
}
@Override
public int hashCode() {
return getKey().hashCode();
}
@Override
public String getIdentifier() {
return switch (this.name()) {
case "BASE" -> "b";
case "SQUARE_BOTTOM_LEFT" -> "bl";
case "SQUARE_BOTTOM_RIGHT" -> "br";
case "SQUARE_TOP_LEFT" -> "tl";
case "SQUARE_TOP_RIGHT" -> "tr";
case "STRIPE_BOTTOM" -> "bs";
case "STRIPE_TOP" -> "ts";
case "STRIPE_LEFT" -> "ls";
case "STRIPE_RIGHT" -> "rs";
case "STRIPE_CENTER" -> "cs";
case "STRIPE_MIDDLE" -> "ms";
case "STRIPE_DOWNRIGHT" -> "drs";
case "STRIPE_DOWNLEFT" -> "dls";
case "SMALL_STRIPES" -> "ss";
case "CROSS" -> "cr";
case "STRAIGHT_CROSS" -> "sc";
case "TRIANGLE_BOTTOM" -> "bt";
case "TRIANGLE_TOP" -> "tt";
case "TRIANGLES_BOTTOM" -> "bts";
case "TRIANGLES_TOP" -> "tts";
case "DIAGONAL_LEFT" -> "ld";
case "DIAGONAL_UP_RIGHT" -> "rd";
case "DIAGONAL_UP_LEFT" -> "lud";
case "DIAGONAL_RIGHT" -> "rud";
case "CIRCLE" -> "mc";
case "RHOMBUS" -> "mr";
case "HALF_VERTICAL" -> "vh";
case "HALF_HORIZONTAL" -> "hh";
case "HALF_VERTICAL_RIGHT" -> "vhr";
case "HALF_HORIZONTAL_BOTTOM" -> "hhb";
case "BORDER" -> "bo";
case "CURLY_BORDER" -> "cbo";
case "CREEPER" -> "cre";
case "GRADIENT" -> "gra";
case "GRADIENT_UP" -> "gru";
case "BRICKS" -> "bri";
case "SKULL" -> "sku";
case "FLOWER" -> "flo";
case "MOJANG" -> "moj";
case "GLOBE" -> "glb";
case "PIGLIN" -> "pig";
case "FLOW" -> "flw";
case "GUSTER" -> "gus";
default -> this.getKey().toString();
};
}
}

View File

@ -17,6 +17,7 @@ import java.util.stream.Collector;
import java.util.stream.Collectors;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.block.banner.PatternType;
import org.bukkit.craftbukkit.legacy.FieldRename;
import org.bukkit.craftbukkit.legacy.reroute.DoNotReroute;
import org.bukkit.craftbukkit.legacy.reroute.InjectPluginVersion;
@ -48,6 +49,7 @@ public class EnumEvil {
REGISTRIES.put(Frog.Variant.class, new LegacyRegistryData(Registry.FROG_VARIANT, Frog.Variant::valueOf));
REGISTRIES.put(Cat.Type.class, new LegacyRegistryData(Registry.CAT_VARIANT, Cat.Type::valueOf));
REGISTRIES.put(MapCursor.Type.class, new LegacyRegistryData(Registry.MAP_DECORATION_TYPE, MapCursor.Type::valueOf));
REGISTRIES.put(PatternType.class, new LegacyRegistryData(Registry.BANNER_PATTERN, PatternType::valueOf));
}
public static LegacyRegistryData getRegistryData(Class<?> clazz) {

View File

@ -83,7 +83,8 @@ public class Commodore {
"org/bukkit/entity/Villager$Profession", "NOP",
"org/bukkit/entity/Frog$Variant", "NOP",
"org/bukkit/entity/Cat$Type", "NOP",
"org/bukkit/map/MapCursor$Type", "NOP"
"org/bukkit/map/MapCursor$Type", "NOP",
"org/bukkit/block/banner/PatternType", "NOP"
);
private static Map<String, RerouteMethodData> createReroutes(Class<?> clazz) {

View File

@ -18,7 +18,7 @@ public class PatternTypeTest extends AbstractTestingBase {
for (EnumBannerPatternType nms : MinecraftServer.getDefaultRegistryAccess().registryOrThrow(Registries.BANNER_PATTERN)) {
PatternType bukkit = Registry.BANNER_PATTERN.get(CraftNamespacedKey.fromMinecraft(nms.assetId()));
assertNotNull(bukkit, "No Bukkit banner for " + nms + " " + nms.toString());
assertNotNull(bukkit, "No Bukkit banner pattern for " + nms + " " + nms);
}
}
@ -35,7 +35,7 @@ public class PatternTypeTest extends AbstractTestingBase {
}
}
assertNotNull(found, "No NMS banner for " + bukkit + " " + bukkit.getKey());
assertNotNull(found, "No NMS banner pattern for " + bukkit + " " + bukkit.getKey());
}
}
}

View File

@ -12,15 +12,18 @@ import net.minecraft.world.entity.animal.WolfVariant;
import net.minecraft.world.entity.npc.VillagerProfession;
import net.minecraft.world.entity.npc.VillagerType;
import net.minecraft.world.item.Instrument;
import net.minecraft.world.level.block.entity.EnumBannerPatternType;
import net.minecraft.world.level.saveddata.maps.MapDecorationType;
import org.bukkit.GameEvent;
import org.bukkit.JukeboxSong;
import org.bukkit.MusicInstrument;
import org.bukkit.block.BlockType;
import org.bukkit.block.banner.PatternType;
import org.bukkit.craftbukkit.CraftGameEvent;
import org.bukkit.craftbukkit.CraftJukeboxSong;
import org.bukkit.craftbukkit.CraftMusicInstrument;
import org.bukkit.craftbukkit.block.CraftBlockType;
import org.bukkit.craftbukkit.block.banner.CraftPatternType;
import org.bukkit.craftbukkit.damage.CraftDamageType;
import org.bukkit.craftbukkit.enchantments.CraftEnchantment;
import org.bukkit.craftbukkit.entity.CraftCat;
@ -75,6 +78,7 @@ public class RegistriesArgumentProvider implements ArgumentsProvider {
register(Frog.Variant.class, Registries.FROG_VARIANT, CraftFrog.CraftVariant.class, FrogVariant.class);
register(Cat.Type.class, Registries.CAT_VARIANT, CraftCat.CraftType.class, CatVariant.class);
register(MapCursor.Type.class, Registries.MAP_DECORATION_TYPE, CraftMapCursor.CraftType.class, MapDecorationType.class);
register(PatternType.class, Registries.BANNER_PATTERN, CraftPatternType.class, EnumBannerPatternType.class);
}