Search in sources :

Example 1 with JSONObject

use of io.github.thebusybiscuit.slimefun4.libraries.unirest.json.JSONObject in project FluffyMachines by NCBPFluffyBear.

the class CargoManipulator method pasteNode.

/**
 * Pastes stored node contents
 * Action: Left Click
 */
private void pasteNode(Block child, Player p, SlimefunItemStack nodeType) {
    Pair<JSONObject, ItemStack[]> nodeSettings = storedFilters.getOrDefault(p, null);
    // No data saved yet
    if (nodeSettings == null) {
        Utils.send(p, "&cYou have not copied a cargo node yet.");
        return;
    }
    // Get saved data
    JSONObject jsonData = nodeSettings.getFirstValue();
    SlimefunItemStack savedNodeType = (SlimefunItemStack) SlimefunItem.getById((String) jsonData.get("id")).getItem();
    if (savedNodeType != nodeType) {
        Utils.send(p, "&cYou copied a " + savedNodeType.getDisplayName() + " &cbut you are trying to modify a " + nodeType.getDisplayName() + "&c!");
        return;
    }
    // Set the data
    BlockStorage.setBlockInfo(child, jsonData.toString(), false);
    if (nodeType != SlimefunItems.CARGO_OUTPUT_NODE) {
        // Set the filter
        BlockMenu nodeMenu = BlockStorage.getInventory(child);
        ItemStack[] filterItems = nodeSettings.getSecondValue();
        Inventory playerInventory = p.getInventory();
        for (int i = 0; i < 9; i++) {
            // Check if item already exists in slot
            if (SlimefunUtils.isItemSimilar(filterItems[i], nodeMenu.getItemInSlot(CARGO_SLOTS[i]), true, false)) {
                continue;
            }
            // Drop item in filter slot
            clearFilterSlot(nodeMenu, CARGO_SLOTS[i], p);
            // No need to insert new items in
            if (filterItems[i] == null) {
                continue;
            }
            // Check if item not in inventory
            if (!SlimefunUtils.containsSimilarItem(playerInventory, filterItems[i], true)) {
                Utils.send(p, "&cYou do not have " + Utils.getViewableName(filterItems[i]) + "&c. Skipping this item.");
                continue;
            }
            // Consume item in player inventory
            for (ItemStack playerItem : playerInventory) {
                if (SlimefunUtils.isItemSimilar(playerItem, filterItems[i], false, false)) {
                    playerItem.setAmount(playerItem.getAmount() - 1);
                    // Insert item into node menu
                    nodeMenu.replaceExistingItem(CARGO_SLOTS[i], new CustomItemStack(playerItem, 1));
                    break;
                }
            }
        }
    }
    // Force menu update
    BlockStorage.getStorage(child.getWorld()).reloadInventory(child.getLocation());
    Utils.send(p, "&aYour " + savedNodeType.getDisplayName() + " &ahas been pasted.");
}
Also used : BlockMenu(me.mrCookieSlime.Slimefun.api.inventory.BlockMenu) JSONObject(io.github.thebusybiscuit.slimefun4.libraries.unirest.json.JSONObject) CustomItemStack(io.github.thebusybiscuit.slimefun4.libraries.dough.items.CustomItemStack) CustomItemStack(io.github.thebusybiscuit.slimefun4.libraries.dough.items.CustomItemStack) ItemStack(org.bukkit.inventory.ItemStack) SlimefunItemStack(io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack) Inventory(org.bukkit.inventory.Inventory) SlimefunItemStack(io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack)

Example 2 with JSONObject

use of io.github.thebusybiscuit.slimefun4.libraries.unirest.json.JSONObject in project Slimefun4 by Slimefun.

the class TagParser method parse.

