private EntityData.Entity serializeEntityDelta(EntityRef entityRef, Prefab prefab) { EntityData.Entity.Builder entity = EntityData.Entity.newBuilder(); entity.setId(entityRef.getId()); entity.setParentPrefab(prefab.getName()); for (Component component : entityRef.iterateComponents()) { if (component.getClass().equals(EntityInfoComponent.class)) continue; Component prefabComponent = prefab.getComponent(component.getClass()); EntityData.Component componentData; if (prefabComponent == null) { componentData = serializeComponent(component); } else { componentData = serializeComponent(prefabComponent, component); } if (componentData != null) { entity.addComponent(componentData); } } for (Component prefabComponent : prefab.listComponents()) { if (!entityRef.hasComponent(prefabComponent.getClass())) { entity.addRemovedComponent(ComponentUtil.getComponentClassName(prefabComponent.getClass())); } } return entity.build(); }
@Override public EntityRef deserializeEntity(EntityData.Entity entityData) { EntityRef entity = entityManager.createEntityRefWithId(entityData.getId()); if (entityData.hasParentPrefab() && !entityData.getParentPrefab().isEmpty() && prefabManager.exists(entityData.getParentPrefab())) { Prefab prefab = prefabManager.getPrefab(entityData.getParentPrefab()); for (Component component : prefab.listComponents()) { String componentName = ComponentUtil.getComponentClassName(component.getClass()); if (!containsIgnoreCase(componentName, entityData.getRemovedComponentList())) { entity.addComponent(componentLibrary.copy(component)); } } entity.addComponent(new EntityInfoComponent(entityData.getParentPrefab())); } for (EntityData.Component componentData : entityData.getComponentList()) { Class<? extends Component> componentClass = getComponentClass(componentData); if (componentClass == null) continue; if (!entity.hasComponent(componentClass)) { entity.addComponent(deserializeComponent(componentData)); } else { deserializeComponentOnto(entity.getComponent(componentClass), componentData); } } return entity; }
/** * Ugly way to retrieve a name from a prefab * * @return a ":" seperated string, with name and flavor text. */ public static String getName() { PrefabManager prefMan = CoreRegistry.get(PrefabManager.class); Prefab prefab = prefMan.getPrefab("miniion:nameslist"); EntityRef namelist = CoreRegistry.get(EntityManager.class).create(prefab); namelist.hasComponent(namesComponent.class); namesComponent namecomp = namelist.getComponent(namesComponent.class); Random rand = new Random(); return namecomp.namelist.get(rand.nextInt(namecomp.namelist.size())); }
/** * triggered when a block was destroyed and dropped in the world used to intercept gathering by * minions and sending the block to their inventory the droppedblock in the world then gets * destroyed, possible duplication exploit */ @ReceiveEvent(components = {MinionComponent.class}) public void onBlockDropped(BlockDroppedEvent event, EntityRef entity) { if (entity.hasComponent(MinionComponent.class)) { EntityRef item; if (event.getOldBlock().getEntityMode() == BlockEntityMode.PERSISTENT) { item = blockItemFactory.newInstance(event.getOldBlock().getBlockFamily(), entity); } else { item = blockItemFactory.newInstance(event.getOldBlock().getBlockFamily()); } entity.send(new ReceiveItemEvent(item)); event.getDroppedBlock().destroy(); } }