Search in sources :

Example 36 with ItemType

use of org.spongepowered.api.item.ItemType in project AdamantineShield by Karanum.

the class LookupLine method getHoverText.

public Text getHoverText() {
    Text result = Text.of(TextColors.DARK_AQUA, "Location: ", TextColors.AQUA, pos.toString());
    if (data == null)
        return result;
    ConfigurationNode workingNode = null;
    ConfigurationNode node = null;
    try {
        node = DataUtils.configNodeFromString(data);
    } catch (IOException e) {
        e.printStackTrace();
        return result;
    }
    // Common tags
    workingNode = node.getNode("UnsafeDamage");
    if (!workingNode.isVirtual()) {
        result = Text.of(result, Text.NEW_LINE, TextColors.DARK_AQUA, "Damage: ", TextColors.AQUA, workingNode.getInt());
    }
    // Item exclusive tags
    if (target instanceof ItemType) {
        workingNode = node.getNode("UnsafeData", "SkullOwner");
        if (!workingNode.isVirtual()) {
            String name = workingNode.getString();
            if (!workingNode.getNode("Name").isVirtual())
                name = workingNode.getNode("Name").getString();
            result = Text.of(result, Text.NEW_LINE, TextColors.DARK_AQUA, "Player: ", TextColors.AQUA, name);
        }
        workingNode = node.getNode("UnsafeData", "display");
        if (!workingNode.isVirtual()) {
            ConfigurationNode innerNode = workingNode.getNode("Name");
            if (!innerNode.isVirtual()) {
                result = Text.of(result, Text.NEW_LINE, TextColors.DARK_AQUA, "Name: ", TextColors.AQUA, innerNode.getString());
            }
            innerNode = workingNode.getNode("color");
            if (!innerNode.isVirtual()) {
                int color = innerNode.getInt();
                result = Text.of(result, Text.NEW_LINE, TextColors.DARK_AQUA, "Color: ", TextColors.AQUA, "(", TextColors.RED, color >> 16, TextColors.AQUA, ", ", TextColors.GREEN, (color >> 8) % 256, TextColors.AQUA, ", ", TextColors.BLUE, color % 256, TextColors.AQUA, ")");
            }
            innerNode = workingNode.getNode("Lore");
            if (!innerNode.isVirtual()) {
                try {
                    Text sub = Text.of(TextColors.DARK_AQUA, "Lore: ");
                    for (String line : innerNode.getList(TypeToken.of(String.class))) {
                        sub = Text.of(sub, Text.NEW_LINE, TextColors.DARK_AQUA, " - ", TextColors.AQUA, line);
                    }
                    result = Text.of(result, Text.NEW_LINE, sub);
                } catch (ObjectMappingException e) {
                    e.printStackTrace();
                }
            }
        }
        workingNode = node.getNode("UnsafeData", "title");
        if (!workingNode.isVirtual()) {
            result = Text.of(result, Text.NEW_LINE, TextColors.DARK_AQUA, "Title: ", TextColors.AQUA, workingNode.getString());
        }
        workingNode = node.getNode("UnsafeData", "author");
        if (!workingNode.isVirtual()) {
            result = Text.of(result, Text.NEW_LINE, TextColors.DARK_AQUA, "Author: ", TextColors.AQUA, workingNode.getString());
        }
        workingNode = node.getNode("UnsafeData", "Unbreakable");
        if (!workingNode.isVirtual()) {
            result = Text.of(result, Text.NEW_LINE, TextColors.AQUA, "Is unbreakable");
        }
    }
    // Block exclusive tags
    if (target instanceof BlockType) {
        workingNode = node.getNode("UnsafeData", "Owner", "Name");
        if (!workingNode.isVirtual() && !workingNode.getString().isEmpty()) {
            result = Text.of(result, Text.NEW_LINE, TextColors.DARK_AQUA, "Player: ", TextColors.AQUA, workingNode.getString());
        }
        workingNode = node.getNode("UnsafeData", "CustomName");
        if (!workingNode.isVirtual()) {
            result = Text.of(result, Text.NEW_LINE, TextColors.DARK_AQUA, "Name: ", TextColors.AQUA, workingNode.getString());
        }
        workingNode = node.getNode("UnsafeData", "Text1");
        if (!workingNode.isVirtual()) {
            ConfigurationNode dataNode = node.getNode("UnsafeData");
            if (!(dataNode.getNode("Text2").isVirtual() || dataNode.getNode("Text3").isVirtual() || dataNode.getNode("Text4").isVirtual())) {
                result = Text.of(result, Text.NEW_LINE, TextColors.DARK_AQUA, "Text:");
                Pattern p = DataUtils.SIGN_TEXT_REGEX;
                for (int i = 1; i <= 4; ++i) {
                    Matcher m = p.matcher(dataNode.getNode("Text" + i).getString());
                    if (m.matches()) {
                        result = Text.of(result, Text.NEW_LINE, TextColors.DARK_AQUA, " - ", TextColors.AQUA, m.group(1));
                    }
                }
            } else {
                result = Text.of(result, Text.NEW_LINE, TextColors.RED, "Contains incomplete sign data");
            }
        }
        workingNode = node.getNode("UnsafeData", "Lock");
        if (!workingNode.isVirtual()) {
            result = Text.of(result, Text.NEW_LINE, TextColors.AQUA, "Is locked");
        }
    }
    return result;
}
Also used : Pattern(java.util.regex.Pattern) ConfigurationNode(ninja.leaping.configurate.ConfigurationNode) BlockType(org.spongepowered.api.block.BlockType) Matcher(java.util.regex.Matcher) ItemType(org.spongepowered.api.item.ItemType) Text(org.spongepowered.api.text.Text) IOException(java.io.IOException) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException)

