Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/move entities list #705

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,12 @@
import net.countercraft.movecraft.craft.type.transform.StringTransform;
import net.countercraft.movecraft.craft.type.transform.Transform;
import net.countercraft.movecraft.processing.MovecraftWorld;
import net.countercraft.movecraft.util.EntityUtil;
import net.countercraft.movecraft.util.Pair;
import net.countercraft.movecraft.util.Tags;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.sound.Sound;
import org.bukkit.*;
import org.bukkit.entity.Animals;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Monster;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -60,7 +57,6 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.function.BiFunction;
Expand Down Expand Up @@ -509,31 +505,7 @@ public static void registerTypeValidator(Predicate<CraftType> validator, String
var entityStringList = data.getStringList(fileKey);
EnumSet<EntityType> entityList = EnumSet.noneOf(EntityType.class);
for (String entityString : entityStringList) {
String upper = entityString.toUpperCase(Locale.ROOT);

// User wants a premade list
if(upper.startsWith("@")) {

switch (upper.substring(1)) {
case "monsters" -> {
entityList.addAll(EntityUtil.getClassEntities(Monster.class));
continue;
}

case "animals" -> {
entityList.addAll(EntityUtil.getClassEntities(Animals.class));
continue;
}

default -> throw new TypeData.InvalidValueException("Value for " + fileKey + " is not a valid EntityType list");
}
}

try {
EntityType.valueOf(upper);
} catch (Exception e) {
throw new TypeData.InvalidValueException("Value for " + fileKey + " must be an EntityType");
}
entityList.addAll(Tags.parseEntities(entityString));
}

return entityList;
Expand Down
25 changes: 0 additions & 25 deletions api/src/main/java/net/countercraft/movecraft/util/EntityUtil.java

This file was deleted.

37 changes: 37 additions & 0 deletions api/src/main/java/net/countercraft/movecraft/util/Tags.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.Tag;
import org.bukkit.entity.EntityType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -135,4 +136,40 @@ public static EnumSet<Material> parseMaterials(@NotNull String materialName) {
}
return returnSet;
}

@Nullable
public static EnumSet<EntityType> parseEntityRegistry(@NotNull String string) {
if (!string.startsWith("#"))
return null;

String nameKey = string.substring(1);
var key = keyFromString(nameKey);
if (key == null)
throw new IllegalArgumentException("Entry " + string + " is not a valid namespace key!");

var tag = Bukkit.getTag(Tag.REGISTRY_ENTITY_TYPES, key, EntityType.class);
if (tag == null)
throw new IllegalArgumentException("Entry " + string + " is not a valid tag!");

var tags = tag.getValues();
return tags.isEmpty() ? EnumSet.noneOf(EntityType.class) : EnumSet.copyOf(tags);
}

/**
* Searches for a tag which matches the provided entityName. Failing that, it attempts to load a matching singular EntityType directly.
*
* @param entityName EntityType name or tag
* @return the set of EntityType the tag/EntityType resolves to
*/
@NotNull
public static EnumSet<EntityType> parseEntities(@NotNull String entityName) {
EnumSet<EntityType> returnSet = EnumSet.noneOf(EntityType.class);
EnumSet<EntityType> tagged = parseEntityRegistry(entityName);
if (tagged != null) {
returnSet.addAll(tagged);
} else {
returnSet.add(EntityType.valueOf(entityName.toUpperCase()));
}
return returnSet;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"values": ["elder_guardian", "wither_skeleton", "stray", "husk", "zombie_villager", "evoker", "vex", "vindicator", "illusioner", "creeper", "skeleton", "spider", "giant", "zombie", "zombified_piglin", "enderman", "cave_spider", "silverfish", "blaze", "wither", "witch", "endermite", "guardian", "drowned", "pillager", "ravager", "piglin", "zoglin", "piglin_brute", "warden", "breeze", "bogged"]
}