Java Code Examples for org.spongepowered.api.item.inventory.ItemStack#of()

The following examples show how to use org.spongepowered.api.item.inventory.ItemStack#of() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UCVHelper56.java    From UltimateChat with GNU General Public License v3.0 5 votes vote down vote up
@Override
public ItemStack getItemInHand(Player sender) {
    if (sender.getItemInHand(HandTypes.MAIN_HAND).isPresent()) {
        return sender.getItemInHand(HandTypes.MAIN_HAND).get();
    } else if (sender.getItemInHand(HandTypes.OFF_HAND).isPresent()) {
        return sender.getItemInHand(HandTypes.OFF_HAND).get();
    }
    return ItemStack.of(ItemTypes.NONE, 1);
}
 
Example 2
Source File: ConfigManager.java    From RedProtect with GNU General Public License v3.0 5 votes vote down vote up
public ItemStack getGuiSeparator() {
    ItemStack separator = ItemStack.of(Sponge.getRegistry().getType(ItemType.class, guiRoot.gui_separator.material).orElse(ItemTypes.GLASS_PANE), 1);
    separator.offer(Keys.DISPLAY_NAME, RedProtect.get().guiLang.getFlagString("separator"));
    separator.offer(Keys.ITEM_DURABILITY, guiRoot.gui_separator.data);
    separator.offer(Keys.ITEM_LORE, Arrays.asList(Text.EMPTY, RedProtect.get().guiLang.getFlagString("separator")));
    return separator;
}
 
Example 3
Source File: ExecuteMethodParam.java    From Web-API with MIT License 4 votes vote down vote up
public Tuple<Class, Object> toObject()
        throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
    String val = value;
    String[] vals = val.split(":");

    switch (type) {
        case INT:
        case INTEGER:
            return new Tuple<>(Integer.class, Integer.parseInt(val));
        case FLOAT:
            return new Tuple<>(Float.class, Float.parseFloat(val));
        case DOUBLE:
            return new Tuple<>(Double.class, Double.parseDouble(val));
        case BOOL:
        case BOOLEAN:
            return new Tuple<>(Boolean.class, Boolean.parseBoolean(val));
        case BYTE:
            return new Tuple<>(Byte.class, Byte.parseByte(val));
        case CHAR:
            return new Tuple<>(Character.class, val.charAt(0));
        case LONG:
            return new Tuple<>(Long.class, Long.parseLong(val));
        case SHORT:
            return new Tuple<>(Short.class, Short.parseShort(val));
        case STRING:
            return new Tuple<>(String.class, val);
        case CLASS:
            return new Tuple<>(Class.class, Class.forName(val));
        case ENUM:
            Class clazz = Class.forName(vals[0]);
            return new Tuple<Class, Object>(clazz, Enum.valueOf(clazz, vals[1]));

        case VECTOR3D:
            return new Tuple<>(Vector3d.class, new Vector3d(
                    Double.parseDouble(vals[0]), Double.parseDouble(vals[1]), Double.parseDouble(vals[2])));

        case VECTOR3I:
            return new Tuple<>(Vector3i.class, new Vector3i(
                    Integer.parseInt(vals[0]), Integer.parseInt(vals[1]), Integer.parseInt(vals[2])));

        case TEXT:
            return new Tuple<>(Text.class, Text.of(val));

        case WORLD:
            Optional<World> w = Sponge.getServer().getWorld(UUID.fromString(val));
            return new Tuple<>(World.class, w.orElse(null));

        case PLAYER:
            Optional<Player> p = Sponge.getServer().getPlayer(UUID.fromString(val));
            return new Tuple<>(Player.class, p.orElse(null));

        case ITEMSTACK:
            Optional<ItemType> t = Sponge.getRegistry().getType(ItemType.class, vals[0]);
            if (!t.isPresent())
                throw new ClassNotFoundException(vals[0]);

            return new Tuple<>(ItemStack.class, ItemStack.of(t.get(), Integer.parseInt(vals[1])));

        case STATIC:
            Class c = Class.forName(vals[0]);
            Field f = c.getField(vals[1]);
            return new Tuple<>(f.getType(), f.get(null));

        default:
            return null;
    }
}
 
