Search in sources :

Example 46 with Prefab

use of org.terasology.entitySystem.prefab.Prefab in project Terasology by MovingBlocks.

the class HealthAuthoritySystem method damageEntity.

static void damageEntity(AttackEvent event, EntityRef targetEntity) {
    int damage = 1;
    Prefab damageType = EngineDamageTypes.PHYSICAL.get();
    // Calculate damage from item
    ItemComponent item = event.getDirectCause().getComponent(ItemComponent.class);
    if (item != null) {
        damage = item.baseDamage;
        if (item.damageType != null) {
            damageType = item.damageType;
        }
    }
    targetEntity.send(new DoDamageEvent(damage, damageType, event.getInstigator(), event.getDirectCause()));
    // consume the event so that the health system can take priority over default engine behavior
    event.consume();
}
Also used : ItemComponent(org.terasology.logic.inventory.ItemComponent) Prefab(org.terasology.entitySystem.prefab.Prefab)

Example 47 with Prefab

use of org.terasology.entitySystem.prefab.Prefab in project Terasology by MovingBlocks.

the class HealthCommands method restoreCollisionDamage.

@Command(shortDescription = "Restore default collision damage values", runOnServer = true, requiredPermission = PermissionManager.CHEAT_PERMISSION)
public String restoreCollisionDamage(@Sender EntityRef client) {
    ClientComponent clientComp = client.getComponent(ClientComponent.class);
    Optional<Prefab> prefab = Assets.get(new ResourceUrn("engine:player"), Prefab.class);
    HealthComponent healthDefault = prefab.get().getComponent(HealthComponent.class);
    HealthComponent health = clientComp.character.getComponent(HealthComponent.class);
    if (health != null && healthDefault != null) {
        health.fallingDamageSpeedThreshold = healthDefault.fallingDamageSpeedThreshold;
        health.horizontalDamageSpeedThreshold = healthDefault.horizontalDamageSpeedThreshold;
        health.excessSpeedDamageMultiplier = healthDefault.excessSpeedDamageMultiplier;
        clientComp.character.saveComponent(health);
    }
    return "Normal collision damage values restored";
}
Also used : ResourceUrn(org.terasology.assets.ResourceUrn) ClientComponent(org.terasology.network.ClientComponent) Prefab(org.terasology.entitySystem.prefab.Prefab) Command(org.terasology.logic.console.commandSystem.annotations.Command)

Example 48 with Prefab

use of org.terasology.entitySystem.prefab.Prefab in project Terasology by MovingBlocks.

the class ItemCommands method give.

@Command(shortDescription = "Adds an item or block to your inventory", helpText = "Puts the desired number of the given item or block with the given shape into your inventory", runOnServer = true, requiredPermission = PermissionManager.CHEAT_PERMISSION)
public String give(@Sender EntityRef client, @CommandParam("prefabId or blockName") String itemPrefabName, @CommandParam(value = "amount", required = false) Integer amount, @CommandParam(value = "blockShapeName", required = false) String shapeUriParam) {
    int itemAmount = amount != null ? amount : 1;
    if (itemAmount < 1) {
        return "Requested zero (0) items / blocks!";
    }
    Set<ResourceUrn> matches = assetManager.resolve(itemPrefabName, Prefab.class);
    if (matches.size() == 1) {
        Prefab prefab = assetManager.getAsset(matches.iterator().next(), Prefab.class).orElse(null);
        if (prefab != null && prefab.getComponent(ItemComponent.class) != null) {
            EntityRef playerEntity = client.getComponent(ClientComponent.class).character;
            for (int quantityLeft = itemAmount; quantityLeft > 0; quantityLeft--) {
                EntityRef item = entityManager.create(prefab);
                if (!inventoryManager.giveItem(playerEntity, playerEntity, item)) {
                    item.destroy();
                    itemAmount -= quantityLeft;
                    break;
                }
            }
            return "You received " + (itemAmount > 1 ? itemAmount + " items of " : "an item of ") + // TODO Use item display name
            prefab.getName() + (shapeUriParam != null ? " (Item can not have a shape)" : "");
        }
    } else if (matches.size() > 1) {
        StringBuilder builder = new StringBuilder();
        builder.append("Requested item \"");
        builder.append(itemPrefabName);
        builder.append("\": matches ");
        Joiner.on(" and ").appendTo(builder, matches);
        builder.append(". Please fully specify one.");
        return builder.toString();
    }
    // If no no matches are found for items, try blocks
    String message = blockCommands.giveBlock(client, itemPrefabName, amount, shapeUriParam);
    if (message != null) {
        return message;
    }
    return "Could not find an item or block matching \"" + itemPrefabName + "\"";
}
Also used : ResourceUrn(org.terasology.assets.ResourceUrn) Prefab(org.terasology.entitySystem.prefab.Prefab) EntityRef(org.terasology.entitySystem.entity.EntityRef) ClientComponent(org.terasology.network.ClientComponent) Command(org.terasology.logic.console.commandSystem.annotations.Command)

Example 49 with Prefab

use of org.terasology.entitySystem.prefab.Prefab in project Terasology by MovingBlocks.

the class ItemCommands method listItems.

@Command(shortDescription = "Lists all available items (prefabs)\nYou can filter by adding the beginning of words " + "after the commands, e.g.: \"listItems engine: core:\" will list all items from the engine and core module", requiredPermission = PermissionManager.CHEAT_PERMISSION)
public String listItems(@CommandParam(value = "startsWith", required = false) String[] startsWith) {
    List<String> stringItems = Lists.newArrayList();
    for (Prefab prefab : prefabManager.listPrefabs()) {
        if (!BlockCommands.uriStartsWithAnyString(prefab.getName(), startsWith)) {
            continue;
        }
        stringItems.add(prefab.getName());
    }
    Collections.sort(stringItems);
    StringBuilder items = new StringBuilder();
    for (String item : stringItems) {
        if (!items.toString().isEmpty()) {
            items.append(Console.NEW_LINE);
        }
        items.append(item);
    }
    return items.toString();
}
Also used : Prefab(org.terasology.entitySystem.prefab.Prefab) Command(org.terasology.logic.console.commandSystem.annotations.Command)

Aggregations

Prefab (org.terasology.entitySystem.prefab.Prefab)49 PojoPrefab (org.terasology.entitySystem.prefab.internal.PojoPrefab)20 PrefabData (org.terasology.entitySystem.prefab.PrefabData)13 Test (org.junit.Test)11 ResourceUrn (org.terasology.assets.ResourceUrn)9 Component (org.terasology.entitySystem.Component)9 EntityRef (org.terasology.entitySystem.entity.EntityRef)9 ModuleAwareAssetTypeManager (org.terasology.assets.module.ModuleAwareAssetTypeManager)7 ContextImpl (org.terasology.context.internal.ContextImpl)6 ModuleManager (org.terasology.engine.module.ModuleManager)6 StringComponent (org.terasology.entitySystem.stubs.StringComponent)6 Command (org.terasology.logic.console.commandSystem.annotations.Command)6 ClientComponent (org.terasology.network.ClientComponent)6 Before (org.junit.Before)5 EntityBuilder (org.terasology.entitySystem.entity.EntityBuilder)5 NetworkComponent (org.terasology.network.NetworkComponent)5 BeforeClass (org.junit.BeforeClass)4 NetworkSystem (org.terasology.network.NetworkSystem)4 EngineEntityManager (org.terasology.entitySystem.entity.internal.EngineEntityManager)3 AssetManager (org.terasology.assets.management.AssetManager)2