new BlockModelLoader

This commit is contained in:
Lukas 2020-07-25 16:26:08 +02:00
parent c102816915
commit e85f7d93a1
9 changed files with 75 additions and 91 deletions

View File

@ -33,8 +33,7 @@ import org.json.JSONObject;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.*;
import java.util.UUID;
public class Minosoft { public class Minosoft {
static Configuration config; static Configuration config;
@ -82,9 +81,10 @@ public class Minosoft {
} else { } else {
Log.mojang("Could not refresh session, you will not be able to join premium servers!"); Log.mojang("Could not refresh session, you will not be able to join premium servers!");
} }
c.setPlayer(new Player(account)); Connection connection = new Connection(config.getString("debug.host"), config.getInteger("debug.port"));
c.connect(); connection.setPlayer(new Player(account));
MainWindow.start(c); connection.connect();
MainWindow.start(connection);
} }
/** /**

View File

@ -13,20 +13,22 @@
package de.bixilon.minosoft.game.datatypes.blocks; package de.bixilon.minosoft.game.datatypes.blocks;
import java.util.ArrayList;
public class Block { public class Block {
final String mod; final String mod;
final String identifier; final String identifier;
final BlockRotation rotation; final BlockRotation rotation;
final BlockProperties[] properties; final ArrayList<BlockProperties> properties;
public Block(String mod, String identifier, BlockProperties[] properties, BlockRotation rotation) { public Block(String mod, String identifier, ArrayList<BlockProperties> properties, BlockRotation rotation) {
this.mod = mod; this.mod = mod;
this.identifier = identifier; this.identifier = identifier;
this.properties = properties; this.properties = properties;
this.rotation = rotation; this.rotation = rotation;
} }
public Block(String mod, String identifier, BlockProperties[] properties) { public Block(String mod, String identifier, ArrayList<BlockProperties> properties) {
this.mod = mod; this.mod = mod;
this.identifier = identifier; this.identifier = identifier;
this.properties = properties; this.properties = properties;
@ -36,14 +38,14 @@ public class Block {
public Block(String mod, String identifier, BlockRotation rotation) { public Block(String mod, String identifier, BlockRotation rotation) {
this.mod = mod; this.mod = mod;
this.identifier = identifier; this.identifier = identifier;
this.properties = new BlockProperties[0]; this.properties = new ArrayList<>();
this.rotation = rotation; this.rotation = rotation;
} }
public Block(String mod, String identifier) { public Block(String mod, String identifier) {
this.mod = mod; this.mod = mod;
this.identifier = identifier; this.identifier = identifier;
this.properties = new BlockProperties[0]; this.properties = new ArrayList<>();
this.rotation = BlockRotation.NONE; this.rotation = BlockRotation.NONE;
} }
@ -59,7 +61,7 @@ public class Block {
return rotation; return rotation;
} }
public BlockProperties[] getProperties() { public ArrayList<BlockProperties> getProperties() {
return properties; return properties;
} }
@ -71,7 +73,7 @@ public class Block {
out.append("rotation="); out.append("rotation=");
out.append(getRotation().name()); out.append(getRotation().name());
} }
if (properties.length > 0) { if (properties.size() > 0) {
if (out.length() > 0) { if (out.length() > 0) {
out.append(" ,"); out.append(" ,");
} else { } else {

View File

@ -590,4 +590,8 @@ public class Blocks {
} }
return blockId; return blockId;
} }
public static ArrayList<Block> getBlockList() {
return blockList;
}
} }

View File

@ -14,6 +14,7 @@
package de.bixilon.minosoft.game.datatypes.world; package de.bixilon.minosoft.game.datatypes.world;
import de.bixilon.minosoft.game.datatypes.blocks.Block; import de.bixilon.minosoft.game.datatypes.blocks.Block;
import de.bixilon.minosoft.game.datatypes.blocks.Blocks;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -34,7 +35,7 @@ public class Chunk {
} }
byte section = (byte) (y / 16); byte section = (byte) (y / 16);
if (nibbles.get(section) == null) { if (nibbles.get(section) == null) {
return Blocks.AIR; return Blocks.nullBlock;
} }
return nibbles.get(section).getBlock(x, y % 16, z); return nibbles.get(section).getBlock(x, y % 16, z);
} }

View File

@ -48,7 +48,7 @@ public class ChunkNibble {
blocks.put(location, block); blocks.put(location, block);
} }
public HashMap<ChunkNibbleLocation, Blocks> getBlocks() { public HashMap<ChunkNibbleLocation, Block> getBlocks() {
return blocks; return blocks;
} }
} }

View File

@ -59,7 +59,7 @@ public class PacketHandler {
public void handle(PacketStatusResponse pkg) { public void handle(PacketStatusResponse pkg) {
if (connection.getReason() == ConnectionReason.GET_VERSION) { if (connection.getReason() == ConnectionReason.GET_VERSION) {
// now we know the version, set it, if the config allows it // now we know the version, set it, if the config allows it
int version = Minosoft.getConfig().getInteger("debug.version"); int version = Minosoft.getConfig().getInteger("version");
if (version == -1) { if (version == -1) {
connection.setVersion(ProtocolVersion.byId(pkg.getResponse().getProtocolNumber())); connection.setVersion(ProtocolVersion.byId(pkg.getResponse().getProtocolNumber()));
} else { } else {

View File

@ -13,6 +13,7 @@
package de.bixilon.minosoft.render; package de.bixilon.minosoft.render;
import de.bixilon.minosoft.game.datatypes.blocks.Block;
import de.bixilon.minosoft.game.datatypes.blocks.Blocks; import de.bixilon.minosoft.game.datatypes.blocks.Blocks;
import de.bixilon.minosoft.game.datatypes.world.*; import de.bixilon.minosoft.game.datatypes.world.*;
import de.bixilon.minosoft.logging.Log; import de.bixilon.minosoft.logging.Log;
@ -56,7 +57,7 @@ public class WorldRenderer {
int xOffset = location.getX() * 16; int xOffset = location.getX() * 16;
int zOffset = location.getZ() * 16; int zOffset = location.getZ() * 16;
for (Map.Entry<Byte, ChunkNibble> set : chunk.getNibbles().entrySet()) { for (Map.Entry<Byte, ChunkNibble> set : chunk.getNibbles().entrySet()) {
for (Map.Entry<ChunkNibbleLocation, Blocks> blockEntry : set.getValue().getBlocks().entrySet()) { for (Map.Entry<ChunkNibbleLocation, Block> blockEntry : set.getValue().getBlocks().entrySet()) {
prepareBlock(new BlockPosition(blockEntry.getKey().getX() + xOffset, prepareBlock(new BlockPosition(blockEntry.getKey().getX() + xOffset,
(short) (blockEntry.getKey().getY() + set.getKey() * 16), (short) (blockEntry.getKey().getY() + set.getKey() * 16),
blockEntry.getKey().getZ() + zOffset), blockEntry.getKey().getZ() + zOffset),
@ -65,16 +66,16 @@ public class WorldRenderer {
} }
} }
public void prepareBlock(BlockPosition position, Blocks block) { public void prepareBlock(BlockPosition position, Block block) {
if (block == Blocks.AIR) if (block == Blocks.nullBlock)
return; return;
for (FaceOrientation orientation : FaceOrientation.values()) { for (FaceOrientation orientation : FaceOrientation.values()) {
BlockPosition neighbourPos = position.add(faceDir[orientation.getId()]); BlockPosition neighbourPos = position.add(faceDir[orientation.getId()]);
if (neighbourPos.getY() >= 0) { if (neighbourPos.getY() >= 0) {
Blocks neighbourBlock = MainWindow.getConnection().getPlayer().getWorld().getBlock(neighbourPos); Block neighbourBlock = MainWindow.getConnection().getPlayer().getWorld().getBlock(neighbourPos);
if (!(neighbourBlock == Blocks.AIR || neighbourBlock == null)) //!modelLoader.isFull(neighbourBlock)) if (!(neighbourBlock == Blocks.nullBlock || neighbourBlock == null)) //!modelLoader.isFull(neighbourBlock))
// if there is a block next to the current block, don't draw the face // if there is a block next to the current block, don't draw the face
continue; continue;
//TODO: fix buggy behavior, not always working correctly, probably a problem in the World or BlockPosition class //TODO: fix buggy behavior, not always working correctly, probably a problem in the World or BlockPosition class

View File

@ -14,52 +14,77 @@
package de.bixilon.minosoft.render.blockModels; package de.bixilon.minosoft.render.blockModels;
import de.bixilon.minosoft.Config; import de.bixilon.minosoft.Config;
import de.bixilon.minosoft.game.datatypes.blocks.Block;
import de.bixilon.minosoft.game.datatypes.blocks.BlockProperties;
import de.bixilon.minosoft.game.datatypes.blocks.Blocks; import de.bixilon.minosoft.game.datatypes.blocks.Blocks;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map;
import static de.bixilon.minosoft.render.blockModels.BlockName.getBlockByName;
public class BlockModelLoader { public class BlockModelLoader {
private final Map<Blocks, DrawDescription> drawDescriptionMap; final HashMap<String, DrawDescription> drawDescriptionMap;
public BlockModelLoader() { public BlockModelLoader() {
drawDescriptionMap = new HashMap<>(); drawDescriptionMap = new HashMap<>();
try { try {
loadModels(Config.homeDir + "assets/minecraft/models/block"); loadModels();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
private void loadModels(String path) throws IOException { private void loadModels() throws IOException {
File[] files = new File(path).listFiles(); for (Block block : Blocks.getBlockList()) {
String mod = block.getMod();
for (File file : files) { String identifier = block.getIdentifier();
String fileName = file.getName().substring(0, file.getName().lastIndexOf('.')); if (handleProperties(block)) {
String fileContent = new String(Files.readAllBytes(Paths.get(file.getPath()))); return;
JSONObject object = new JSONObject(fileContent); }
DrawDescription drawDescription = new DrawDescription(object); if (identifier.contains("pane")) {
drawDescriptionMap.put(getBlockByName(fileName), drawDescription); // TODO: handle glass panes
continue;
}
if (identifier.equals("large_fern")) {
loadModel(mod, identifier + "_bottom");
loadModel(mod, identifier + "_top");
continue;
}
if (drawDescriptionMap.containsKey((mod + ":" + identifier))) {
// a description for that block already exists, checking because Blocks.getBlockList()
// returns all blocks with all possible combinations
continue;
}
loadModel(mod, identifier);
} }
} }
public DrawDescription getDrawDescription(Blocks block) { private boolean handleProperties(Block block) {
if (!drawDescriptionMap.containsKey(block)) return !block.getProperties().contains(BlockProperties.NONE) && block.getProperties().size() != 0;
throw new IllegalArgumentException(String.format("no description for block %s found", block));
return drawDescriptionMap.get(block);
} }
public boolean isFull(Blocks block) { private void loadModel(String mod, String identifier) throws IOException {
if (block == Blocks.AIR || block == null) { String path = Config.homeDir + "assets/" + mod + "/models/block/" + identifier + ".json";
String fileContent = new String(Files.readAllBytes(Paths.get(path)));
JSONObject object = new JSONObject(fileContent);
DrawDescription description = new DrawDescription(object);
drawDescriptionMap.put(mod + ":" + identifier, description);
}
public DrawDescription getDrawDescription(Block block) {
if (!drawDescriptionMap.containsKey(block)) {
throw new IllegalArgumentException(String.format("No description for block %s found", block));
}
return drawDescriptionMap.get(block.getMod() + ":" + block.getIdentifier());
}
public boolean isFull(Block block) {
if (block == Blocks.nullBlock || block == null) {
return false; return false;
} }
return drawDescriptionMap.get(block).isFull(); return getDrawDescription(block).isFull();
} }
} }

View File

@ -1,49 +0,0 @@
/*
* Codename Minosoft
* Copyright (C) 2020 Lukas Eisenhauer
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.render.blockModels;
import de.bixilon.minosoft.game.datatypes.blocks.Blocks;
import java.util.HashMap;
import java.util.Map;
import static de.bixilon.minosoft.game.datatypes.blocks.Blocks.byId;
public class BlockName {
private static final Map<String, Blocks> nameToBlock = new HashMap<String, Blocks>() {
{
//put("air", byId(0));
put("bedrock", byId(7));
put("grass_block", byId(2));
put("dirt", byId(3));
}
};
private static final Map<Blocks, String> blockToName = new HashMap<Blocks, String>() {
{
//put("air", byId(0));
put(byId(7), "bedrock");
put(byId(2), "grass_block");
put(byId(3), "dirt");
}
};
public static Blocks getBlockByName(String name) {
return nameToBlock.get(name);
}
public static String getNameByBlock(Blocks block) {
return blockToName.get(block);
}
}