Added EntityFireEvent and EntitySpawnEvent

This commit is contained in:
Felix Cravic 2020-05-05 21:04:05 +02:00
parent 0627d8d368
commit 5de6888b59
9 changed files with 88 additions and 52 deletions

View File

@ -17,7 +17,6 @@ import net.minestom.server.item.ItemStack;
import net.minestom.server.item.Material;
import net.minestom.server.network.ConnectionManager;
import net.minestom.server.ping.ResponseDataConsumer;
import net.minestom.server.storage.StorageFolder;
import net.minestom.server.timer.TaskRunnable;
import net.minestom.server.utils.MathUtils;
import net.minestom.server.utils.Position;
@ -33,11 +32,11 @@ public class PlayerInit {
private static volatile InstanceContainer instanceContainer;
static {
StorageFolder storageFolder = MinecraftServer.getStorageManager().getFolder("chunk_data");
//StorageFolder storageFolder = MinecraftServer.getStorageManager().getFolder("chunk_data");
ChunkGeneratorDemo chunkGeneratorDemo = new ChunkGeneratorDemo();
NoiseTestGenerator noiseTestGenerator = new NoiseTestGenerator();
instanceContainer = MinecraftServer.getInstanceManager().createInstanceContainer(storageFolder);
//instanceContainer = MinecraftServer.getInstanceManager().createInstanceContainer();
//instanceContainer = MinecraftServer.getInstanceManager().createInstanceContainer(storageFolder);
instanceContainer = MinecraftServer.getInstanceManager().createInstanceContainer();
instanceContainer.enableAutoChunkLoad(true);
instanceContainer.setChunkGenerator(noiseTestGenerator);

View File

@ -15,7 +15,7 @@ public class StoneBlock extends CustomBlock {
@Override
public void onPlace(Instance instance, BlockPosition blockPosition, Data data) {
System.out.println("PLACED at "+blockPosition);
}
@Override
@ -30,9 +30,7 @@ public class StoneBlock extends CustomBlock {
@Override
public void updateFromNeighbor(Instance instance, BlockPosition thisPosition, BlockPosition neighborPosition, boolean directNeighbor) {
if(directNeighbor) {
System.out.println("Block at "+thisPosition+" has been updated by neighbor at "+neighborPosition);
}
}
@Override

View File

@ -25,24 +25,11 @@ public class SimpleCommand implements CommandProcessor {
Instance instance = player.getInstance();
instance.saveChunksToStorageFolder();
for (EntityCreature creature : instance.getCreatures()) {
creature.setPathTo(player.getPosition());
}
/*StorageManager storageManager = MinecraftServer.getStorageManager();
StorageFolder storageFolder = storageManager.getFolder("player_data");
// Load a data directly into a DataContainer
// The StorageFolder keeps track of the returned data and automatically save it with the #save method
storageFolder.getAndCacheData("held_data", player.getInventory().getItemInMainHand());
storageFolder.saveCachedData();*/
player.sendMessage("Direction: " + MathUtils.getHorizontalDirection(player.getPosition().getYaw()));
return true;

View File

@ -7,11 +7,11 @@ import net.minestom.server.collision.CollisionUtils;
import net.minestom.server.data.Data;
import net.minestom.server.data.DataContainer;
import net.minestom.server.event.CancellableEvent;
import net.minestom.server.event.EntitySpawnEvent;
import net.minestom.server.event.Event;
import net.minestom.server.event.EventCallback;
import net.minestom.server.instance.Chunk;
import net.minestom.server.instance.Instance;
import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.CustomBlock;
import net.minestom.server.network.packet.PacketWriter;
import net.minestom.server.network.packet.server.play.*;
@ -127,8 +127,9 @@ public abstract class Entity implements Viewable, DataContainer {
/**
* Checks if now is a good time to send a velocity update packet
* @return
*
* @param time
* @return
*/
protected boolean shouldSendVelocityUpdate(long time) {
return (time - lastVelocityUpdateTime) >= velocityUpdatePeriod;
@ -136,6 +137,7 @@ public abstract class Entity implements Viewable, DataContainer {
/**
* Gets the period, in ms, between two velocity update packets
*
* @return period, in ms, between two velocity update packets
*/
public long getVelocityUpdatePeriod() {
@ -144,6 +146,7 @@ public abstract class Entity implements Viewable, DataContainer {
/**
* Sets the period, in ms, between two velocity update packets
*
* @param velocityUpdatePeriod period, in ms, between two velocity update packets
*/
public void setVelocityUpdatePeriod(long velocityUpdatePeriod) {
@ -397,6 +400,8 @@ public abstract class Entity implements Viewable, DataContainer {
this.instance = instance;
instance.addEntity(this);
spawn();
EntitySpawnEvent entitySpawnEvent = new EntitySpawnEvent(instance);
callEvent(EntitySpawnEvent.class, entitySpawnEvent);
}
public Vector getVelocity() {

View File

@ -5,6 +5,7 @@ import net.minestom.server.entity.damage.DamageType;
import net.minestom.server.entity.property.Attribute;
import net.minestom.server.event.DeathEvent;
import net.minestom.server.event.EntityDamageEvent;
import net.minestom.server.event.EntityFireEvent;
import net.minestom.server.event.PickupItemEvent;
import net.minestom.server.instance.Chunk;
import net.minestom.server.item.ItemStack;
@ -137,21 +138,26 @@ public abstract class LivingEntity extends Entity {
/**
* Sets fire to this entity for a given duration
*
* @param duration duration in ticks of the effect
*/
public void setFireForDuration(int duration) {
setOnFire(true);
setFireForDuration(duration, TimeUnit.TICK);
}
/**
* Sets fire to this entity for a given duration
*
* @param duration duration of the effet
* @param unit unit used to express the duration
*/
public void setFireForDuration(int duration, TimeUnit unit) {
EntityFireEvent entityFireEvent = new EntityFireEvent(duration, unit);
callCancellableEvent(EntityFireEvent.class, entityFireEvent, () -> {
long fireTime = entityFireEvent.getFireTime(TimeUnit.MILLISECOND);
setOnFire(true);
fireExtinguishTime = System.currentTimeMillis()+unit.toMilliseconds(duration);
fireExtinguishTime = System.currentTimeMillis() + fireTime;
});
}
/**

View File

@ -0,0 +1,31 @@
package net.minestom.server.event;
import net.minestom.server.utils.time.TimeUnit;
public class EntityFireEvent extends CancellableEvent {
private int duration;
private TimeUnit timeUnit;
public EntityFireEvent(int duration, TimeUnit timeUnit) {
setFireTime(duration, timeUnit);
}
public long getFireTime(TimeUnit timeUnit) {
switch (timeUnit) {
case TICK:
return duration;
case MILLISECOND:
return timeUnit.toMilliseconds(duration);
default:
// Unexpected
return -1;
}
}
public void setFireTime(int duration, TimeUnit timeUnit) {
this.duration = duration;
this.timeUnit = timeUnit;
}
}

View File

@ -0,0 +1,17 @@
package net.minestom.server.event;
import net.minestom.server.instance.Instance;
public class EntitySpawnEvent extends Event {
private Instance spawnInstance;
public EntitySpawnEvent(Instance spawnInstance) {
this.spawnInstance = spawnInstance;
}
public Instance getSpawnInstance() {
return spawnInstance;
}
}

View File

@ -2,15 +2,8 @@ package net.minestom.server.event;
import net.minestom.server.instance.Instance;
public class PlayerSpawnEvent extends Event {
private Instance spawnInstance;
public class PlayerSpawnEvent extends EntitySpawnEvent {
public PlayerSpawnEvent(Instance spawnInstance) {
this.spawnInstance = spawnInstance;
}
public Instance getSpawnInstance() {
return spawnInstance;
super(spawnInstance);
}
}