Skip to content

Commit

Permalink
SPIGOT-7837: Support data pack banner patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
Doc94 authored and md-5 committed Jul 27, 2024
1 parent 9c3bd43 commit fccf524
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 17 deletions.
5 changes: 5 additions & 0 deletions src/main/java/org/bukkit/craftbukkit/CraftRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
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;
Expand Down Expand Up @@ -181,6 +183,9 @@ public static <B extends Keyed> Registry<?> createRegistry(Class<? super B> bukk
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;
}
Expand Down
141 changes: 127 additions & 14 deletions src/main/java/org/bukkit/craftbukkit/block/banner/CraftPatternType.java
Original file line number Diff line number Diff line change
@@ -1,37 +1,31 @@
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> {

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);
private static int count = 0;

return bukkit;
public static PatternType minecraftToBukkit(EnumBannerPatternType minecraft) {
return CraftRegistry.minecraftToBukkit(minecraft, Registries.BANNER_PATTERN, Registry.BANNER_PATTERN);
}

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

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) {
Expand All @@ -46,4 +40,123 @@ public static Holder<EnumBannerPatternType> bukkitToMinecraftHolder(PatternType
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();
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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;
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/bukkit/craftbukkit/util/Commodore.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/bukkit/block/banner/PatternTypeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void testToBukkit() {
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);
}
}

Expand All @@ -35,7 +35,7 @@ public void testToNMS() {
}
}

assertNotNull(found, "No NMS banner for " + bukkit + " " + bukkit.getKey());
assertNotNull(found, "No NMS banner pattern for " + bukkit + " " + bukkit.getKey());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@
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;
Expand Down Expand Up @@ -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);

}

Expand Down

0 comments on commit fccf524

Please sign in to comment.