![JAR search and dependency download from the Maven repository](/logo.png)
mmb.engine.block.BlockLoader Maven / Gradle / Ivy
/**
*
*/
package mmb.engine.block;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import mmb.Nil;
import mmb.engine.debug.Debugger;
import mmb.engine.item.Items;
import mmb.engine.worlds.world.World;
/**
* Loads blocks from JSON.
* To save a block, just call save() on the block
* @author oskar
*/
public class BlockLoader{
private BlockLoader() {}
private static final Debugger debug = new Debugger("BLOCK LOADER");
/**
* Loads a block
* @param data block data node
* @param x X coordinate of the block
* @param y Y coordinate of the block
* @param map world which holds the block
* @return a loaded block
*/
public static BlockEntry load(@Nil JsonNode data, int x, int y, World map) {
if(data == null) {
debug.printl("The block data is null");
return null;
}
if(data.isNull()) {
debug.printl("The block data is null node");
return null;
}
if(data.isMissingNode()) {
debug.printl("The block data is a virtual node");
return null;
}
if(data.isObject()) return doLoadEnhanced((ObjectNode) data, x, y); //with properties
String text = data.asText(); //without properties
if(text != null) return doLoadBasic(text);
debug.printl("Unknown block data: "+data);
return null;
}
private static BlockEntry doLoadBasic(String text) {
BlockType type = Items.getExpectType(text, BlockType.class);
if(type == null) {
debug.printl("Unknown block type: \""+text+"\"");
return Blocks.grass;
}
return type.createBlock();
}
private static BlockEntry doLoadEnhanced(ObjectNode on, int x, int y) {
//find block type
String blockName = on.get("blocktype").asText();
BlockType typ = Items.getExpectType(blockName, BlockType.class);
if(typ == null) {
debug.printl("Block type "+blockName+" was not found");
return null;
}
BlockEntry block = typ.createBlock();
try {
block.load(on);
}catch(Exception e) {
debug.stacktraceError(e, "Failed to load a block "+blockName+" at ["+x+","+y+"]");
}
return block;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy