WIP synchronization fix + updated hydrazine

This commit is contained in:
Felix Cravic 2020-08-10 07:24:43 +02:00
parent 644c1d9a71
commit 008002f11b
9 changed files with 39 additions and 50 deletions

View File

@ -69,8 +69,8 @@ dependencies {
api 'org.projectlombok:lombok:1.18.12'
annotationProcessor 'org.projectlombok:lombok:1.18.12'
// Pathfinding
api 'com.github.MadMartian:hydrazine-path-finding:1.2.3'
// Path finding
api 'com.github.MadMartian:hydrazine-path-finding:1.3.0'
api "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
api 'com.github.jglrxavpok:Hephaistos:v1.0.5'

View File

@ -36,25 +36,11 @@ public class Main {
commandManager.register(new ShutdownCommand());
commandManager.register(new TeleportCommand());
/*RecipeManager recipeManager = MinecraftServer.getRecipeManager();
ShapelessRecipe shapelessRecipe = new ShapelessRecipe("test", "groupname") {
@Override
public boolean shouldShow(Player player) {
return true;
}
};
shapelessRecipe.setResult(new ItemStack(Material.STONE, (byte) 1));
DeclareRecipesPacket.Ingredient ingredient = new DeclareRecipesPacket.Ingredient();
ingredient.items = new ItemStack[]{new ItemStack(Material.STONE, (byte) 3)};
shapelessRecipe.addIngredient(ingredient);
recipeManager.addRecipe(shapelessRecipe);*/
StorageManager storageManager = MinecraftServer.getStorageManager();
storageManager.defineDefaultStorageSystem(FileStorageSystem::new);
MinecraftServer.getBenchmarkManager().enable(new UpdateOption(10 * 1000, TimeUnit.MILLISECOND));
MinecraftServer.getSchedulerManager().buildShutdownTask(() -> System.out.println("Good night")).schedule();
PlayerInit.init();

View File

@ -15,6 +15,7 @@ import net.minestom.server.event.item.ItemDropEvent;
import net.minestom.server.event.item.ItemUpdateStateEvent;
import net.minestom.server.event.item.PickupItemEvent;
import net.minestom.server.event.player.*;
import net.minestom.server.instance.Chunk;
import net.minestom.server.instance.Instance;
import net.minestom.server.instance.InstanceContainer;
import net.minestom.server.instance.WorldBorder;
@ -26,6 +27,7 @@ import net.minestom.server.item.Material;
import net.minestom.server.item.metadata.MapMeta;
import net.minestom.server.network.ConnectionManager;
import net.minestom.server.ping.ResponseDataConsumer;
import net.minestom.server.scoreboard.Sidebar;
import net.minestom.server.utils.MathUtils;
import net.minestom.server.utils.Position;
import net.minestom.server.utils.Vector;
@ -112,6 +114,7 @@ public class PlayerInit {
});
connectionManager.addPlayerInitialization(player -> {
player.addEventCallback(EntityAttackEvent.class, event -> {
final Entity entity = event.getTarget();
if (entity instanceof EntityCreature) {
@ -144,16 +147,14 @@ public class PlayerInit {
if (block == Block.TORCH) {
event.setCustomBlock((short) 3); // custom torch block
}
/*for (Player p : player.getInstance().getPlayers()) {
if (p != player)
p.teleport(player.getPosition());
}*/
for (int i = 0; i < 1; i++) {
for (int i = 0; i < 100; i++) {
ChickenCreature chickenCreature = new ChickenCreature(player.getPosition());
chickenCreature.setInstance(player.getInstance());
//chickenCreature.setTarget(player);
}
/*EntityZombie zombie = new EntityZombie(player.getPosition());
@ -266,6 +267,12 @@ public class PlayerInit {
player.getInventory().setItemStack(i, new ItemStack(Material.STONE, (byte) 127));
}*/
{
Sidebar sidebar = new Sidebar("title");
sidebar.createLine(new Sidebar.ScoreboardLine("id1", ColoredText.of("text"), 1));
sidebar.addViewer(player);
}
{
ItemStack map = new ItemStack(Material.FILLED_MAP, (byte) 1);
MapMeta mapMeta = (MapMeta) map.getItemMeta();
@ -359,7 +366,7 @@ public class PlayerInit {
System.out.println("PLAYER EAT EVENT");
});
/*player.addEventCallback(PlayerChunkUnloadEvent.class, event -> {
player.addEventCallback(PlayerChunkUnloadEvent.class, event -> {
Instance instance = player.getInstance();
Chunk chunk = instance.getChunk(event.getChunkX(), event.getChunkZ());
@ -371,7 +378,7 @@ public class PlayerInit {
if (chunk.getViewers().isEmpty()) {
player.getInstance().unloadChunk(chunk);
}
});*/
});
});
}

View File

@ -2,14 +2,12 @@ package fr.themode.demo.entity;
import net.minestom.server.attribute.Attribute;
import net.minestom.server.entity.LivingEntity;
import net.minestom.server.entity.ai.goal.MeleeAttackGoal;
import net.minestom.server.entity.ai.target.ClosestEntityTarget;
import net.minestom.server.entity.ai.goal.RandomStrollGoal;
import net.minestom.server.entity.damage.DamageType;
import net.minestom.server.entity.type.animal.EntityChicken;
import net.minestom.server.event.entity.EntityAttackEvent;
import net.minestom.server.utils.Position;
import net.minestom.server.utils.Vector;
import net.minestom.server.utils.time.TimeUnit;
public class ChickenCreature extends EntityChicken {
@ -17,8 +15,8 @@ public class ChickenCreature extends EntityChicken {
super(defaultPosition);
//goalSelectors.add(new DoNothingGoal(this, 500, 0.1f));
goalSelectors.add(new MeleeAttackGoal(this, 500, TimeUnit.MILLISECOND));
//goalSelectors.add(new RandomStrollGoal(this, 2));
//goalSelectors.add(new MeleeAttackGoal(this, 500, TimeUnit.MILLISECOND));
goalSelectors.add(new RandomStrollGoal(this, 2));
/*goalSelectors.add(new EatBlockGoal(this,
new HashMap<>() {
{
@ -36,7 +34,7 @@ public class ChickenCreature extends EntityChicken {
//targetSelectors.add(new LastEntityDamagerTarget(this, 15));
targetSelectors.add(new ClosestEntityTarget(this, 15, LivingEntity.class));
//targetSelectors.add(new ClosestEntityTarget(this, 15, LivingEntity.class));
setAttribute(Attribute.MOVEMENT_SPEED, 0.1f);

View File

@ -25,7 +25,7 @@ public class NoiseTestGenerator extends ChunkGenerator {
double height = jNoise.getNoise((x + chunkX * 16) / 16.0, (z + chunkZ * 16) / 16.0) * 15 + 40;
for (int y = 0; y < height; y++) {
if (random.nextInt(100) > 10) {
batch.setCustomBlock(x, y, z, "custom_block");
batch.setBlock(x, y, z, Block.DIAMOND_BLOCK);
} else {
batch.setBlock(x, y, z, Block.GOLD_BLOCK);
}

View File

@ -123,7 +123,7 @@ public abstract class EntityCreature extends LivingEntity {
// Path finding
{
if (pathPosition != null) {
if (pathPosition != null && !pathLock.isLocked()) {
PATHFINDER_MANAGER.getPool().execute(() -> {
this.pathLock.lock();
this.path = pathFinder.updatePathFor(pathingEntity);
@ -347,7 +347,6 @@ public abstract class EntityCreature extends LivingEntity {
}
this.pathLock.lock();
this.pathFinder.reset();
if (position == null) {
this.pathLock.unlock();

View File

@ -7,13 +7,13 @@ import net.minestom.server.instance.Chunk;
import net.minestom.server.instance.Instance;
import net.minestom.server.instance.block.Block;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class PFInstanceSpace implements IInstanceSpace {
private Instance instance;
private Map<Chunk, PFColumnarSpace> chunkSpaceMap = new HashMap<>();
private Map<Chunk, PFColumnarSpace> chunkSpaceMap = new ConcurrentHashMap<>();
public PFInstanceSpace(Instance instance) {
this.instance = instance;

View File

@ -1,8 +1,6 @@
package net.minestom.server.instance;
import io.netty.buffer.ByteBuf;
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import net.minestom.server.MinecraftServer;
import net.minestom.server.UpdateManager;
import net.minestom.server.data.Data;
@ -72,7 +70,7 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
protected Set<ObjectEntity> objectEntities = new CopyOnWriteArraySet<>();
protected Set<ExperienceOrb> experienceOrbs = new CopyOnWriteArraySet<>();
// Entities per chunk
protected Long2ObjectMap<Set<Entity>> chunkEntities = new Long2ObjectOpenHashMap<>();
protected Map<Long, Set<Entity>> chunkEntities = new ConcurrentHashMap<>();
protected UUID uniqueId;
protected List<Consumer<Instance>> nextTick = Collections.synchronizedList(new ArrayList<>());
@ -452,14 +450,15 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
*
* @param chunk the chunk to get the entities from
* @return an unmodifiable set containing all the entities in a chunk,
* if {@code chunk} is null, return an empty {@link HashSet}
* if {@code chunk} is unloaded, return an empty {@link HashSet}
*/
public Set<Entity> getChunkEntities(Chunk chunk) {
if (chunk == null)
if (ChunkUtils.isChunkUnloaded(chunk))
return new HashSet<>();
final long index = ChunkUtils.getChunkIndex(chunk.getChunkX(), chunk.getChunkZ());
return Collections.unmodifiableSet(getEntitiesInChunk(index));
final Set<Entity> entities = getEntitiesInChunk(index);
return Collections.unmodifiableSet(entities);
}
/**
@ -550,7 +549,7 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
* @param x the X position
* @param y the Y position
* @param z the Z position
* @return the visual block id at the position
* @return the block state id at the position
*/
public short getBlockStateId(int x, int y, int z) {
final Chunk chunk = getChunkAt(x, z);
@ -564,7 +563,7 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
* @param x the X position
* @param y the Y position
* @param z the Z position
* @return the visual block id at the position
* @return the block state id at the position
*/
public short getBlockStateId(float x, float y, float z) {
return getBlockStateId(Math.round(x), Math.round(y), Math.round(z));
@ -574,7 +573,7 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
* Give the block state id at the given position
*
* @param blockPosition the block position
* @return the visual block id at the position
* @return the block state id at the position
*/
public short getBlockStateId(BlockPosition blockPosition) {
return getBlockStateId(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ());
@ -725,19 +724,17 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
public <E extends Event> void addEventCallback(Class<E> eventClass, EventCallback<E> eventCallback) {
List<EventCallback> callbacks = getEventCallbacks(eventClass);
callbacks.add(eventCallback);
this.eventCallbacks.put(eventClass, callbacks);
}
@Override
public <E extends Event> void removeEventCallback(Class<E> eventClass, EventCallback<E> eventCallback) {
List<EventCallback> callbacks = getEventCallbacks(eventClass);
callbacks.remove(eventCallback);
this.eventCallbacks.put(eventClass, callbacks);
}
@Override
public <E extends Event> List<EventCallback> getEventCallbacks(Class<E> eventClass) {
return eventCallbacks.getOrDefault(eventClass, new CopyOnWriteArrayList<>());
return eventCallbacks.computeIfAbsent(eventClass, clazz -> new CopyOnWriteArrayList<>());
}
// UNSAFE METHODS (need most of time to be synchronized)

View File

@ -5,6 +5,8 @@ import net.minestom.server.instance.Instance;
import net.minestom.server.utils.chunk.ChunkUtils;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;
/**
* Separate chunks into group of linked chunks
@ -16,12 +18,12 @@ public class PerGroupChunkProvider extends ThreadProvider {
/**
* Chunk -> its chunk group
*/
private Map<Instance, Map<ChunkCoordinate, Set<ChunkCoordinate>>> instanceChunksGroupMap = new HashMap<>();
private Map<Instance, Map<ChunkCoordinate, Set<ChunkCoordinate>>> instanceChunksGroupMap = new ConcurrentHashMap<>();
/**
* Used to know to which instance is linked a Set of chunks
*/
private Map<Instance, Map<Set<ChunkCoordinate>, Instance>> instanceInstanceMap = new HashMap<>();
private Map<Instance, Map<Set<ChunkCoordinate>, Instance>> instanceInstanceMap = new ConcurrentHashMap<>();
@Override
public void onChunkLoad(Instance instance, int chunkX, int chunkZ) {
@ -46,7 +48,7 @@ public class PerGroupChunkProvider extends ThreadProvider {
if (!findGroup) {
// Create group of one chunk
final ChunkCoordinate chunkCoordinate = new ChunkCoordinate(chunkX, chunkZ);
Set<ChunkCoordinate> chunkCoordinates = new HashSet<>();
Set<ChunkCoordinate> chunkCoordinates = new CopyOnWriteArraySet<>();
chunkCoordinates.add(chunkCoordinate);
chunksGroupMap.put(chunkCoordinate, chunkCoordinates);
@ -56,7 +58,7 @@ public class PerGroupChunkProvider extends ThreadProvider {
}
// Represent the merged group of all the neighbours
Set<ChunkCoordinate> finalGroup = new HashSet<>();
Set<ChunkCoordinate> finalGroup = new CopyOnWriteArraySet<>();
// Add the newly loaded chunk to the group
finalGroup.add(new ChunkCoordinate(chunkX, chunkZ));
@ -168,11 +170,11 @@ public class PerGroupChunkProvider extends ThreadProvider {
}
private Map<ChunkCoordinate, Set<ChunkCoordinate>> getChunksGroupMap(Instance instance) {
return instanceChunksGroupMap.computeIfAbsent(instance, inst -> new HashMap<>());
return instanceChunksGroupMap.computeIfAbsent(instance, inst -> new ConcurrentHashMap<>());
}
private Map<Set<ChunkCoordinate>, Instance> getInstanceMap(Instance instance) {
return instanceInstanceMap.computeIfAbsent(instance, inst -> new HashMap<>());
return instanceInstanceMap.computeIfAbsent(instance, inst -> new ConcurrentHashMap<>());
}
}