mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-16 02:45:13 -04:00
new BlockModelLoader
This commit is contained in:
parent
c102816915
commit
e85f7d93a1
@ -33,8 +33,7 @@ import org.json.JSONObject;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.*;
|
||||
|
||||
public class Minosoft {
|
||||
static Configuration config;
|
||||
@ -82,9 +81,10 @@ public class Minosoft {
|
||||
} else {
|
||||
Log.mojang("Could not refresh session, you will not be able to join premium servers!");
|
||||
}
|
||||
c.setPlayer(new Player(account));
|
||||
c.connect();
|
||||
MainWindow.start(c);
|
||||
Connection connection = new Connection(config.getString("debug.host"), config.getInteger("debug.port"));
|
||||
connection.setPlayer(new Player(account));
|
||||
connection.connect();
|
||||
MainWindow.start(connection);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -13,20 +13,22 @@
|
||||
|
||||
package de.bixilon.minosoft.game.datatypes.blocks;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Block {
|
||||
final String mod;
|
||||
final String identifier;
|
||||
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.identifier = identifier;
|
||||
this.properties = properties;
|
||||
this.rotation = rotation;
|
||||
}
|
||||
|
||||
public Block(String mod, String identifier, BlockProperties[] properties) {
|
||||
public Block(String mod, String identifier, ArrayList<BlockProperties> properties) {
|
||||
this.mod = mod;
|
||||
this.identifier = identifier;
|
||||
this.properties = properties;
|
||||
@ -36,14 +38,14 @@ public class Block {
|
||||
public Block(String mod, String identifier, BlockRotation rotation) {
|
||||
this.mod = mod;
|
||||
this.identifier = identifier;
|
||||
this.properties = new BlockProperties[0];
|
||||
this.properties = new ArrayList<>();
|
||||
this.rotation = rotation;
|
||||
}
|
||||
|
||||
public Block(String mod, String identifier) {
|
||||
this.mod = mod;
|
||||
this.identifier = identifier;
|
||||
this.properties = new BlockProperties[0];
|
||||
this.properties = new ArrayList<>();
|
||||
this.rotation = BlockRotation.NONE;
|
||||
}
|
||||
|
||||
@ -59,7 +61,7 @@ public class Block {
|
||||
return rotation;
|
||||
}
|
||||
|
||||
public BlockProperties[] getProperties() {
|
||||
public ArrayList<BlockProperties> getProperties() {
|
||||
return properties;
|
||||
}
|
||||
|
||||
@ -71,7 +73,7 @@ public class Block {
|
||||
out.append("rotation=");
|
||||
out.append(getRotation().name());
|
||||
}
|
||||
if (properties.length > 0) {
|
||||
if (properties.size() > 0) {
|
||||
if (out.length() > 0) {
|
||||
out.append(" ,");
|
||||
} else {
|
||||
|
@ -590,4 +590,8 @@ public class Blocks {
|
||||
}
|
||||
return blockId;
|
||||
}
|
||||
|
||||
public static ArrayList<Block> getBlockList() {
|
||||
return blockList;
|
||||
}
|
||||
}
|
||||
|
@ -14,6 +14,7 @@
|
||||
package de.bixilon.minosoft.game.datatypes.world;
|
||||
|
||||
import de.bixilon.minosoft.game.datatypes.blocks.Block;
|
||||
import de.bixilon.minosoft.game.datatypes.blocks.Blocks;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@ -34,7 +35,7 @@ public class Chunk {
|
||||
}
|
||||
byte section = (byte) (y / 16);
|
||||
if (nibbles.get(section) == null) {
|
||||
return Blocks.AIR;
|
||||
return Blocks.nullBlock;
|
||||
}
|
||||
return nibbles.get(section).getBlock(x, y % 16, z);
|
||||
}
|
||||
|
@ -48,7 +48,7 @@ public class ChunkNibble {
|
||||
blocks.put(location, block);
|
||||
}
|
||||
|
||||
public HashMap<ChunkNibbleLocation, Blocks> getBlocks() {
|
||||
public HashMap<ChunkNibbleLocation, Block> getBlocks() {
|
||||
return blocks;
|
||||
}
|
||||
}
|
||||
|
@ -59,7 +59,7 @@ public class PacketHandler {
|
||||
public void handle(PacketStatusResponse pkg) {
|
||||
if (connection.getReason() == ConnectionReason.GET_VERSION) {
|
||||
// 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) {
|
||||
connection.setVersion(ProtocolVersion.byId(pkg.getResponse().getProtocolNumber()));
|
||||
} else {
|
||||
|
@ -13,6 +13,7 @@
|
||||
|
||||
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.world.*;
|
||||
import de.bixilon.minosoft.logging.Log;
|
||||
@ -56,7 +57,7 @@ public class WorldRenderer {
|
||||
int xOffset = location.getX() * 16;
|
||||
int zOffset = location.getZ() * 16;
|
||||
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,
|
||||
(short) (blockEntry.getKey().getY() + set.getKey() * 16),
|
||||
blockEntry.getKey().getZ() + zOffset),
|
||||
@ -65,16 +66,16 @@ public class WorldRenderer {
|
||||
}
|
||||
}
|
||||
|
||||
public void prepareBlock(BlockPosition position, Blocks block) {
|
||||
if (block == Blocks.AIR)
|
||||
public void prepareBlock(BlockPosition position, Block block) {
|
||||
if (block == Blocks.nullBlock)
|
||||
return;
|
||||
|
||||
for (FaceOrientation orientation : FaceOrientation.values()) {
|
||||
BlockPosition neighbourPos = position.add(faceDir[orientation.getId()]);
|
||||
|
||||
if (neighbourPos.getY() >= 0) {
|
||||
Blocks neighbourBlock = MainWindow.getConnection().getPlayer().getWorld().getBlock(neighbourPos);
|
||||
if (!(neighbourBlock == Blocks.AIR || neighbourBlock == null)) //!modelLoader.isFull(neighbourBlock))
|
||||
Block neighbourBlock = MainWindow.getConnection().getPlayer().getWorld().getBlock(neighbourPos);
|
||||
if (!(neighbourBlock == Blocks.nullBlock || neighbourBlock == null)) //!modelLoader.isFull(neighbourBlock))
|
||||
// if there is a block next to the current block, don't draw the face
|
||||
continue;
|
||||
//TODO: fix buggy behavior, not always working correctly, probably a problem in the World or BlockPosition class
|
||||
|
@ -14,52 +14,77 @@
|
||||
package de.bixilon.minosoft.render.blockModels;
|
||||
|
||||
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 org.json.JSONObject;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static de.bixilon.minosoft.render.blockModels.BlockName.getBlockByName;
|
||||
|
||||
public class BlockModelLoader {
|
||||
private final Map<Blocks, DrawDescription> drawDescriptionMap;
|
||||
final HashMap<String, DrawDescription> drawDescriptionMap;
|
||||
|
||||
public BlockModelLoader() {
|
||||
drawDescriptionMap = new HashMap<>();
|
||||
try {
|
||||
loadModels(Config.homeDir + "assets/minecraft/models/block");
|
||||
loadModels();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void loadModels(String path) throws IOException {
|
||||
File[] files = new File(path).listFiles();
|
||||
|
||||
for (File file : files) {
|
||||
String fileName = file.getName().substring(0, file.getName().lastIndexOf('.'));
|
||||
String fileContent = new String(Files.readAllBytes(Paths.get(file.getPath())));
|
||||
JSONObject object = new JSONObject(fileContent);
|
||||
DrawDescription drawDescription = new DrawDescription(object);
|
||||
drawDescriptionMap.put(getBlockByName(fileName), drawDescription);
|
||||
private void loadModels() throws IOException {
|
||||
for (Block block : Blocks.getBlockList()) {
|
||||
String mod = block.getMod();
|
||||
String identifier = block.getIdentifier();
|
||||
if (handleProperties(block)) {
|
||||
return;
|
||||
}
|
||||
if (identifier.contains("pane")) {
|
||||
// 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) {
|
||||
if (!drawDescriptionMap.containsKey(block))
|
||||
throw new IllegalArgumentException(String.format("no description for block %s found", block));
|
||||
return drawDescriptionMap.get(block);
|
||||
private boolean handleProperties(Block block) {
|
||||
return !block.getProperties().contains(BlockProperties.NONE) && block.getProperties().size() != 0;
|
||||
}
|
||||
|
||||
public boolean isFull(Blocks block) {
|
||||
if (block == Blocks.AIR || block == null) {
|
||||
private void loadModel(String mod, String identifier) throws IOException {
|
||||
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 drawDescriptionMap.get(block).isFull();
|
||||
return getDrawDescription(block).isFull();
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user