/**
 * This will parse the given JSON {@link String} and run the provided callback with {@link Set Sets} of
 * matched {@link Material Materials} and {@link Tag Tags}.
 *
 * @param json
 *            The JSON {@link String} to parse
 * @param callback
 *            A callback to run after successfully parsing the input
 *
 * @throws TagMisconfigurationException
 *             This is thrown whenever the given input is malformed or no adequate
 *             {@link Material} or {@link Tag} could be found
 */
public void parse(@Nonnull String json, @Nonnull BiConsumer<Set<Material>, Set<Tag<Material>>> callback) throws TagMisconfigurationException {
    Validate.notNull(json, "Cannot parse a null String");
    try {
        Set<Material> materials = EnumSet.noneOf(Material.class);
        Set<Tag<Material>> tags = new HashSet<>();
        JsonObject root = JsonUtils.parseString(json).getAsJsonObject();
        JsonElement child = root.get("values");
        if (child instanceof JsonArray) {
            JsonArray values = child.getAsJsonArray();
            for (JsonElement element : values) {
                if (element instanceof JsonPrimitive && ((JsonPrimitive) element).isString()) {
                    // Strings will be parsed directly
                    parsePrimitiveValue(element.getAsString(), materials, tags, true);
                } else if (element instanceof JsonObject) {
                    /*
                         * JSONObjects can have a "required" property which can
                         * make it optional to resolve the underlying value
                         */
                    parseComplexValue(element.getAsJsonObject(), materials, tags);
                } else {
                    throw new TagMisconfigurationException(key, "Unexpected value format: " + element.getClass().getSimpleName() + " - " + element.toString());
                }
            }
            // Run the callback with the filled-in materials and tags
            callback.accept(materials, tags);
        } else {
            // The JSON seems to be empty yet valid
            throw new TagMisconfigurationException(key, "No values array specified");
        }
    } catch (IllegalStateException | JsonParseException x) {
        throw new TagMisconfigurationException(key, x);
    }
}
Also used : TagMisconfigurationException(io.github.thebusybiscuit.slimefun4.api.exceptions.TagMisconfigurationException) JsonPrimitive(com.google.gson.JsonPrimitive) JsonObject(com.google.gson.JsonObject) Material(org.bukkit.Material) JsonParseException(com.google.gson.JsonParseException) JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement) Tag(org.bukkit.Tag) HashSet(java.util.HashSet)

Example 3 with JSONObject

use of io.github.thebusybiscuit.slimefun4.libraries.unirest.json.JSONObject in project FluffyMachines by NCBPFluffyBear.

the class CargoManipulator method copyNode.

/**
 * Copy's a node's data into the manipulator. Cargo inventories stored in map.
 * Action: Right Click Block
 */
private void copyNode(Block parent, Player p, SlimefunItemStack nodeType) {
    // Copy BlockStorage data
    JSONObject nodeData = new JSONObject(BlockStorage.getBlockInfoAsJson(parent));
    ItemStack[] filterItems = new ItemStack[9];
    if (nodeType != SlimefunItems.CARGO_OUTPUT_NODE) {
        // No inventory
        // Copy inventory into map
        BlockMenu parentInventory = BlockStorage.getInventory(parent);
        for (int i = 0; i < 9; i++) {
            // Iterate through all slots in cargo filter
            ItemStack menuItem = parentInventory.getItemInSlot(CARGO_SLOTS[i]);
            if (menuItem != null) {
                filterItems[i] = new CustomItemStack(menuItem, 1);
            } else {
                filterItems[i] = null;
            }
        }
    }
    // Save cargo slots into map
    storedFilters.put(p, new Pair<>(nodeData, filterItems));
    Utils.send(p, "&aYour " + SlimefunItem.getById((String) nodeData.get("id")).getItemName() + " &ahas been copied.");
}
Also used : BlockMenu(me.mrCookieSlime.Slimefun.api.inventory.BlockMenu) JSONObject(io.github.thebusybiscuit.slimefun4.libraries.unirest.json.JSONObject) CustomItemStack(io.github.thebusybiscuit.slimefun4.libraries.dough.items.CustomItemStack) CustomItemStack(io.github.thebusybiscuit.slimefun4.libraries.dough.items.CustomItemStack) ItemStack(org.bukkit.inventory.ItemStack) SlimefunItemStack(io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack)

