Search in sources :

Example 1 with BlockRenderOptions

use of com.bergerkiller.bukkit.common.wrappers.BlockRenderOptions in project BKCommonLib by bergerhealer.

the class ConditionalDeserializer method deserialize.

@Override
public BlockModelState.Condition deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
    BlockModelState.Condition result = new BlockModelState.Condition();
    result.mode = BlockModelState.Condition.Mode.AND;
    result.conditions = new ArrayList<BlockModelState.Condition>(1);
    if (jsonElement.isJsonPrimitive()) {
        // Options stored in a single String token
        Map<String, String> options = new BlockRenderOptions(BlockData.AIR, jsonElement.getAsString());
        for (Map.Entry<String, String> option : options.entrySet()) {
            result.conditions.add(createSelfCondition(option.getKey(), option.getValue()));
        }
    } else {
        // Handle operator types in the condition structure
        JsonObject obj = jsonElement.getAsJsonObject();
        for (Map.Entry<String, JsonElement> entry : obj.entrySet()) {
            // Start a new sub-condition, defaulting to mode SELF
            BlockModelState.Condition subCondition = new BlockModelState.Condition();
            subCondition.mode = BlockModelState.Condition.Mode.SELF;
            for (BlockModelState.Condition.Mode mode : BlockModelState.Condition.Mode.values()) {
                if (entry.getKey().equals(mode.name())) {
                    subCondition.mode = mode;
                    break;
                }
            }
            if (subCondition.mode == BlockModelState.Condition.Mode.SELF) {
                // Self: store key:value pair
                subCondition = createSelfCondition(entry.getKey(), entry.getValue().getAsString());
            } else {
                // Create a sub-tree with this operator mode
                if (entry.getValue().isJsonArray()) {
                    // Array of Object conditions
                    JsonArray condArr = entry.getValue().getAsJsonArray();
                    subCondition.conditions = new ArrayList<BlockModelState.Condition>(condArr.size());
                    for (JsonElement condElem : condArr) {
                        subCondition.conditions.add(deserialize(condElem, type, jsonDeserializationContext));
                    }
                } else {
                    // Single Object condition
                    subCondition.conditions = Arrays.asList(deserialize(entry.getValue(), type, jsonDeserializationContext));
                }
            }
            result.conditions.add(subCondition);
        }
    }
    // Simplify if only one element
    if (result.conditions.size() == 1) {
        return result.conditions.get(0);
    } else {
        return result;
    }
}
Also used : BlockRenderOptions(com.bergerkiller.bukkit.common.wrappers.BlockRenderOptions) BlockModelState(com.bergerkiller.bukkit.common.map.util.BlockModelState) JsonObject(com.google.gson.JsonObject) JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement) Map(java.util.Map)

Example 2 with BlockRenderOptions

use of com.bergerkiller.bukkit.common.wrappers.BlockRenderOptions in project BKCommonLib by bergerhealer.

the class Pseudo3DImagePanel method paintComponent.

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    MapTexture image = MapTexture.createEmpty(MapModelRenderTest.RES_WIDTH, MapModelRenderTest.RES_HEIGHT);
    image.draw(background, 0, 0);
    image.setBlendMode(MapBlendMode.OVERLAY);
    // new Vector3(-1.0, 1.0, -1.0));
    image.setLightOptions(0.0f, 1.0f, new Vector3(-1, 1, -1));
    System.out.println("{" + p3.x + ", " + p2.x + ", " + p3.y + "}");
    // Draws a 3D quad
    float scale = 16.0f;
    float yaw = (float) (p1.x - 128);
    float pitch = (float) (p1.y - 128);
    System.out.println("Yaw=" + yaw + " Pitch=" + pitch);
    BlockRenderOptions opt = BlockData.fromMaterialData(Material.RAILS, 5).getDefaultRenderOptions();
    opt.put("west", "side");
    System.out.println(opt);
    ItemStack item = ItemUtil.createItem(Material.COOKED_FISH, 0, 1);
    ItemUtil.getMetaTag(item, true).putValue("Unbreakable", true);
    // textures.getBlockModel(opt);
    Model model = textures.getItemModel(item);
    Matrix4x4 transform = new Matrix4x4();
    // image.draw(textures.getItemTexture(item, 32, 32), 0, 0);
    transform.translate(p0.x, 0.0f, p0.y);
    transform.scale(scale);
    transform.rotateX(pitch);
    transform.rotateY(yaw);
    transform.translate(-8, -8, -8);
    // image.drawModel(textures.getBlockModel(Material.QUARTZ_BLOCK), transform);
    transform.translate(20, 0, 0);
    image.drawModel(model, transform);
    g.drawImage(image.toJavaImage(), 0, 0, null);
    int r = 8;
    g.setColor(Color.BLUE);
    g.fillOval((int) p0.x - r, (int) p0.y - r, r + r, r + r);
    g.fillOval((int) p1.x - r, (int) p1.y - r, r + r, r + r);
    g.fillOval((int) p2.x - r, (int) p2.y - r, r + r, r + r);
    g.fillOval((int) p3.x - r, (int) p3.y - r, r + r, r + r);
}
Also used : MapTexture(com.bergerkiller.bukkit.common.map.MapTexture) BlockRenderOptions(com.bergerkiller.bukkit.common.wrappers.BlockRenderOptions) Model(com.bergerkiller.bukkit.common.map.util.Model) Vector3(com.bergerkiller.bukkit.common.math.Vector3) ItemStack(org.bukkit.inventory.ItemStack) Matrix4x4(com.bergerkiller.bukkit.common.math.Matrix4x4)

