Search in sources :

Example 6 with Model

use of com.bergerkiller.bukkit.common.map.util.Model in project BKCommonLib by bergerhealer.

the class MapResourcePack method getItemTexture.

/**
 * Renders the item gui slot texture of an item
 *
 * @param item to render
 * @param width of the produced icon image
 * @param height of the produced icon image
 * @return rendered item slot image
 */
public MapTexture getItemTexture(ItemStack item, int width, int height) {
    Model model = this.getItemModel(item);
    if (model == null || model.placeholder) {
        return createPlaceholderTexture(width, height);
    }
    MapTexture texture = MapTexture.createEmpty(width, height);
    Matrix4x4 transform = new Matrix4x4();
    if (width != 16 || height != 16) {
        transform.scale((double) width / 16.0, 1.0, (double) height / 16.0);
    }
    Model.Display display = model.display.get("gui");
    if (display != null) {
        display.apply(transform);
        texture.setLightOptions(0.0f, 1.0f, new Vector3(-1, 1, -1));
    } else {
    // System.out.println("GUI DISPLAY ELEMENT NOT FOUND");
    }
    texture.drawModel(model, transform);
    return texture;
}
Also used : Model(com.bergerkiller.bukkit.common.map.util.Model) GeneratedModel(com.bergerkiller.bukkit.common.internal.resources.builtin.GeneratedModel) Vector3(com.bergerkiller.bukkit.common.math.Vector3) Matrix4x4(com.bergerkiller.bukkit.common.math.Matrix4x4)

Example 7 with Model

use of com.bergerkiller.bukkit.common.map.util.Model in project BKCommonLib by bergerhealer.

the class MapResourcePack method getModel.

/**
 * Loads a JSON-syntax model from this resource pack.
 *
 * @param path to the model, e.g. "block/stone"
 * @return the model, a placeholder cube model if it could not be found
 */
public Model getModel(String path) {
    Model model = modelCache.get(path);
    if (model != null) {
        return model;
    }
    model = this.loadModel(path);
    if (model == null) {
        // failed to load or find
        model = this.createPlaceholderModel(BlockData.AIR.getDefaultRenderOptions());
        model.name = path;
    }
    modelCache.put(path, model);
    return model;
}
Also used : Model(com.bergerkiller.bukkit.common.map.util.Model) GeneratedModel(com.bergerkiller.bukkit.common.internal.resources.builtin.GeneratedModel)

Example 8 with Model

use of com.bergerkiller.bukkit.common.map.util.Model in project BKCommonLib by bergerhealer.

the class MapResourcePack method createPlaceholderModel.

/**
 * Creates a placeholder model. Used when models can not be loaded.
 *
 * @return placeholder model
 */
protected final Model createPlaceholderModel(RenderOptions renderOptions) {
    Model model = new Model();
    Model.Element element = new Model.Element();
    for (BlockFace face : FaceUtil.BLOCK_SIDES) {
        element.faces.put(face, createPlaceholderFace());
    }
    element.buildQuads();
    model.placeholder = true;
    model.elements.add(element);
    model.name = renderOptions.lookupModelName();
    return model;
}
Also used : BlockFace(org.bukkit.block.BlockFace) Model(com.bergerkiller.bukkit.common.map.util.Model) GeneratedModel(com.bergerkiller.bukkit.common.internal.resources.builtin.GeneratedModel)

Example 9 with Model

use of com.bergerkiller.bukkit.common.map.util.Model in project BKCommonLib by bergerhealer.

the class MapResourcePack method getItemModel.

/**
 * Loads the model to be displayed for items displayed in the world, held in a player's hand
 * or shown in a GUI item slot.
 *
 * @param item to get the model for
 * @return item model for the item
 */
public Model getItemModel(ItemStack item) {
    ItemRenderOptions options = ModelInfoLookup.lookupItemRenderOptions(item);
    String itemModelName = options.lookupModelName();
    Model m = this.loadModel("item/" + itemModelName, options);
    if (m != null) {
        m.buildBlock(options);
        m.buildQuads();
    }
    if (m == null) {
        m = this.createPlaceholderModel(options);
    }
    return m;
}
Also used : ItemRenderOptions(com.bergerkiller.bukkit.common.wrappers.ItemRenderOptions) Model(com.bergerkiller.bukkit.common.map.util.Model) GeneratedModel(com.bergerkiller.bukkit.common.internal.resources.builtin.GeneratedModel)

Example 10 with Model

use of com.bergerkiller.bukkit.common.map.util.Model in project BKCommonLib by bergerhealer.

the class MapResourcePack method loadModel.

/**
 * Loads a model, always fetching it from the resource pack instead of the cache
 *
 * @param path to find the model at
 * @param options to apply when building the model
 * @return the model, or <i>null</i> if not found
 */
protected final Model loadModel(String path, RenderOptions options) {
    // Builtin models
    if (path.equals("builtin/generated")) {
        return new GeneratedModel();
    }
    Model model = openGsonObject(Model.class, ResourceType.MODELS, path);
    if (model == null) {
        Logging.LOGGER_MAPDISPLAY.once(Level.WARNING, "Failed to load model " + path);
        return null;
    }
    // Handle overrides first
    if (model.overrides != null && !model.overrides.isEmpty()) {
        for (Model.ModelOverride override : model.overrides) {
            if (override.matches(options) && !override.model.equals(path)) {
                // System.out.println("MATCH " + override.model + "  " + options);
                return this.loadModel(override.model, options);
            }
        }
    }
    // Insert the parent model as required
    if (model.getParentName() != null) {
        Model parentModel = this.loadModel(model.getParentName(), options);
        if (parentModel == null || parentModel.placeholder) {
            Logging.LOGGER_MAPDISPLAY.once(Level.WARNING, "Parent of model " + path + " not found: " + model.getParentName());
            return null;
        }
        model.loadParent(parentModel);
    }
    // Make all texture paths absolute
    model.build(this, options);
    return model;
}
Also used : GeneratedModel(com.bergerkiller.bukkit.common.internal.resources.builtin.GeneratedModel) Model(com.bergerkiller.bukkit.common.map.util.Model) GeneratedModel(com.bergerkiller.bukkit.common.internal.resources.builtin.GeneratedModel)

Aggregations

Model (com.bergerkiller.bukkit.common.map.util.Model)11 GeneratedModel (com.bergerkiller.bukkit.common.internal.resources.builtin.GeneratedModel)7 MapResourcePack (com.bergerkiller.bukkit.common.map.MapResourcePack)2 MapTexture (com.bergerkiller.bukkit.common.map.MapTexture)2 Matrix4x4 (com.bergerkiller.bukkit.common.math.Matrix4x4)2 Vector3 (com.bergerkiller.bukkit.common.math.Vector3)2 HashSet (java.util.HashSet)2 BlockFace (org.bukkit.block.BlockFace)2 ItemStack (org.bukkit.inventory.ItemStack)2 Ignore (org.junit.Ignore)2 Test (org.junit.Test)2 BlockRenderProvider (com.bergerkiller.bukkit.common.internal.blocks.BlockRenderProvider)1 BlockModelState (com.bergerkiller.bukkit.common.map.util.BlockModelState)1 Face (com.bergerkiller.bukkit.common.map.util.Model.Element.Face)1 BlockData (com.bergerkiller.bukkit.common.wrappers.BlockData)1 BlockRenderOptions (com.bergerkiller.bukkit.common.wrappers.BlockRenderOptions)1 ItemRenderOptions (com.bergerkiller.bukkit.common.wrappers.ItemRenderOptions)1 Material (org.bukkit.Material)1