diff --git a/src/games/stendhal/server/entity/creature/Creature.java b/src/games/stendhal/server/entity/creature/Creature.java index 472eadad0ec..1a485ae1501 100644 --- a/src/games/stendhal/server/entity/creature/Creature.java +++ b/src/games/stendhal/server/entity/creature/Creature.java @@ -49,7 +49,7 @@ import games.stendhal.server.entity.item.Corpse; import games.stendhal.server.entity.item.Item; import games.stendhal.server.entity.item.StackableItem; -import games.stendhal.server.entity.mapstuff.spawner.CreatureRespawnPoint; +import games.stendhal.server.entity.mapstuff.spawner.CreatureSpawner; import games.stendhal.server.entity.npc.NPC; import games.stendhal.server.entity.player.Player; import games.stendhal.server.entity.slot.EntitySlot; @@ -122,7 +122,7 @@ public class Creature extends NPC { private int corpseWidth = 1; private int corpseHeight = 1; - private CreatureRespawnPoint point; + private CreatureSpawner spawner; /** Respawn time in turns */ private int respawnTime; @@ -549,18 +549,40 @@ public Map getAIProfiles() { return aiProfiles; } - public void setRespawnPoint(final CreatureRespawnPoint point) { - this.point = point; + /** + * Registers this creature with a spawner. + * + * @param spawner + * Spawner instance. + */ + public void setSpawner(final CreatureSpawner spawner) { + this.spawner = spawner; setRespawned(true); } + @Deprecated + public void setRespawnPoint(final CreatureSpawner spawner) { + setSpawner(spawner); + } + + /** + * Gets the spawner of this creature. + * + * @return + * Spawner instance. + */ + public CreatureSpawner getRespawner() { + return spawner; + } + /** * gets the respan point of this create * * @return CreatureRespawnPoint */ - public CreatureRespawnPoint getRespawnPoint() { - return point; + @Deprecated + public CreatureSpawner getRespawnPoint() { + return getRespawner(); } /** @@ -685,8 +707,8 @@ public void onDead(final Killer killer, final boolean remove) { notifyRegisteredObjects(); - if (this.point != null) { - this.point.notifyDead(this); + if (this.spawner != null) { + this.spawner.onRemoved(this); } super.onDead(killer, remove); diff --git a/src/games/stendhal/server/entity/mapstuff/spawner/CreatureRespawnPoint.java b/src/games/stendhal/server/entity/mapstuff/spawner/CreatureRespawnPoint.java index b81fd3919a0..56dfc1e427f 100644 --- a/src/games/stendhal/server/entity/mapstuff/spawner/CreatureRespawnPoint.java +++ b/src/games/stendhal/server/entity/mapstuff/spawner/CreatureRespawnPoint.java @@ -21,7 +21,6 @@ import games.stendhal.common.Rand; import games.stendhal.server.core.engine.SingletonRepository; import games.stendhal.server.core.engine.StendhalRPZone; -import games.stendhal.server.core.events.TurnListener; import games.stendhal.server.core.rp.StendhalRPAction; import games.stendhal.server.entity.creature.Creature; import games.stendhal.server.util.Observer; @@ -38,12 +37,7 @@ * pattern is used; the prototypeCreature will be copied to create new * creatures. */ -public class CreatureRespawnPoint implements TurnListener { - /** longest possible respawn time in turns. half a year - should be longer than the - * server is up in one phase */ - private static final int MAX_RESPAWN_TIME = 200 * 60 * 24 * 30 * 6; - /** minimum respawn time in turns. about 10s */ - private static final int MIN_RESPAWN_TIME = 33; +public class CreatureRespawnPoint implements CreatureSpawner { /** the logger instance. */ private static final Logger logger = Logger.getLogger(CreatureRespawnPoint.class); @@ -143,23 +137,34 @@ public void setRespawnTime(final int respawnTime) { this.respawnTime = respawnTime; } - /** - * Notifies this respawn point about the death of a creature that was - * spawned here. - * - * @param dead - * The creature that has died - */ - public void notifyDead(final Creature dead) { + @Override + public void onSpawned(Creature spawned) { + spawned.init(); + spawned.setRespawnPoint(this); + creatures.add(spawned); + } + @Override + public void onRemoved(Creature removed) { if (!respawning) { // start respawning a new creature respawning = true; SingletonRepository.getTurnNotifier().notifyInTurns( calculateNextRespawnTurn(), this); } + creatures.remove(removed); + } - creatures.remove(dead); + /** + * Notifies this respawn point about the death of a creature that was + * spawned here. + * + * @param dead + * The creature that has died + */ + @Deprecated + public void notifyDead(final Creature dead) { + onRemoved(dead); } /** @@ -226,6 +231,7 @@ public void setPrototypeCreature(final Creature creature) { * add observer to observers list * @param observer - observer to add */ + @Override public void addObserver(final Observer observer) { observers.add(observer); } @@ -234,6 +240,7 @@ public void addObserver(final Observer observer) { * remove observer from list * @param observer - observer to remove */ + @Override public void removeObserver(final Observer observer) { observers.remove(observer); } @@ -256,10 +263,7 @@ protected void respawn() { newentity.registerObjectsForNotification(observers); if (StendhalRPAction.placeat(zone, newentity, x, y)) { - newentity.init(); - newentity.setRespawnPoint(this); - - creatures.add(newentity); + onSpawned(newentity); } else { // Could not place the creature anywhere. // Treat it like it just had died. diff --git a/src/games/stendhal/server/entity/mapstuff/spawner/CreatureSpawner.java b/src/games/stendhal/server/entity/mapstuff/spawner/CreatureSpawner.java new file mode 100644 index 00000000000..85f348f0249 --- /dev/null +++ b/src/games/stendhal/server/entity/mapstuff/spawner/CreatureSpawner.java @@ -0,0 +1,47 @@ +/*************************************************************************** + * Copyright © 2024 - Faiumoni e. V. * + *************************************************************************** + *************************************************************************** + * * + * 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 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ +package games.stendhal.server.entity.mapstuff.spawner; + +import games.stendhal.server.core.events.TurnListener; +import games.stendhal.server.entity.creature.Creature; +import games.stendhal.server.util.Observer; + + +public interface CreatureSpawner extends TurnListener { + + /** Longest possible respawn time in turns (half a year - should be longer than the server is up + * in one phase). */ + static final int MAX_RESPAWN_TIME = 200 * 60 * 24 * 30 * 6; + /** Minimum respawn time in turns (about 10s) */ + static final int MIN_RESPAWN_TIME = 33; + + + void addObserver(Observer observer); + + void removeObserver(Observer observer); + + /** + * Notifies this spawner that a creature was spawned. + * + * @param spawned + * The new creature. + */ + void onSpawned(Creature spawned); + + /** + * Notifies this spawner about the death of a creature that was spawned with it. + * + * @param removed + * The creature that was removed. + */ + void onRemoved(Creature removed); +}