use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class InventoryClientSystem method moveItemToSlots.
@Override
public boolean moveItemToSlots(EntityRef instigator, EntityRef fromInventory, int slotFrom, EntityRef toInventory, List<Integer> toSlots) {
Collection<EntityRef> clientTempEntities = new HashSet<>();
if (moveItemToSlotsFillClientTempEntities(instigator, fromInventory, slotFrom, toInventory, toSlots, clientTempEntities)) {
return false;
}
MoveItemToSlotsRequest request = new MoveItemToSlotsRequest(instigator, fromInventory, slotFrom, toInventory, toSlots, changeId++, clientTempEntities);
pendingMoves.put(request.getChangeId(), request);
localPlayer.getClientEntity().send(request);
return true;
}
use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class InventoryUtils method moveToFreeSlots.
/**
* @param instigator used to verify if the action is allowed
* @param to has to provide {@link InventoryComponent} for a successful transfer
* @param slotFrom slot number to take the items from.
* @param from has to provide {@link InventoryComponent} for a successful transfer
* @param toSlots slots that will be checked if they are free
* @return true if at least 1 item got moved from the specified location.
*/
private static boolean moveToFreeSlots(EntityRef instigator, EntityRef from, int slotFrom, EntityRef to, List<Integer> toSlots) {
EntityRef fromItem = getItemAt(from, slotFrom);
ItemComponent fromItemComp = fromItem.getComponent(ItemComponent.class);
if (fromItemComp == null) {
return false;
}
for (int toSlot : toSlots) {
EntityRef toItem = getItemAt(to, toSlot);
if (!toItem.exists()) {
BeforeItemPutInInventory putTo = new BeforeItemPutInInventory(instigator, fromItem, toSlot);
to.send(putTo);
boolean allowed = !putTo.isConsumed();
if (allowed) {
putItemIntoSlot(from, EntityRef.NULL, slotFrom);
putItemIntoSlot(to, fromItem, toSlot);
return true;
}
}
}
return false;
}
use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class InventoryUtils method putItemIntoSlot.
static void putItemIntoSlot(EntityRef entity, EntityRef item, int slot) {
InventoryComponent inventory = entity.getComponent(InventoryComponent.class);
EntityRef oldItem = inventory.itemSlots.get(slot);
inventory.itemSlots.set(slot, item);
entity.saveComponent(inventory);
entity.send(new InventorySlotChangedEvent(slot, oldItem, item));
}
use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class EntityAwareWorldProvider method updateBlockEntity.
private void updateBlockEntity(EntityRef blockEntity, Vector3i pos, Block oldType, Block type, boolean forceEntityUpdate, Set<Class<? extends Component>> retainComponents) {
if (type.isKeepActive()) {
temporaryBlockEntities.remove(blockEntity);
} else if (oldType.isKeepActive() && isTemporaryBlock(blockEntity, type)) {
temporaryBlockEntities.add(blockEntity);
}
if (forceEntityUpdate || !(Objects.equal(oldType.getBlockFamily(), type.getBlockFamily()) && Objects.equal(oldType.getPrefab(), type.getPrefab()))) {
updateBlockEntityComponents(blockEntity, oldType, type, retainComponents);
}
EntityRef regionEntity = blockRegionLookup.get(pos);
if (regionEntity != null) {
regionEntity.send(new OnChangedBlock(pos, type, oldType));
}
blockEntity.send(new OnChangedBlock(new Vector3i(pos), type, oldType));
}
use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class EntityAwareWorldProvider method createBlockEntity.
private EntityRef createBlockEntity(Vector3i blockPosition, Block block) {
EntityBuilder builder = entityManager.newBuilder(block.getPrefab().orElse(null));
builder.addComponent(new LocationComponent(blockPosition.toVector3f()));
builder.addComponent(new BlockComponent(block, blockPosition));
boolean isTemporary = isTemporaryBlock(builder, block);
if (!isTemporary && !builder.hasComponent(NetworkComponent.class)) {
builder.addComponent(new NetworkComponent());
}
EntityRef blockEntity;
if (isTemporary) {
blockEntity = builder.buildWithoutLifecycleEvents();
temporaryBlockEntities.add(blockEntity);
} else {
blockEntity = builder.build();
}
blockEntityLookup.put(new Vector3i(blockPosition), blockEntity);
return blockEntity;
}
Aggregations