diff --git a/src/main/java/de/bixilon/minosoft/Minosoft.java b/src/main/java/de/bixilon/minosoft/Minosoft.java index 09926deda..ee854550a 100644 --- a/src/main/java/de/bixilon/minosoft/Minosoft.java +++ b/src/main/java/de/bixilon/minosoft/Minosoft.java @@ -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); } /** diff --git a/src/main/java/de/bixilon/minosoft/game/datatypes/blocks/Block.java b/src/main/java/de/bixilon/minosoft/game/datatypes/blocks/Block.java index 3c8e923d1..c99eb1b7e 100644 --- a/src/main/java/de/bixilon/minosoft/game/datatypes/blocks/Block.java +++ b/src/main/java/de/bixilon/minosoft/game/datatypes/blocks/Block.java @@ -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 properties; - public Block(String mod, String identifier, BlockProperties[] properties, BlockRotation rotation) { + public Block(String mod, String identifier, ArrayList 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 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 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 { diff --git a/src/main/java/de/bixilon/minosoft/game/datatypes/blocks/Blocks.java b/src/main/java/de/bixilon/minosoft/game/datatypes/blocks/Blocks.java index 537295b85..5c9965afa 100644 --- a/src/main/java/de/bixilon/minosoft/game/datatypes/blocks/Blocks.java +++ b/src/main/java/de/bixilon/minosoft/game/datatypes/blocks/Blocks.java @@ -590,4 +590,8 @@ public class Blocks { } return blockId; } + + public static ArrayList getBlockList() { + return blockList; + } } diff --git a/src/main/java/de/bixilon/minosoft/game/datatypes/world/Chunk.java b/src/main/java/de/bixilon/minosoft/game/datatypes/world/Chunk.java index fb8951a89..5aa48207b 100644 --- a/src/main/java/de/bixilon/minosoft/game/datatypes/world/Chunk.java +++ b/src/main/java/de/bixilon/minosoft/game/datatypes/world/Chunk.java @@ -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); } diff --git a/src/main/java/de/bixilon/minosoft/game/datatypes/world/ChunkNibble.java b/src/main/java/de/bixilon/minosoft/game/datatypes/world/ChunkNibble.java index 1a4b74fa8..a2d86c5ef 100644 --- a/src/main/java/de/bixilon/minosoft/game/datatypes/world/ChunkNibble.java +++ b/src/main/java/de/bixilon/minosoft/game/datatypes/world/ChunkNibble.java @@ -48,7 +48,7 @@ public class ChunkNibble { blocks.put(location, block); } - public HashMap getBlocks() { + public HashMap getBlocks() { return blocks; } } diff --git a/src/main/java/de/bixilon/minosoft/protocol/protocol/PacketHandler.java b/src/main/java/de/bixilon/minosoft/protocol/protocol/PacketHandler.java index 33633dea0..83b106f8f 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/protocol/PacketHandler.java +++ b/src/main/java/de/bixilon/minosoft/protocol/protocol/PacketHandler.java @@ -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 { diff --git a/src/main/java/de/bixilon/minosoft/render/WorldRenderer.java b/src/main/java/de/bixilon/minosoft/render/WorldRenderer.java index bd844feb5..c9a4da326 100644 --- a/src/main/java/de/bixilon/minosoft/render/WorldRenderer.java +++ b/src/main/java/de/bixilon/minosoft/render/WorldRenderer.java @@ -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 set : chunk.getNibbles().entrySet()) { - for (Map.Entry blockEntry : set.getValue().getBlocks().entrySet()) { + for (Map.Entry 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 diff --git a/src/main/java/de/bixilon/minosoft/render/blockModels/BlockModelLoader.java b/src/main/java/de/bixilon/minosoft/render/blockModels/BlockModelLoader.java index c19180dfd..17595de8f 100644 --- a/src/main/java/de/bixilon/minosoft/render/blockModels/BlockModelLoader.java +++ b/src/main/java/de/bixilon/minosoft/render/blockModels/BlockModelLoader.java @@ -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 drawDescriptionMap; + final HashMap 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(); } } diff --git a/src/main/java/de/bixilon/minosoft/render/blockModels/BlockName.java b/src/main/java/de/bixilon/minosoft/render/blockModels/BlockName.java deleted file mode 100644 index 9cd48899b..000000000 --- a/src/main/java/de/bixilon/minosoft/render/blockModels/BlockName.java +++ /dev/null @@ -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 . - * - * 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 nameToBlock = new HashMap() { - { - //put("air", byId(0)); - put("bedrock", byId(7)); - put("grass_block", byId(2)); - put("dirt", byId(3)); - } - }; - - private static final Map blockToName = new HashMap() { - { - //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); - } -}