Example 4 with JSONObject

use of io.github.thebusybiscuit.slimefun4.libraries.unirest.json.JSONObject in project Slimefun4 by Slimefun.

the class PostSetup method setupWiki.

public static void setupWiki() {
    Slimefun.logger().log(Level.INFO, "Loading Wiki pages...");
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(Slimefun.class.getResourceAsStream("/wiki.json"), StandardCharsets.UTF_8))) {
        JsonElement element = JsonUtils.parseString(reader.lines().collect(Collectors.joining("")));
        JsonObject json = element.getAsJsonObject();
        for (Map.Entry<String, JsonElement> entry : json.entrySet()) {
            SlimefunItem item = SlimefunItem.getById(entry.getKey());
            if (item != null) {
                item.addOfficialWikipage(entry.getValue().getAsString());
            }
        }
    } catch (IOException e) {
        Slimefun.logger().log(Level.SEVERE, "Failed to load wiki.json file", e);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) JsonElement(com.google.gson.JsonElement) BufferedReader(java.io.BufferedReader) JsonObject(com.google.gson.JsonObject) SlimefunItem(io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem) IOException(java.io.IOException) Map(java.util.Map)

Example 5 with JSONObject

use of io.github.thebusybiscuit.slimefun4.libraries.unirest.json.JSONObject in project Slimefun4 by Slimefun.

the class TagParser method parseComplexValue.

@ParametersAreNonnullByDefault
private void parseComplexValue(JsonObject entry, Set<Material> materials, Set<Tag<Material>> tags) throws TagMisconfigurationException {
    JsonElement id = entry.get("id");
    JsonElement required = entry.get("required");
    // Check if the entry contains elements of the correct type
    if (id instanceof JsonPrimitive && ((JsonPrimitive) id).isString() && required instanceof JsonPrimitive && ((JsonPrimitive) required).isBoolean()) {
        boolean isRequired = required.getAsBoolean();
        /*
             * If the Tag is required, an exception may be thrown.
             * Otherwise it will just ignore the value
             */
        parsePrimitiveValue(id.getAsString(), materials, tags, isRequired);
    } else {
        throw new TagMisconfigurationException(key, "Found a JSON Object value without an id!");
    }
}
Also used : TagMisconfigurationException(io.github.thebusybiscuit.slimefun4.api.exceptions.TagMisconfigurationException) JsonPrimitive(com.google.gson.JsonPrimitive) JsonElement(com.google.gson.JsonElement) ParametersAreNonnullByDefault(javax.annotation.ParametersAreNonnullByDefault)

Aggregations

JsonElement (com.google.gson.JsonElement)3 JsonObject (com.google.gson.JsonObject)2 JsonPrimitive (com.google.gson.JsonPrimitive)2 TagMisconfigurationException (io.github.thebusybiscuit.slimefun4.api.exceptions.TagMisconfigurationException)2 SlimefunItemStack (io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack)2 CustomItemStack (io.github.thebusybiscuit.slimefun4.libraries.dough.items.CustomItemStack)2 JSONObject (io.github.thebusybiscuit.slimefun4.libraries.unirest.json.JSONObject)2 BlockMenu (me.mrCookieSlime.Slimefun.api.inventory.BlockMenu)2 ItemStack (org.bukkit.inventory.ItemStack)2 JsonArray (com.google.gson.JsonArray)1 JsonParseException (com.google.gson.JsonParseException)1 SlimefunItem (io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 ParametersAreNonnullByDefault (javax.annotation.ParametersAreNonnullByDefault)1 Material (org.bukkit.Material)1 Tag (org.bukkit.Tag)1