Various minor

Add entity registry
Re-add AbstractLoggingExtent
Fill in missing nbt on entity load
This commit is contained in:
Jesse Boyd
2018-08-14 19:24:10 +10:00
parent 55d34343dd
commit 131fd39f63
13 changed files with 356 additions and 171 deletions

View File

@ -21,46 +21,15 @@ package com.sk89q.worldedit.world.entity;
import com.sk89q.worldedit.registry.NamespacedRegistry;
public class EntityType {
public static final NamespacedRegistry<EntityType> REGISTRY = new NamespacedRegistry<>("entity type");
private String id;
public EntityType(String id) {
// If it has no namespace, assume minecraft.
if (!id.contains(":")) {
id = "minecraft:" + id;
}
this.id = id;
}
public String getId() {
return this.id;
}
public interface EntityType {
String getId();
/**
* Gets the name of this item, or the ID if the name cannot be found.
*
* @return The name, or ID
*/
public String getName() {
default String getName() {
return getId();
}
@Override
public String toString() {
return getId();
}
@Override
public int hashCode() {
return this.id.hashCode();
}
@Override
public boolean equals(Object obj) {
return obj instanceof EntityType && this.id.equals(((EntityType) obj).id);
}
}

View File

@ -19,109 +19,156 @@
package com.sk89q.worldedit.world.entity;
import com.boydti.fawe.util.ReflectionUtils;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.blocks.BaseItem;
import com.sk89q.worldedit.blocks.BaseItemStack;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.extension.platform.Capability;
import com.sk89q.worldedit.world.block.BlockTypes;
import com.sk89q.worldedit.world.item.ItemType;
import com.sk89q.worldedit.world.registry.LegacyMapper;
import javax.annotation.Nullable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
public class EntityTypes {
public enum EntityTypes implements EntityType {
/*
-----------------------------------------------------
Replaced at runtime by the entity registry
-----------------------------------------------------
*/
AREA_EFFECT_CLOUD,
ARMOR_STAND,
ARROW,
BAT,
BLAZE,
BOAT,
CAVE_SPIDER,
CHEST_MINECART,
CHICKEN,
COD,
COMMAND_BLOCK_MINECART,
COW,
CREEPER,
DOLPHIN,
DONKEY,
DRAGON_FIREBALL,
DROWNED,
EGG,
ELDER_GUARDIAN,
END_CRYSTAL,
ENDER_DRAGON,
ENDER_PEARL,
ENDERMAN,
ENDERMITE,
EVOKER,
EVOKER_FANGS,
EXPERIENCE_BOTTLE,
EXPERIENCE_ORB,
EYE_OF_ENDER,
FALLING_BLOCK,
FIREBALL,
FIREWORK_ROCKET,
FISHING_BOBBER,
FURNACE_MINECART,
GHAST,
GIANT,
GUARDIAN,
HOPPER_MINECART,
HORSE,
HUSK,
ILLUSIONER,
IRON_GOLEM,
ITEM,
ITEM_FRAME,
LEASH_KNOT,
LIGHTNING_BOLT,
LLAMA,
LLAMA_SPIT,
MAGMA_CUBE,
MINECART,
MOOSHROOM,
MULE,
OCELOT,
PAINTING,
PARROT,
PHANTOM,
PIG,
PLAYER,
POLAR_BEAR,
POTION,
PUFFERFISH,
RABBIT,
SALMON,
SHEEP,
SHULKER,
SHULKER_BULLET,
SILVERFISH,
SKELETON,
SKELETON_HORSE,
SLIME,
SMALL_FIREBALL,
SNOW_GOLEM,
SNOWBALL,
SPAWNER_MINECART,
SPECTRAL_ARROW,
SPIDER,
SQUID,
STRAY,
TNT,
TNT_MINECART,
TRIDENT,
TROPICAL_FISH,
TURTLE,
VEX,
VILLAGER,
VINDICATOR,
WITCH,
WITHER,
WITHER_SKELETON,
WITHER_SKULL,
WOLF,
ZOMBIE,
ZOMBIE_HORSE,
ZOMBIE_PIGMAN,
ZOMBIE_VILLAGER,
public static final EntityType AREA_EFFECT_CLOUD = register("minecraft:area_effect_cloud");
public static final EntityType ARMOR_STAND = register("minecraft:armor_stand");
public static final EntityType ARROW = register("minecraft:arrow");
public static final EntityType BAT = register("minecraft:bat");
public static final EntityType BLAZE = register("minecraft:blaze");
public static final EntityType BOAT = register("minecraft:boat");
public static final EntityType CAVE_SPIDER = register("minecraft:cave_spider");
public static final EntityType CHEST_MINECART = register("minecraft:chest_minecart");
public static final EntityType CHICKEN = register("minecraft:chicken");
public static final EntityType COD = register("minecraft:cod");
public static final EntityType COMMAND_BLOCK_MINECART = register("minecraft:command_block_minecart");
public static final EntityType COW = register("minecraft:cow");
public static final EntityType CREEPER = register("minecraft:creeper");
public static final EntityType DOLPHIN = register("minecraft:dolphin");
public static final EntityType DONKEY = register("minecraft:donkey");
public static final EntityType DRAGON_FIREBALL = register("minecraft:dragon_fireball");
public static final EntityType DROWNED = register("minecraft:drowned");
public static final EntityType EGG = register("minecraft:egg");
public static final EntityType ELDER_GUARDIAN = register("minecraft:elder_guardian");
public static final EntityType END_CRYSTAL = register("minecraft:end_crystal");
public static final EntityType ENDER_DRAGON = register("minecraft:ender_dragon");
public static final EntityType ENDER_PEARL = register("minecraft:ender_pearl");
public static final EntityType ENDERMAN = register("minecraft:enderman");
public static final EntityType ENDERMITE = register("minecraft:endermite");
public static final EntityType EVOKER = register("minecraft:evoker");
public static final EntityType EVOKER_FANGS = register("minecraft:evoker_fangs");
public static final EntityType EXPERIENCE_BOTTLE = register("minecraft:experience_bottle");
public static final EntityType EXPERIENCE_ORB = register("minecraft:experience_orb");
public static final EntityType EYE_OF_ENDER = register("minecraft:eye_of_ender");
public static final EntityType FALLING_BLOCK = register("minecraft:falling_block");
public static final EntityType FIREBALL = register("minecraft:fireball");
public static final EntityType FIREWORK_ROCKET = register("minecraft:firework_rocket");
public static final EntityType FISHING_BOBBER = register("minecraft:fishing_bobber");
public static final EntityType FURNACE_MINECART = register("minecraft:furnace_minecart");
public static final EntityType GHAST = register("minecraft:ghast");
public static final EntityType GIANT = register("minecraft:giant");
public static final EntityType GUARDIAN = register("minecraft:guardian");
public static final EntityType HOPPER_MINECART = register("minecraft:hopper_minecart");
public static final EntityType HORSE = register("minecraft:horse");
public static final EntityType HUSK = register("minecraft:husk");
public static final EntityType ILLUSIONER = register("minecraft:illusioner");
public static final EntityType IRON_GOLEM = register("minecraft:iron_golem");
public static final EntityType ITEM = register("minecraft:item");
public static final EntityType ITEM_FRAME = register("minecraft:item_frame");
public static final EntityType LEASH_KNOT = register("minecraft:leash_knot");
public static final EntityType LIGHTNING_BOLT = register("minecraft:lightning_bolt");
public static final EntityType LLAMA = register("minecraft:llama");
public static final EntityType LLAMA_SPIT = register("minecraft:llama_spit");
public static final EntityType MAGMA_CUBE = register("minecraft:magma_cube");
public static final EntityType MINECART = register("minecraft:minecart");
public static final EntityType MOOSHROOM = register("minecraft:mooshroom");
public static final EntityType MULE = register("minecraft:mule");
public static final EntityType OCELOT = register("minecraft:ocelot");
public static final EntityType PAINTING = register("minecraft:painting");
public static final EntityType PARROT = register("minecraft:parrot");
public static final EntityType PHANTOM = register("minecraft:phantom");
public static final EntityType PIG = register("minecraft:pig");
public static final EntityType PLAYER = register("minecraft:player");
public static final EntityType POLAR_BEAR = register("minecraft:polar_bear");
public static final EntityType POTION = register("minecraft:potion");
public static final EntityType PUFFERFISH = register("minecraft:pufferfish");
public static final EntityType RABBIT = register("minecraft:rabbit");
public static final EntityType SALMON = register("minecraft:salmon");
public static final EntityType SHEEP = register("minecraft:sheep");
public static final EntityType SHULKER = register("minecraft:shulker");
public static final EntityType SHULKER_BULLET = register("minecraft:shulker_bullet");
public static final EntityType SILVERFISH = register("minecraft:silverfish");
public static final EntityType SKELETON = register("minecraft:skeleton");
public static final EntityType SKELETON_HORSE = register("minecraft:skeleton_horse");
public static final EntityType SLIME = register("minecraft:slime");
public static final EntityType SMALL_FIREBALL = register("minecraft:small_fireball");
public static final EntityType SNOW_GOLEM = register("minecraft:snow_golem");
public static final EntityType SNOWBALL = register("minecraft:snowball");
public static final EntityType SPAWNER_MINECART = register("minecraft:spawner_minecart");
public static final EntityType SPECTRAL_ARROW = register("minecraft:spectral_arrow");
public static final EntityType SPIDER = register("minecraft:spider");
public static final EntityType SQUID = register("minecraft:squid");
public static final EntityType STRAY = register("minecraft:stray");
public static final EntityType TNT = register("minecraft:tnt");
public static final EntityType TNT_MINECART = register("minecraft:tnt_minecart");
public static final EntityType TRIDENT = register("minecraft:trident");
public static final EntityType TROPICAL_FISH = register("minecraft:tropical_fish");
public static final EntityType TURTLE = register("minecraft:turtle");
public static final EntityType VEX = register("minecraft:vex");
public static final EntityType VILLAGER = register("minecraft:villager");
public static final EntityType VINDICATOR = register("minecraft:vindicator");
public static final EntityType WITCH = register("minecraft:witch");
public static final EntityType WITHER = register("minecraft:wither");
public static final EntityType WITHER_SKELETON = register("minecraft:wither_skeleton");
public static final EntityType WITHER_SKULL = register("minecraft:wither_skull");
public static final EntityType WOLF = register("minecraft:wolf");
public static final EntityType ZOMBIE = register("minecraft:zombie");
public static final EntityType ZOMBIE_HORSE = register("minecraft:zombie_horse");
public static final EntityType ZOMBIE_PIGMAN = register("minecraft:zombie_pigman");
public static final EntityType ZOMBIE_VILLAGER = register("minecraft:zombie_villager");
;
private EntityTypes() {
private String id;
EntityTypes() {
this(null);
}
EntityTypes(String id) {
if (id == null) id = "minecraft:" + name().toLowerCase();
// If it has no namespace, assume minecraft.
else if (!id.contains(":")) {
id = "minecraft:" + id;
}
this.id = id;
}
@Override
public String getId() {
return id;
}
@Override
public String toString() {
return getId();
}
/*
-----------------------------------------------------
Static Initializer
-----------------------------------------------------
*/
public static EntityType parse(String id) {
if (id.startsWith("minecraft:")) id = id.substring(10);
switch (id) {
@ -148,7 +195,7 @@ public class EntityTypes {
default:
if (Character.isUpperCase(id.charAt(0))) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < result.length(); i++) {
for (int i = 0; i < id.length(); i++) {
char c = id.charAt(i);
if (Character.isUpperCase(c)) {
c = Character.toLowerCase(c);
@ -189,18 +236,56 @@ public class EntityTypes {
}
}
private static EntityType register(final String id) {
return register(new EntityType(id));
private static final Map<String, EntityTypes> $REGISTRY = new HashMap<>();
public static final EntityTypes[] values;
static {
try {
Collection<String> ents = WorldEdit.getInstance().getPlatformManager().queryCapability(Capability.GAME_HOOKS).getRegistries().getEntityRegistry().registerEntities();
if (!ents.isEmpty()) { // No types found - use defaults
for (String ent : ents) {
register(ent);
}
}
// Cache the values
values = values();
} catch (Throwable e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public static EntityType register(final EntityType entityType) {
String id = entityType.getId();
if (id.startsWith("minecraft:")) EntityType.REGISTRY.register(id.substring(10), entityType);
return EntityType.REGISTRY.register(id, entityType);
private static EntityTypes register(final String id) {
// Get the enum name (remove namespace if minecraft:)
int propStart = id.indexOf('[');
String typeName = id.substring(0, propStart == -1 ? id.length() : propStart);
String enumName = (typeName.startsWith("minecraft:") ? typeName.substring(10) : typeName).toUpperCase();
// Check existing
EntityTypes existing = null;
try { existing = valueOf(enumName.toUpperCase()); } catch (IllegalArgumentException ignore) {}
if (existing != null) {
// TODO additional registration
} else {
// Create it
existing = ReflectionUtils.addEnum(EntityTypes.class, enumName, new Class[]{String.class}, new Object[]{id});
}
if (typeName.startsWith("minecraft:")) $REGISTRY.put(typeName.substring(10), existing);
$REGISTRY.put(typeName, existing);
return existing;
}
public static @Nullable EntityType get(final String id) {
return EntityType.REGISTRY.get(id);
public static final @Nullable EntityTypes get(final String id) {
return $REGISTRY.get(id);
}
@Deprecated
public static final EntityTypes get(final int ordinal) {
return values[ordinal];
}
public static int size() {
return values.length;
}
}

View File

@ -28,6 +28,7 @@ import com.sk89q.worldedit.extension.platform.Capability;
import com.sk89q.worldedit.registry.NamespacedRegistry;
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.block.BlockTypes;
import com.sk89q.worldedit.world.entity.EntityTypes;
import com.sk89q.worldedit.world.registry.BundledItemData;
import com.sk89q.worldedit.world.registry.LegacyMapper;
@ -914,10 +915,10 @@ public enum ItemTypes implements ItemType {
}
/*
-----------------------------------------------------
Static Initializer
-----------------------------------------------------
*/
-----------------------------------------------------
Static Initializer
-----------------------------------------------------
*/
private static final Map<String, ItemTypes> $REGISTRY = new HashMap<>();
public static final ItemTypes[] values;
@ -961,7 +962,8 @@ public enum ItemTypes implements ItemType {
String typeName = id.substring(0, propStart == -1 ? id.length() : propStart);
String enumName = (typeName.startsWith("minecraft:") ? typeName.substring(10) : typeName).toUpperCase();
// Check existing
ItemTypes existing = valueOf(enumName.toUpperCase());
ItemTypes existing = null;
try { existing = valueOf(enumName.toUpperCase()); } catch (IllegalArgumentException ignore) {}
if (existing != null) {
// TODO additional registration
} else {

View File

@ -19,9 +19,16 @@
package com.sk89q.worldedit.world.registry;
import com.sk89q.worldedit.blocks.BaseItem;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.world.entity.EntityTypes;
import com.sk89q.worldedit.world.item.ItemType;
import com.sk89q.worldedit.world.item.ItemTypes;
import javax.annotation.Nullable;
import java.util.Collection;
import java.util.Collections;
/**
* Provides information on entities.
@ -35,6 +42,15 @@ public interface EntityRegistry {
* @return the entity, which may be null if the entity does not exist
*/
@Nullable
BaseEntity createFromId(String id);
default BaseEntity createFromId(String id) {
EntityTypes entType = EntityTypes.get(id);
return entType == null ? null : new BaseEntity(entType);
}
/**
* Register all entities
*/
default Collection<String> registerEntities() {
return Collections.emptyList();
}
}