Example 37 with ItemType

use of org.spongepowered.api.item.ItemType in project AdamantineShield by Karanum.

the class RollbackManager method performAddition.

// TODO: Set proper causes for block changes caused by rollback/undo
private void performAddition(LookupLine line) {
    World w = Sponge.getServer().getWorld(line.getWorld()).orElse(null);
    if (w == null)
        return;
    if (line.getTarget() instanceof ItemType) {
        Optional<TileEntity> te = w.getTileEntity(line.getPos());
        if (te.isPresent() && te.get() instanceof TileEntityCarrier) {
            TileEntityCarrier c = (TileEntityCarrier) te.get();
            Inventory i = c.getInventory();
            ItemType type = (ItemType) line.getTarget();
            ItemStack stack = ItemStack.builder().fromContainer(line.getDataAsView()).itemType(type).quantity(line.getCount()).build();
            Inventory slot = i.query(QueryOperationTypes.INVENTORY_PROPERTY.of(SlotIndex.of(line.getSlot())));
            slot.set(stack);
        }
    } else if (line.getTarget() instanceof BlockType) {
        BlockState block = null;
        if (line.getDataAsView() == null) {
            block = BlockState.builder().blockType((BlockType) line.getTarget()).build();
            w.setBlock(line.getPos(), block);
        } else {
            DataView blockData = line.getDataAsView();
            DataView blockState = blockData.getView(DataQuery.of("BlockState")).orElse(null);
            block = BlockState.builder().build(blockState).orElse(null);
            if (block != null)
                w.setBlock(line.getPos(), block);
        }
    }
}
Also used : TileEntity(org.spongepowered.api.block.tileentity.TileEntity) DataView(org.spongepowered.api.data.DataView) BlockState(org.spongepowered.api.block.BlockState) BlockType(org.spongepowered.api.block.BlockType) ItemType(org.spongepowered.api.item.ItemType) TileEntityCarrier(org.spongepowered.api.block.tileentity.carrier.TileEntityCarrier) World(org.spongepowered.api.world.World) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Inventory(org.spongepowered.api.item.inventory.Inventory)

Example 38 with ItemType

use of org.spongepowered.api.item.ItemType in project AdamantineShield by Karanum.

the class RollbackManager method performRemoval.

