Skip to content

Commit

Permalink
Support wolf variants + armadillos
Browse files Browse the repository at this point in the history
  • Loading branch information
JRoy committed May 2, 2024
1 parent 0decadb commit b9c5a53
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
1 change: 1 addition & 0 deletions Essentials/src/main/java/com/earth2me/essentials/Mob.java
Expand Up @@ -112,6 +112,7 @@ public enum Mob {
CHEST_BOAT("ChestBoat", Enemies.NEUTRAL, "CHEST_BOAT"),
CAMEL("Camel", Enemies.FRIENDLY, "CAMEL"),
SNIFFER("Sniffer", Enemies.FRIENDLY, "SNIFFER"),
ARMADILLO("Armadillo", Enemies.FRIENDLY, "ARMADILLO"),
;

private static final Map<String, Mob> hashMap = new HashMap<>();
Expand Down
14 changes: 14 additions & 0 deletions Essentials/src/main/java/com/earth2me/essentials/MobCompat.java
@@ -1,6 +1,7 @@
package com.earth2me.essentials;

import com.earth2me.essentials.utils.EnumUtil;
import com.earth2me.essentials.utils.RegistryUtil;
import com.earth2me.essentials.utils.VersionUtil;
import net.ess3.nms.refl.ReflUtil;
import org.bukkit.Material;
Expand All @@ -20,6 +21,7 @@
import org.bukkit.entity.Player;
import org.bukkit.entity.TropicalFish;
import org.bukkit.entity.Villager;
import org.bukkit.entity.Wolf;
import org.bukkit.inventory.ItemStack;

import java.lang.reflect.Method;
Expand Down Expand Up @@ -222,6 +224,18 @@ public static void setCamelSaddle(final Entity entity, final Player target) {
}
}

public static void setWolfVariant(final Entity entity, final String variant) {
if (VersionUtil.getServerBukkitVersion().isLowerThan(VersionUtil.v1_20_6_R01)) {
return;
}

if (entity instanceof Wolf) {
final Wolf wolf = (Wolf) entity;
//noinspection DataFlowIssue
wolf.setVariant(RegistryUtil.valueOf(Wolf.Variant.class, variant));
}
}

public enum CatType {
// These are (loosely) Mojang names for the cats
SIAMESE("SIAMESE", "SIAMESE_CAT"),
Expand Down
12 changes: 12 additions & 0 deletions Essentials/src/main/java/com/earth2me/essentials/MobData.java
Expand Up @@ -209,6 +209,15 @@ public enum MobData {
OAK_BOAT("oak", Boat.class, MobCompat.BoatVariant.OAK, true),
SPRUCE_BOAT("spruce", Boat.class, MobCompat.BoatVariant.SPRUCE, true),
SADDLE_CAMEL("saddle", MobCompat.CAMEL, Data.CAMELSADDLE, true),
PALE_WOLF("pale", EntityType.WOLF, "wolf:PALE", true),
SPOTTED_WOLF("spotted", EntityType.WOLF, "wolf:PALE", true),
SNOWY_WOLF("snowy", EntityType.WOLF, "wolf:PALE", true),
BLACK_WOLF("black", EntityType.WOLF, "wolf:BLACK", true),
ASHEN_WOLF("ashen", EntityType.WOLF, "wolf:ASHEN", true),
RUSTY_WOLF("rusty", EntityType.WOLF, "wolf:RUSTY", true),
WOODS_WOLF("woods", EntityType.WOLF, "wolf:WOODS", true),
CHESTNUT_WOLF("chestnut", EntityType.WOLF, "wolf:CHESTNUT", true),
STRIPED_WOLF("striped", EntityType.WOLF, "wolf:STRIPED", true),
;

final private String nickname;
Expand Down Expand Up @@ -424,6 +433,9 @@ public void setData(final Entity spawned, final Player target, final String rawD
case "frog":
MobCompat.setFrogVariant(spawned, split[1]);
break;
case "wolf":
MobCompat.setWolfVariant(spawned, split[1]);
break;
}
} else {
Essentials.getWrappedLogger().warning("Unknown mob data type: " + this.toString());
Expand Down
@@ -1,15 +1,27 @@
package com.earth2me.essentials.utils;

import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;

public final class RegistryUtil {
private static final Table<Class<?>, String, Object> registryCache = HashBasedTable.create();

private RegistryUtil() {
}

public static <T> T valueOf(Class<T> registry, String... names) {
for (String name : names) {
for (final String name : names) {
//noinspection unchecked
T value = (T) registryCache.get(registry, name);
if (value != null) {
return value;
}

try {
final T value = (T) registry.getDeclaredField(name).get(null);
//noinspection unchecked
value = (T) registry.getDeclaredField(name).get(null);
if (value != null) {
registryCache.put(registry, name, value);
return value;
}
} catch (NoSuchFieldException | IllegalAccessException ignored) {
Expand Down

0 comments on commit b9c5a53

Please sign in to comment.