Example 3 with BlockRenderOptions

use of com.bergerkiller.bukkit.common.wrappers.BlockRenderOptions in project BKCommonLib by bergerhealer.

the class ModelInfoLookup method lookupItemRenderOptions.

public static ItemRenderOptions lookupItemRenderOptions(ItemStack item) {
    // Blocks
    Material type = (item == null) ? Material.AIR : item.getType();
    if (item == null || type.isBlock()) {
        BlockRenderOptions blockOpt = BlockData.fromItemStack(item).getDefaultRenderOptions();
        return new ItemRenderOptions(item, blockOpt);
    }
    // Some items, like leather boots, require additional render options passed
    ItemRenderOptions options = new ItemRenderOptions(item, "");
    if (type == Material.LEATHER_BOOTS || type == Material.LEATHER_CHESTPLATE || type == Material.LEATHER_HELMET || type == Material.LEATHER_LEGGINGS) {
        // Check 'display.color' metadata tag for custom colors
        // default brown
        int color = 5190175;
        CommonTagCompound nbt = ItemUtil.getMetaTag(item, false);
        if (nbt != null) {
            CommonTagCompound display = nbt.getValue("display", CommonTagCompound.class);
            if (display != null) {
                color = display.getValue("color", color);
            }
        }
        // Convert color to hexadecimal and store it as an option
        options.put("layer0tint", String.format("#%06x", color));
    }
    // Similarly, the liquid inside potion bottles have a color set
    if (type == Material.POTION || type.name().equals("LINGERING_POTION") || type.name().equals("SPLASH_POTION")) {
        int color = getPotionColor(item.getDurability());
        // Check 'CustomPotionColor' metadata tag for custom colors
        CommonTagCompound nbt = ItemUtil.getMetaTag(item, false);
        if (nbt != null) {
            color = nbt.getValue("CustomPotionColor", color);
        }
        // Convert color to hexadecimal and store it as an option
        options.put("layer0tint", String.format("#%06x", color));
    }
    // damage and damaged properties of weapons, armor and tools
    if (ItemUtil.hasDurability(item)) {
        boolean unbreakable = false;
        CommonTagCompound nbt = ItemUtil.getMetaTag(item, false);
        if (nbt != null) {
            unbreakable = nbt.getValue("Unbreakable", unbreakable);
        }
        options.put("damaged", unbreakable ? "0" : "1");
        options.put("damage", Double.toString((double) item.getDurability() / (double) (ItemUtil.getMaxDurability(item) + 1)));
    }
    return options;
}
Also used : BlockRenderOptions(com.bergerkiller.bukkit.common.wrappers.BlockRenderOptions) ItemRenderOptions(com.bergerkiller.bukkit.common.wrappers.ItemRenderOptions) CommonTagCompound(com.bergerkiller.bukkit.common.nbt.CommonTagCompound) Material(org.bukkit.Material)

Aggregations

BlockRenderOptions (com.bergerkiller.bukkit.common.wrappers.BlockRenderOptions)3 MapTexture (com.bergerkiller.bukkit.common.map.MapTexture)1 BlockModelState (com.bergerkiller.bukkit.common.map.util.BlockModelState)1 Model (com.bergerkiller.bukkit.common.map.util.Model)1 Matrix4x4 (com.bergerkiller.bukkit.common.math.Matrix4x4)1 Vector3 (com.bergerkiller.bukkit.common.math.Vector3)1 CommonTagCompound (com.bergerkiller.bukkit.common.nbt.CommonTagCompound)1 ItemRenderOptions (com.bergerkiller.bukkit.common.wrappers.ItemRenderOptions)1 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 Map (java.util.Map)1 Material (org.bukkit.Material)1 ItemStack (org.bukkit.inventory.ItemStack)1