private void performRemoval(LookupLine line) {
    World w = Sponge.getServer().getWorld(line.getWorld()).orElse(null);
    if (w == null)
        return;
    if (line.getTarget() instanceof ItemType) {
        Optional<TileEntity> te = w.getTileEntity(line.getPos());
        if (te.isPresent() && te.get() instanceof TileEntityCarrier) {
            TileEntityCarrier c = (TileEntityCarrier) te.get();
            Inventory i = c.getInventory();
            Inventory slot = i.query(QueryOperationTypes.INVENTORY_PROPERTY.of(SlotIndex.of(line.getSlot())));
            slot.set(ItemStack.of(ItemTypes.NONE, 0));
        }
    } else if (line.getTarget() instanceof BlockType) {
        BlockState block = BlockState.builder().blockType(BlockTypes.AIR).build();
        w.setBlock(line.getPos(), block);
    }
}
Also used : TileEntity(org.spongepowered.api.block.tileentity.TileEntity) BlockState(org.spongepowered.api.block.BlockState) BlockType(org.spongepowered.api.block.BlockType) ItemType(org.spongepowered.api.item.ItemType) TileEntityCarrier(org.spongepowered.api.block.tileentity.carrier.TileEntityCarrier) World(org.spongepowered.api.world.World) Inventory(org.spongepowered.api.item.inventory.Inventory)

Example 39 with ItemType

use of org.spongepowered.api.item.ItemType in project core by CubeEngine.

the class MaterialMatcher method buildLocalizedNames.

private void buildLocalizedNames(Map<String, ItemType> defLocalizedName, Map<String, ItemType> localizedName) {
    for (ItemType itemType : Sponge.getRegistry().getAllOf(ItemType.class)) {
        String id = itemType.getName();
        ids.put(id, itemType);
        if (id.startsWith("minecraft:")) {
            ids.put(id.substring("minecraft:".length()), itemType);
        }
        String defName = itemType.getTranslation().get(Locale.getDefault());
        defLocalizedName.put(defName, itemType);
        String[] splitDefName = defName.split(" ");
        if (splitDefName.length > 1) {
            defName = splitDefName[splitDefName.length - 1];
            defName += String.join(" ", splitDefName).replace(defName, "");
            defLocalizedName.put(defName, itemType);
        }
        String sourceName = itemType.getTranslation().get(Locale.US);
        localizedName.put(sourceName, itemType);
        String[] splitSourceName = sourceName.split(" ");
        if (splitSourceName.length > 1) {
            sourceName = splitSourceName[splitSourceName.length - 1];
            sourceName += String.join(" ", splitSourceName).replace(sourceName, "");
            localizedName.put(sourceName, itemType);
        }
    }
}
Also used : ItemType(org.spongepowered.api.item.ItemType)

Example 40 with ItemType

use of org.spongepowered.api.item.ItemType in project core by CubeEngine.

the class ItemTypeParser method suggest.

@Override
public List<String> suggest(Class type, CommandInvocation invocation) {
    ArrayList<String> list = new ArrayList<>();
    String token = invocation.currentToken().toLowerCase();
    if (MINECRAFT.startsWith(token)) {
        list.add(MINECRAFT);
    }
    boolean startMc = token.startsWith(MINECRAFT);
    for (ItemType iType : Sponge.getRegistry().getAllOf(ItemType.class)) {
        if (iType.getId().startsWith(token)) {
            if (!iType.getId().startsWith(MINECRAFT) || startMc) {
                list.add(iType.getId());
            }
        }
        if (iType.getId().startsWith(MINECRAFT + token)) {
            list.add(iType.getId().substring(MINECRAFT.length()));
        }
    }
    return list;
}
Also used : ItemType(org.spongepowered.api.item.ItemType) ArrayList(java.util.ArrayList)

Aggregations

ItemType (org.spongepowered.api.item.ItemType)53 ItemStack (org.spongepowered.api.item.inventory.ItemStack)19 BlockState (org.spongepowered.api.block.BlockState)12 Listener (org.spongepowered.api.event.Listener)12 World (org.spongepowered.api.world.World)11 Optional (java.util.Optional)9 Text (org.spongepowered.api.text.Text)8 Region (br.net.fabiozumbi12.RedProtect.Sponge.Region)7 ArrayList (java.util.ArrayList)7 Sponge (org.spongepowered.api.Sponge)7 BlockType (org.spongepowered.api.block.BlockType)7 List (java.util.List)6 BlockSnapshot (org.spongepowered.api.block.BlockSnapshot)6 Player (org.spongepowered.api.entity.living.player.Player)6 Vector3d (com.flowpowered.math.vector.Vector3d)5 CommandResult (org.spongepowered.api.command.CommandResult)5 Collectors (java.util.stream.Collectors)4 IBlockState (net.minecraft.block.state.IBlockState)4 Inventory (org.spongepowered.api.item.inventory.Inventory)4 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)4