Search in sources :

Example 81 with EntityRef

use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class EntityAwareWorldProvider method setBlockRetainComponent.

@Override
@SafeVarargs
public final Block setBlockRetainComponent(Vector3i pos, Block type, Class<? extends Component>... components) {
    if (GameThread.isCurrentThread()) {
        EntityRef blockEntity = getBlockEntityAt(pos);
        Block oldType = super.setBlock(pos, type);
        if (oldType != null) {
            updateBlockEntity(blockEntity, pos, oldType, type, false, Sets.newHashSet(components));
        }
        return oldType;
    }
    return null;
}
Also used : Block(org.terasology.world.block.Block) OnChangedBlock(org.terasology.world.OnChangedBlock) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Example 82 with EntityRef

use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class BlockCommands method replaceBlock.

@Command(shortDescription = "Replaces a block in front of user", helpText = "Replaces a block in front of the user at the specified max distance", runOnServer = true, requiredPermission = PermissionManager.CHEAT_PERMISSION)
public void replaceBlock(@Sender EntityRef sender, @CommandParam("blockName") String uri, @CommandParam(value = "maxDistance", required = false) Integer maxDistanceParam) {
    int maxDistance = maxDistanceParam != null ? maxDistanceParam : 12;
    EntityRef playerEntity = sender.getComponent(ClientComponent.class).character;
    EntityRef gazeEntity = GazeAuthoritySystem.getGazeEntityForCharacter(playerEntity);
    LocationComponent gazeLocation = gazeEntity.getComponent(LocationComponent.class);
    Set<ResourceUrn> matchingUris = Assets.resolveAssetUri(uri, BlockFamilyDefinition.class);
    targetSystem.updateTarget(gazeLocation.getWorldPosition(), gazeLocation.getWorldDirection(), maxDistance);
    EntityRef target = targetSystem.getTarget();
    BlockComponent targetLocation = target.getComponent(BlockComponent.class);
    if (matchingUris.size() == 1) {
        Optional<BlockFamilyDefinition> def = Assets.get(matchingUris.iterator().next(), BlockFamilyDefinition.class);
        if (def.isPresent()) {
            BlockFamily blockFamily = blockManager.getBlockFamily(uri);
            Block block = blockManager.getBlock(blockFamily.getURI());
            world.setBlock(targetLocation.getPosition(), block);
        } else if (matchingUris.size() > 1) {
            StringBuilder builder = new StringBuilder();
            builder.append("Non-unique shape name, possible matches: ");
            Iterator<ResourceUrn> shapeUris = sortItems(matchingUris).iterator();
            while (shapeUris.hasNext()) {
                builder.append(shapeUris.next().toString());
                if (shapeUris.hasNext()) {
                    builder.append(", ");
                }
            }
        }
    }
}
Also used : ClientComponent(org.terasology.network.ClientComponent) LocationComponent(org.terasology.logic.location.LocationComponent) BlockComponent(org.terasology.world.block.BlockComponent) Iterator(java.util.Iterator) Block(org.terasology.world.block.Block) BlockFamily(org.terasology.world.block.family.BlockFamily) ResourceUrn(org.terasology.assets.ResourceUrn) BlockFamilyDefinition(org.terasology.world.block.loader.BlockFamilyDefinition) EntityRef(org.terasology.entitySystem.entity.EntityRef) Command(org.terasology.logic.console.commandSystem.annotations.Command)

Example 83 with EntityRef

use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class BlockEntitySystem method commonDefaultDropsHandling.

public void commonDefaultDropsHandling(CreateBlockDropsEvent event, EntityRef entity, Vector3i location, Block block) {
    BlockDamageModifierComponent blockDamageModifierComponent = event.getDamageType().getComponent(BlockDamageModifierComponent.class);
    float chanceOfBlockDrop = 1;
    if (blockDamageModifierComponent != null) {
        chanceOfBlockDrop = 1 - blockDamageModifierComponent.blockAnnihilationChance;
    }
    if (random.nextFloat() < chanceOfBlockDrop) {
        EntityRef item = blockItemFactory.newInstance(block.getBlockFamily(), entity);
        entity.send(new OnBlockToItem(item));
        if (shouldDropToWorld(event, block, blockDamageModifierComponent, item)) {
            float impulsePower = 0;
            if (blockDamageModifierComponent != null) {
                impulsePower = blockDamageModifierComponent.impulsePower;
            }
            processDropping(item, location, impulsePower);
        }
    }
}
Also used : BlockDamageModifierComponent(org.terasology.world.block.entity.damage.BlockDamageModifierComponent) OnBlockToItem(org.terasology.world.block.items.OnBlockToItem) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Example 84 with EntityRef

use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class SideBlockSupportRequired method getEntity.

private EntityRef getEntity(Vector3i location, Map<Vector3i, Block> blockOverrides) {
    final Block overwrittenBlock = blockOverrides.get(location);
    if (overwrittenBlock != null) {
        return overwrittenBlock.getEntity();
    }
    EntityRef blockEntity = getBlockEntityRegistry().getExistingBlockEntityAt(location);
    if (blockEntity.exists()) {
        return blockEntity;
    } else {
        return getWorldProvider().getBlock(location).getEntity();
    }
}
Also used : Block(org.terasology.world.block.Block) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Example 85 with EntityRef

use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class InventoryUtils method adjustStackSize.

static void adjustStackSize(EntityRef entity, int slot, int newCount) {
    InventoryComponent inventory = entity.getComponent(InventoryComponent.class);
    EntityRef item = inventory.itemSlots.get(slot);
    ItemComponent itemComponent = item.getComponent(ItemComponent.class);
    byte oldSize = itemComponent.stackCount;
    itemComponent.stackCount = (byte) newCount;
    item.saveComponent(itemComponent);
    entity.send(new InventorySlotStackSizeChangedEvent(slot, oldSize, newCount));
}
Also used : InventorySlotStackSizeChangedEvent(org.terasology.logic.inventory.events.InventorySlotStackSizeChangedEvent) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Aggregations

EntityRef (org.terasology.entitySystem.entity.EntityRef)337 Test (org.junit.Test)106 ClientComponent (org.terasology.network.ClientComponent)49 LocationComponent (org.terasology.logic.location.LocationComponent)45 Vector3f (org.terasology.math.geom.Vector3f)44 ReceiveEvent (org.terasology.entitySystem.event.ReceiveEvent)36 Vector3i (org.terasology.math.geom.Vector3i)34 Command (org.terasology.logic.console.commandSystem.annotations.Command)28 StringComponent (org.terasology.entitySystem.stubs.StringComponent)26 NetworkComponent (org.terasology.network.NetworkComponent)21 EntityData (org.terasology.protobuf.EntityData)21 DisplayNameComponent (org.terasology.logic.common.DisplayNameComponent)17 Block (org.terasology.world.block.Block)16 Component (org.terasology.entitySystem.Component)15 EntityBuilder (org.terasology.entitySystem.entity.EntityBuilder)15 CharacterComponent (org.terasology.logic.characters.CharacterComponent)14 Quat4f (org.terasology.math.geom.Quat4f)14 BlockComponent (org.terasology.world.block.BlockComponent)13 Map (java.util.Map)11 LocalPlayer (org.terasology.logic.players.LocalPlayer)11