diff --git a/src/main/java/fr/themode/demo/commands/SimpleCommand.java b/src/main/java/fr/themode/demo/commands/SimpleCommand.java index 0b3d2b122..127f92e8a 100644 --- a/src/main/java/fr/themode/demo/commands/SimpleCommand.java +++ b/src/main/java/fr/themode/demo/commands/SimpleCommand.java @@ -1,14 +1,16 @@ package fr.themode.demo.commands; +import fr.themode.demo.generator.ChunkGeneratorDemo; +import net.minestom.server.MinecraftServer; import net.minestom.server.command.CommandProcessor; -import net.minestom.server.entity.EntityCreature; import net.minestom.server.entity.Player; import net.minestom.server.instance.Instance; +import net.minestom.server.world.Dimension; public class SimpleCommand implements CommandProcessor { @Override public String getCommandName() { - return "follow"; + return "test"; } @Override @@ -22,9 +24,15 @@ public class SimpleCommand implements CommandProcessor { Instance instance = player.getInstance(); - for (EntityCreature creature : instance.getCreatures()) { + /*for (EntityCreature creature : instance.getCreatures()) { creature.setPathTo(player.getPosition()); - } + }*/ + + Instance inst = MinecraftServer.getInstanceManager().createInstanceContainer(Dimension.NETHER); + inst.enableAutoChunkLoad(true); + inst.setChunkGenerator(new ChunkGeneratorDemo()); + + player.setInstance(inst); return true; } diff --git a/src/main/java/fr/themode/demo/generator/ChunkGeneratorDemo.java b/src/main/java/fr/themode/demo/generator/ChunkGeneratorDemo.java index 799810adc..961b0eb83 100644 --- a/src/main/java/fr/themode/demo/generator/ChunkGeneratorDemo.java +++ b/src/main/java/fr/themode/demo/generator/ChunkGeneratorDemo.java @@ -20,7 +20,7 @@ public class ChunkGeneratorDemo extends ChunkGenerator { if (random.nextInt(100) > 10) { batch.setCustomBlock(x, y, z, "custom_block"); } else { - batch.setBlock(x, y, z, Block.COMMAND_BLOCK); + batch.setBlock(x, y, z, Block.DIAMOND_BLOCK); } } } diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index faa19000d..ba90effee 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -308,6 +308,12 @@ public class Player extends LivingEntity { } viewableChunks.clear(); + if (this.instance != null) { + Dimension instanceDimension = instance.getDimension(); + if (dimension != instanceDimension) + sendDimension(instanceDimension); + } + long[] visibleChunks = ChunkUtils.getChunksInRange(position, getChunkRange()); int length = visibleChunks.length; @@ -604,7 +610,8 @@ public class Player extends LivingEntity { return gameMode; } - public void setDimension(Dimension dimension) { + // Require sending chunk data after + public void sendDimension(Dimension dimension) { if (dimension == null) throw new IllegalArgumentException("Dimension cannot be null!"); if (dimension.equals(getDimension())) diff --git a/src/main/java/net/minestom/server/entity/pathfinding/AStarPathfinder.java b/src/main/java/net/minestom/server/entity/pathfinding/AStarPathfinder.java index b4cfacdc6..7d04cfe8a 100644 --- a/src/main/java/net/minestom/server/entity/pathfinding/AStarPathfinder.java +++ b/src/main/java/net/minestom/server/entity/pathfinding/AStarPathfinder.java @@ -113,7 +113,7 @@ public class AStarPathfinder { Block targetBlock = Block.fromId(instance.getBlockId(target)); Block belowBlock = Block.fromId(instance.getBlockId(target.clone().add(0, -1, 0))); - boolean result = targetBlock.isAir() && belowBlock.isSolid(); + boolean result = !targetBlock.isSolid() && belowBlock.isSolid(); return result; } @@ -138,7 +138,6 @@ public class AStarPathfinder { private static boolean isShorter(Node neighbor, Node current) { return getTentativeGScore(neighbor, current) < neighbor.g; } - private static LinkedList buildPath(Node finalNode) { LinkedList result = new LinkedList<>(); Node cache = finalNode; @@ -146,25 +145,26 @@ public class AStarPathfinder { result.add(cache.blockPosition); cache = cache.parent; } + // Make the list start->end Collections.reverse(result); return result; } private static class Node { - public int g, h, f; + public int g, f; public Node parent; private int x, y, z; private BlockPosition blockPosition; - public Node(int x, int y, int z) { - this.x = x; - this.y = y; - this.z = z; - this.blockPosition = new BlockPosition(x, y, z); + public Node(BlockPosition blockPosition) { + this.x = blockPosition.getX(); + this.y = blockPosition.getY(); + this.z = blockPosition.getZ(); + this.blockPosition = blockPosition.clone(); } public static Node fromBlockPosition(BlockPosition blockPosition) { - Node node = new Node(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ()); + Node node = new Node(blockPosition); return node; } diff --git a/src/main/java/net/minestom/server/instance/Instance.java b/src/main/java/net/minestom/server/instance/Instance.java index 4b7d72550..8ca11e630 100644 --- a/src/main/java/net/minestom/server/instance/Instance.java +++ b/src/main/java/net/minestom/server/instance/Instance.java @@ -16,6 +16,7 @@ import net.minestom.server.network.packet.server.play.ChunkDataPacket; import net.minestom.server.utils.BlockPosition; import net.minestom.server.utils.ChunkUtils; import net.minestom.server.utils.Position; +import net.minestom.server.world.Dimension; import java.io.File; import java.util.*; @@ -28,6 +29,8 @@ public abstract class Instance implements BlockModifier, DataContainer { protected static final ChunkLoaderIO CHUNK_LOADER_IO = new ChunkLoaderIO(); protected static final BlockManager BLOCK_MANAGER = MinecraftServer.getBlockManager(); + private Dimension dimension; + // Entities present in this instance protected Set players = new CopyOnWriteArraySet<>(); protected Set creatures = new CopyOnWriteArraySet<>(); @@ -39,8 +42,9 @@ public abstract class Instance implements BlockModifier, DataContainer { private Data data; - protected Instance(UUID uniqueId) { + protected Instance(UUID uniqueId, Dimension dimension) { this.uniqueId = uniqueId; + this.dimension = dimension; } public abstract void refreshBlockId(int x, int y, int z, short blockId); @@ -115,6 +119,11 @@ public abstract class Instance implements BlockModifier, DataContainer { } // + + public Dimension getDimension() { + return dimension; + } + public Set getPlayers() { return Collections.unmodifiableSet(players); } diff --git a/src/main/java/net/minestom/server/instance/InstanceContainer.java b/src/main/java/net/minestom/server/instance/InstanceContainer.java index 646f1fb79..7285b6317 100644 --- a/src/main/java/net/minestom/server/instance/InstanceContainer.java +++ b/src/main/java/net/minestom/server/instance/InstanceContainer.java @@ -17,6 +17,7 @@ import net.minestom.server.particle.ParticleCreator; import net.minestom.server.utils.BlockPosition; import net.minestom.server.utils.ChunkUtils; import net.minestom.server.utils.SerializerUtils; +import net.minestom.server.world.Dimension; import java.io.File; import java.util.*; @@ -38,8 +39,8 @@ public class InstanceContainer extends Instance { private boolean autoChunkLoad; - protected InstanceContainer(UUID uniqueId, File folder) { - super(uniqueId); + protected InstanceContainer(UUID uniqueId, Dimension dimension, File folder) { + super(uniqueId, dimension); this.folder = folder; } diff --git a/src/main/java/net/minestom/server/instance/InstanceManager.java b/src/main/java/net/minestom/server/instance/InstanceManager.java index 56eabf255..cb0a0d189 100644 --- a/src/main/java/net/minestom/server/instance/InstanceManager.java +++ b/src/main/java/net/minestom/server/instance/InstanceManager.java @@ -2,6 +2,7 @@ package net.minestom.server.instance; import net.minestom.server.MinecraftServer; import net.minestom.server.utils.thread.MinestomThread; +import net.minestom.server.world.Dimension; import java.io.File; import java.util.Collections; @@ -16,17 +17,21 @@ public class InstanceManager { private Set instances = Collections.synchronizedSet(new HashSet<>()); - public InstanceContainer createInstanceContainer(File folder) { + public InstanceContainer createInstanceContainer(Dimension dimension, File folder) { if (folder != null && !folder.exists()) folder.mkdir(); - InstanceContainer instance = new InstanceContainer(UUID.randomUUID(), folder); + InstanceContainer instance = new InstanceContainer(UUID.randomUUID(), dimension, folder); this.instances.add(instance); return instance; } + public InstanceContainer createInstanceContainer(Dimension dimension) { + return createInstanceContainer(dimension, null); + } + public InstanceContainer createInstanceContainer() { - return createInstanceContainer(null); + return createInstanceContainer(Dimension.OVERWORLD); } public SharedInstance createSharedInstance(InstanceContainer instanceContainer) { diff --git a/src/main/java/net/minestom/server/instance/SharedInstance.java b/src/main/java/net/minestom/server/instance/SharedInstance.java index 1b3eb4acf..7be1058a5 100644 --- a/src/main/java/net/minestom/server/instance/SharedInstance.java +++ b/src/main/java/net/minestom/server/instance/SharedInstance.java @@ -19,7 +19,7 @@ public class SharedInstance extends Instance { private InstanceContainer instanceContainer; protected SharedInstance(UUID uniqueId, InstanceContainer instanceContainer) { - super(uniqueId); + super(uniqueId, instanceContainer.getDimension()); this.instanceContainer = instanceContainer; }