Search in sources :

Example 1 with InventoryScriptContainer

use of com.denizenscript.denizen.scripts.containers.core.InventoryScriptContainer in project Denizen-For-Bukkit by DenizenScript.

the class InventoryTag method internalGetInventoryFor.

public static InventoryTag internalGetInventoryFor(TagContext context, List<String> properties) {
    String typeName = properties.get(0);
    String holder = null;
    int size = -1;
    for (String property : properties) {
        if (property.startsWith("holder=")) {
            holder = ObjectFetcher.unescapeProperty(property.substring("holder=".length()));
        } else if (property.startsWith("script_name=")) {
            holder = ObjectFetcher.unescapeProperty(property.substring("script_name=".length()));
            typeName = "script";
        } else if (property.startsWith("uniquifier=")) {
            String idText = property.substring("uniquifier=".length());
            if (!ArgumentHelper.matchesInteger(idText)) {
                return null;
            }
            long id = Long.parseLong(idText);
            InventoryTag fixedResult = InventoryTrackerSystem.idTrackedInventories.get(id);
            if (fixedResult != null) {
                trackTemporaryInventory(fixedResult);
                return fixedResult;
            }
        } else if (property.startsWith("size=")) {
            String sizeText = property.substring("size=".length());
            if (!ArgumentHelper.matchesInteger(sizeText)) {
                return null;
            }
            size = Integer.parseInt(sizeText);
        }
    }
    if (holder != null) {
        switch(typeName) {
            case "player":
            case "enderchest":
            case "workbench":
            case "crafting":
                PlayerTag player = PlayerTag.valueOf(holder, context);
                if (player == null) {
                    if (context == null || context.showErrors()) {
                        Debug.echoError("Invalid inventory player '" + holder + "'");
                    }
                    return null;
                }
                switch(typeName) {
                    case "player":
                        return player.getInventory();
                    case "enderchest":
                        return player.getEnderChest();
                    case "workbench":
                        return player.getWorkbench();
                    case "crafting":
                        Inventory opened = player.getPlayerEntity().getOpenInventory().getTopInventory();
                        if (opened instanceof CraftingInventory) {
                            return new InventoryTag(opened, player.getPlayerEntity());
                        } else {
                            return player.getInventory();
                        }
                }
                break;
            case "npc":
                NPCTag npc = NPCTag.valueOf(holder, context);
                if (npc == null) {
                    if (context == null || context.showErrors()) {
                        Debug.echoError("Invalid inventory npc '" + holder + "'");
                    }
                    return null;
                }
                return npc.getDenizenInventory();
            case "entity":
                EntityTag entity = EntityTag.valueOf(holder, context);
                if (entity == null) {
                    if (context == null || context.showErrors()) {
                        Debug.echoError("Invalid inventory entity '" + holder + "'");
                    }
                    return null;
                }
                return entity.getInventory();
            case "location":
                LocationTag location = LocationTag.valueOf(holder, context);
                if (location == null) {
                    if (context == null || context.showErrors()) {
                        Debug.echoError("Invalid inventory location '" + holder + "'");
                    }
                    return null;
                }
                return location.getInventory();
        }
    }
    InventoryTag result = null;
    if (typeName.equals("generic")) {
        if (holder != null && !new ElementTag(holder).matchesEnum(InventoryType.class)) {
            if (context == null || context.showErrors()) {
                Debug.echoError("Unknown inventory type '" + holder + "'");
            }
            return null;
        }
        InventoryType type = holder == null ? InventoryType.CHEST : InventoryType.valueOf(holder.toUpperCase());
        if (size == -1 || type != InventoryType.CHEST) {
            result = new InventoryTag(type);
        } else {
            result = new InventoryTag(size);
        }
    } else if (typeName.equals("script") && holder != null) {
        ScriptTag script = ScriptTag.valueOf(holder, context);
        if (script == null || !(script.getContainer() instanceof InventoryScriptContainer)) {
            if (context == null || context.showErrors()) {
                Debug.echoError("Unknown inventory script '" + holder + "'");
            }
            return null;
        }
        result = ((InventoryScriptContainer) script.getContainer()).getInventoryFrom(context);
        if (size != -1) {
            result.setSize(size);
        }
    }
    if (result == null && holder != null) {
        ScriptTag script = ScriptTag.valueOf(holder, context);
        if (script != null && (script.getContainer() instanceof InventoryScriptContainer)) {
            result = ((InventoryScriptContainer) script.getContainer()).getInventoryFrom(context);
        }
    }
    if (result == null && new ElementTag(typeName).matchesEnum(InventoryType.class)) {
        InventoryType type = InventoryType.valueOf(typeName.toUpperCase());
        if (size == -1 || type != InventoryType.CHEST) {
            result = new InventoryTag(type);
        } else {
            result = new InventoryTag(size);
        }
    }
    if (result == null) {
        if (context == null || context.showErrors()) {
            Debug.echoError("Unknown inventory idType '" + typeName + "'");
        }
        return null;
    }
    internalApplyPropertySet(result, context, properties);
    return result;
}
Also used : InventoryScriptContainer(com.denizenscript.denizen.scripts.containers.core.InventoryScriptContainer) InventoryType(org.bukkit.event.inventory.InventoryType) ScriptTag(com.denizenscript.denizencore.objects.core.ScriptTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag)

Aggregations

InventoryScriptContainer (com.denizenscript.denizen.scripts.containers.core.InventoryScriptContainer)1 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)1 ScriptTag (com.denizenscript.denizencore.objects.core.ScriptTag)1 InventoryType (org.bukkit.event.inventory.InventoryType)1