diff --git a/common/src/main/java/com/dfsek/terra/api/event/events/AbstractCancellable.java b/common/src/main/java/com/dfsek/terra/api/event/events/AbstractCancellable.java new file mode 100644 index 000000000..e6fdd5319 --- /dev/null +++ b/common/src/main/java/com/dfsek/terra/api/event/events/AbstractCancellable.java @@ -0,0 +1,20 @@ +package com.dfsek.terra.api.event.events; + +import com.dfsek.terra.api.util.mutable.MutableBoolean; + +/** + * Abstract class containing basic {@link Cancellable} implementation. + */ +public abstract class AbstractCancellable implements Cancellable { + private final MutableBoolean cancelled = new MutableBoolean(false); + + @Override + public boolean isCancelled() { + return cancelled.get(); + } + + @Override + public void setCancelled(boolean cancelled) { + this.cancelled.set(cancelled); + } +} diff --git a/common/src/main/java/com/dfsek/terra/api/event/events/world/generation/EntitySpawnEvent.java b/common/src/main/java/com/dfsek/terra/api/event/events/world/generation/EntitySpawnEvent.java new file mode 100644 index 000000000..2f4b25bbe --- /dev/null +++ b/common/src/main/java/com/dfsek/terra/api/event/events/world/generation/EntitySpawnEvent.java @@ -0,0 +1,45 @@ +package com.dfsek.terra.api.event.events.world.generation; + +import com.dfsek.terra.api.event.events.PackEvent; +import com.dfsek.terra.api.math.vector.Location; +import com.dfsek.terra.api.platform.entity.Entity; +import com.dfsek.terra.api.structures.structure.buffer.items.BufferedEntity; +import com.dfsek.terra.config.pack.ConfigPack; + +/** + * Called when an entity is spawned via {@link BufferedEntity}. + */ +public class EntitySpawnEvent implements PackEvent { + private final ConfigPack pack; + private final Entity entity; + private final Location location; + + public EntitySpawnEvent(ConfigPack pack, Entity entity, Location location) { + this.pack = pack; + this.entity = entity; + this.location = location; + } + + @Override + public ConfigPack getPack() { + return pack; + } + + /** + * Get the entity that triggered the event. + * + * @return The entity. + */ + public Entity getEntity() { + return entity; + } + + /** + * Get the location of the entity. + * + * @return Location of the entity. + */ + public Location getLocation() { + return location; + } +} diff --git a/common/src/main/java/com/dfsek/terra/api/event/events/world/generation/LootPopulateEvent.java b/common/src/main/java/com/dfsek/terra/api/event/events/world/generation/LootPopulateEvent.java index ca7eeefb2..f1cbfd74d 100644 --- a/common/src/main/java/com/dfsek/terra/api/event/events/world/generation/LootPopulateEvent.java +++ b/common/src/main/java/com/dfsek/terra/api/event/events/world/generation/LootPopulateEvent.java @@ -1,21 +1,20 @@ package com.dfsek.terra.api.event.events.world.generation; +import com.dfsek.terra.api.event.events.AbstractCancellable; import com.dfsek.terra.api.event.events.Cancellable; import com.dfsek.terra.api.event.events.PackEvent; import com.dfsek.terra.api.platform.block.Block; import com.dfsek.terra.api.platform.block.state.Container; import com.dfsek.terra.api.structures.structure.buffer.items.BufferedLootApplication; -import com.dfsek.terra.api.util.mutable.MutableBoolean; import com.dfsek.terra.config.pack.ConfigPack; /** * Called when loot is populated via {@link BufferedLootApplication}. */ -public class LootPopulateEvent implements PackEvent, Cancellable { +public class LootPopulateEvent extends AbstractCancellable implements PackEvent, Cancellable { private final Block block; private final Container container; private final ConfigPack pack; - private final MutableBoolean cancelled = new MutableBoolean(); public LootPopulateEvent(Block block, Container container, ConfigPack pack) { this.block = block; @@ -30,6 +29,7 @@ public class LootPopulateEvent implements PackEvent, Cancellable { /** * Get the block containing the tile entity loot is applied to. + * * @return Block at which loot is applied. */ public Block getBlock() { @@ -38,19 +38,10 @@ public class LootPopulateEvent implements PackEvent, Cancellable { /** * Get the {@link Container} representing the inventory. + * * @return Inventory recieving loot. */ public Container getContainer() { return container; } - - @Override - public boolean isCancelled() { - return cancelled.get(); - } - - @Override - public void setCancelled(boolean cancelled) { - this.cancelled.set(cancelled); - } } diff --git a/common/src/main/java/com/dfsek/terra/api/structures/script/functions/EntityFunction.java b/common/src/main/java/com/dfsek/terra/api/structures/script/functions/EntityFunction.java index 547ae7a00..b8b9f8d1b 100644 --- a/common/src/main/java/com/dfsek/terra/api/structures/script/functions/EntityFunction.java +++ b/common/src/main/java/com/dfsek/terra/api/structures/script/functions/EntityFunction.java @@ -21,9 +21,11 @@ public class EntityFunction implements Function { private final EntityType data; private final Returnable x, y, z; private final Position position; + private final TerraPlugin main; public EntityFunction(Returnable x, Returnable y, Returnable z, Returnable data, TerraPlugin main, Position position) throws ParseException { this.position = position; + this.main = main; if(!(data instanceof ConstantExpression)) throw new ParseException("Entity data must be constant", data.getPosition()); this.data = main.getWorldHandle().getEntity(((ConstantExpression) data).getConstant()); @@ -39,7 +41,7 @@ public class EntityFunction implements Function { RotationUtil.rotateVector(xz, arguments.getRotation()); - arguments.getBuffer().addItem(new BufferedEntity(data), new Vector3(xz.getX(), y.apply(implementationArguments, variableMap).doubleValue(), xz.getZ()).toLocation(arguments.getBuffer().getOrigin().getWorld())); + arguments.getBuffer().addItem(new BufferedEntity(data, main), new Vector3(xz.getX(), y.apply(implementationArguments, variableMap).doubleValue(), xz.getZ()).toLocation(arguments.getBuffer().getOrigin().getWorld())); return null; } diff --git a/common/src/main/java/com/dfsek/terra/api/structures/structure/buffer/items/BufferedEntity.java b/common/src/main/java/com/dfsek/terra/api/structures/structure/buffer/items/BufferedEntity.java index b0e7987ce..6b8315ca2 100644 --- a/common/src/main/java/com/dfsek/terra/api/structures/structure/buffer/items/BufferedEntity.java +++ b/common/src/main/java/com/dfsek/terra/api/structures/structure/buffer/items/BufferedEntity.java @@ -1,18 +1,24 @@ package com.dfsek.terra.api.structures.structure.buffer.items; +import com.dfsek.terra.api.TerraPlugin; +import com.dfsek.terra.api.event.events.world.generation.EntitySpawnEvent; import com.dfsek.terra.api.math.vector.Location; +import com.dfsek.terra.api.platform.entity.Entity; import com.dfsek.terra.api.platform.entity.EntityType; public class BufferedEntity implements BufferedItem { private final EntityType type; + private final TerraPlugin main; - public BufferedEntity(EntityType type) { + public BufferedEntity(EntityType type, TerraPlugin main) { this.type = type; + this.main = main; } @Override public void paste(Location origin) { - origin.clone().add(0.5, 0, 0.5).getWorld().spawnEntity(origin, type); + Entity entity = origin.clone().add(0.5, 0, 0.5).getWorld().spawnEntity(origin, type); + main.getEventManager().callEvent(new EntitySpawnEvent(main.getWorld(entity.getWorld()).getGenerator().getConfigPack(), entity, entity.getLocation())); } } diff --git a/common/src/main/java/com/dfsek/terra/api/util/mutable/MutableBoolean.java b/common/src/main/java/com/dfsek/terra/api/util/mutable/MutableBoolean.java index 50abd8feb..a71fdb9be 100644 --- a/common/src/main/java/com/dfsek/terra/api/util/mutable/MutableBoolean.java +++ b/common/src/main/java/com/dfsek/terra/api/util/mutable/MutableBoolean.java @@ -5,6 +5,14 @@ import org.jetbrains.annotations.NotNull; public class MutableBoolean implements MutablePrimitive { private boolean value; + public MutableBoolean() { + this.value = false; + } + + public MutableBoolean(boolean value) { + this.value = value; + } + @Override public Boolean get() { return value;