Example 4
Source File: FlagGui.java    From RedProtect with GNU General Public License v3.0 4 votes vote down vote up
public FlagGui(String name, Player player, Region region, boolean editable, int maxSlots) {
    this.editable = editable;
    this.player = player;
    this.region = region;
    if (maxSlots <= 9) {
        this.size = 9;
    } else if (maxSlots <= 18) {
        this.size = 18;
    } else if (maxSlots <= 27) {
        this.size = 27;
    } else if (maxSlots <= 36) {
        this.size = 36;
    } else if (maxSlots <= 45) {
        this.size = 45;
    } else if (maxSlots <= 54) {
        this.size = 54;
    } else {
        throw new IllegalArgumentException("Parameter size is exceeding size limit (54)");
    }
    this.guiItems = new ItemStack[this.size];

    for (String flag : region.getFlags().keySet()) {
        try {
            if (!(region.getFlags().get(flag) instanceof Boolean) || !RedProtect.get().config.guiRoot().gui_flags.containsKey(flag)) {
                continue;
            }
            if (RedProtect.get().ph.hasFlagPerm(player, flag) && (RedProtect.get().config.configRoot().flags.containsKey(flag) || RedProtect.get().config.AdminFlags.contains(flag))) {
                if (flag.equals("pvp") && !RedProtect.get().config.configRoot().flags.containsKey("pvp")) {
                    continue;
                }

                int i = RedProtect.get().config.getGuiSlot(flag);

                this.guiItems[i] = ItemStack.of(Sponge.getRegistry().getType(ItemType.class, RedProtect.get().config.guiRoot().gui_flags.get(flag).material).orElse(ItemTypes.GLASS_PANE), 1);
                this.guiItems[i].offer(Keys.DISPLAY_NAME, RedProtect.get().getUtil().toText(RedProtect.get().guiLang.getFlagName(flag)));
                List<Text> lore = new ArrayList<>(Arrays.asList(
                        Text.joinWith(Text.of(" "), RedProtect.get().guiLang.getFlagString("value"), RedProtect.get().guiLang.getFlagString(region.getFlags().get(flag).toString())),
                        RedProtect.get().getUtil().toText("&0" + flag)));
                lore.addAll(RedProtect.get().guiLang.getFlagDescription(flag));
                this.guiItems[i].offer(Keys.ITEM_LORE, lore);

                if (!this.region.getFlagBool(flag)) {
                    this.guiItems[i].remove(Keys.ITEM_ENCHANTMENTS);
                } else {
                    this.guiItems[i] = RedProtect.get().getVersionHelper().offerEnchantment(this.guiItems[i]);
                }
                this.guiItems[i].offer(Keys.HIDE_ENCHANTMENTS, true);
                this.guiItems[i].offer(Keys.HIDE_ATTRIBUTES, true);
            }
        } catch (Exception ex) {
            this.player.sendMessage(Text.of(Color.RED, "Seems RedProtect have a wrong Item Gui or a problem on guiconfig for flag " + flag));
        }
    }

    this.inv = RedProtect.get().getVersionHelper().newInventory(size, name);

    for (int slotc = 0; slotc < this.size; slotc++) {
        if (this.guiItems[slotc] == null) {
            this.guiItems[slotc] = RedProtect.get().config.getGuiSeparator();
        }
        int line = 0;
        int slot = slotc;
        if (slotc > 8) {
            line = slotc / 9;
            slot = slotc - (line * 9);
        }
        RedProtect.get().getVersionHelper().query(inv, slot, line).set(this.guiItems[slotc]);
    }
}