Egg destruction/respawn fix, small changes/fixes

Fixed some bugs related to egg destruction/respawning, deleted duplicate code
Hopefully fixed occasional string formatting issues
Small change to config
Small change to readme
This commit is contained in:
J0nasL 2023-07-18 05:45:17 -04:00
parent 5f8d029052
commit 5f2038e97b
6 changed files with 64 additions and 75 deletions

View file

@ -22,7 +22,7 @@
This plugin's configuration file (`config.yml`) allows for several gameplay customizations to accomodate different types of servers.
- The egg can be made invulnerable, or can be set to respawn in The End when it is destroyed
- If set to respawn, the egg can either respawn immediately, or only after the dragon is beaten again
- When the egg teleports, it will become "lost", so it will no longer have an owner until a player finds it
- When the egg teleports, it can become "lost", so it will no longer have an owner until a player finds it
- Dragon eggs that are already stored in a player's ender chest can be excluded from the hunt
**Installation**

View file

@ -19,12 +19,12 @@ import io.github.J0hnL0cke.egghunt.Model.LogHandler;
*/
public class Announcement {
private static final String PREFIX_FORMAT_CODE = "§5§l";
private static final String PREFIX_FORMAT_CODE = ChatColor.COLOR_CHAR+"l"; //bold
private static final ChatColor PREFIX_COLOR = ChatColor.DARK_PURPLE;
private static final ChatColor LOCATION_COLOR = ChatColor.GREEN;
private static final ChatColor CORRECT_COLOR = ChatColor.DARK_GREEN;
private static final ChatColor INCORRECT_COLOR = ChatColor.RED;
private static final String RESET_CODE = "§r";
private static final ChatColor RESET_CODE = ChatColor.RESET;
private static final String RAW_MESSAGE_PREFIX = "[Egg Hunt] ";
public static void sendMessage(Player p, String message) {
@ -106,7 +106,7 @@ public class Announcement {
}
private static String formatMessage(String message) {
return String.format("%s%s%s%s", PREFIX_FORMAT_CODE, RAW_MESSAGE_PREFIX, RESET_CODE, message);
return String.format("%s%s%s%s%s", PREFIX_FORMAT_CODE, PREFIX_COLOR, RAW_MESSAGE_PREFIX, RESET_CODE, message);
}
}

View file

@ -56,7 +56,7 @@ public class EggDestroyListener implements Listener {
if (Egg.hasEgg(item)) {
//make sure item is destroyed to prevent dupes
event.getEntity().remove();
eggDestroyed();
Egg.eggDestroyed(config, data, logger);
}
}
}
@ -75,7 +75,7 @@ public class EggDestroyListener implements Listener {
log("canceled explosion of egg item frame");
event.setCancelled(true);
} else {
eggDestroyed();
Egg.eggDestroyed(config, data, logger);
frame.setItem(null); //make sure item is removed
}
}
@ -94,7 +94,7 @@ public class EggDestroyListener implements Listener {
if (Egg.hasEgg((ItemStack) event.getEntity())) {
//remove just in case to prevent dupes
event.getEntity().remove();
eggDestroyed();
Egg.eggDestroyed(config, data, logger);
}
}
@ -105,7 +105,8 @@ public class EggDestroyListener implements Listener {
//if the egg does not exist
if (config.getEndWorld().getEnderDragonBattle().hasBeenPreviouslyKilled()) {
//if the dragon has already been beaten
respawnEgg();
Egg.respawnEgg(config, data, logger);
Announcement.announce("The dragon egg has spawned in the end!", logger);
}
}
}
@ -209,7 +210,7 @@ public class EggDestroyListener implements Listener {
Egg.removeEgg(b); //delete egg
}
log("Dragon egg was replaced");
eggDestroyed();
Egg.eggDestroyed(config, data, logger);
}
}
}
@ -240,7 +241,7 @@ public class EggDestroyListener implements Listener {
Egg.removeEgg(b); //delete egg
}
log("Dragon egg was replaced");
eggDestroyed();
Egg.eggDestroyed(config, data, logger);
}
}
}
@ -262,33 +263,6 @@ public class EggDestroyListener implements Listener {
}
}
/**
* Alerts when the egg is destroyed and respawns it if needed
*/
private void eggDestroyed() {
data.resetEggOwner(false, config);
if (config.getRespawnEgg()) {
if (config.getRespawnImmediately()) {
announce("The dragon egg has been destroyed!");
respawnEgg();
} else {
data.resetEggLocation();
announce("The dragon egg has been destroyed! It will respawn the next time the Ender Dragon is defeated.");
}
}
}
/**
* Respawns the dragon egg in the end
*/
private void respawnEgg() {
Block eggBlock = Egg.respawnEgg(config);
data.updateEggLocation(eggBlock);
Announcement.ShowEggEffects(eggBlock.getLocation().add(0.5, 0, 0.5)); //target the center bottom of the block
announce("The dragon egg has spawned in the end!");
}
private void announce(String msg) {
Announcement.announce(msg, logger);
}

View file

@ -38,13 +38,14 @@ public class EventScheduler extends BukkitRunnable {
//TODO first check if chunk is loaded
if (data.getEggType() == Data.Egg_Storage_Type.ENTITY) {
if (data.getEggEntity() == null) {
logger.warning("Lost track of the dragon egg entity!");
logger.warning("Resetting egg location to prevent repeated warnings");
data.resetEggLocation();
return;
logger.warning("Lost track of the dragon egg entity!");
logger.warning("Resetting egg location to prevent repeated warnings");
data.resetEggLocation();
return;
} else if (isUnderWorld(data.getEggEntity())) {
logger.log("Egg entity is under the word. Removing egg from entity");
Location respawnLoc = data.getEggEntity().getLocation();
removeMaterialFromEntity(Material.DRAGON_EGG, data.getEggEntity());
Egg.removeEgg(data.getEggEntity());
if (config.getEggInvulnerable()) {
// get coords for egg to spawn at
@ -59,8 +60,8 @@ public class EventScheduler extends BukkitRunnable {
respawnLoc.setY(yPos);
Egg.spawnEggItem(respawnLoc, config, data); //do not need to update data with this location since item spawn event will be called
} else {
eggDestroyed();
respawnEgg(respawnLoc);
//alert and respawn if applicable
Egg.eggDestroyed(config, data, logger);
}
data.resetEggOwner(true, config);
}
@ -68,22 +69,6 @@ public class EventScheduler extends BukkitRunnable {
}
}
private void eggDestroyed() {
Announcement.announce("The dragon egg has been destroyed!", logger);
data.resetEggOwner(false, config);
}
private void respawnEgg(Location respawnLoc) {
if (config.getRespawnEgg()) {
if (config.getRespawnImmediately()) {
Egg.respawnEgg(config);
} else {
data.resetEggLocation();
Announcement.announce("It will respawn the next time the dragon is defeated", logger);
}
}
}
public boolean isUnderWorld(Entity entity) {
return entity.getLocation().getY() < UNDER_WORLD_HEIGHT;
}

View file

@ -20,6 +20,8 @@ import org.bukkit.inventory.meta.BlockStateMeta;
import org.bukkit.inventory.meta.BundleMeta;
import org.bukkit.util.Vector;
import io.github.J0hnL0cke.egghunt.Controller.Announcement;
/**
* Provides methods related to the dragon egg
*/
@ -35,6 +37,35 @@ public class Egg {
entity.setInvulnerable(true);
}
}
/**
* Alerts when the egg is destroyed and respawns it if needed
*/
public static void eggDestroyed(Configuration config, Data data, LogHandler logger) {
logger.log("egg was destroyed");
OfflinePlayer oldOwner = data.getEggOwner();
data.resetEggOwner(false, config);
data.resetEggLocation();
String msg;
if (config.getRespawnEgg()) {
if (config.getRespawnImmediately()) {
Egg.respawnEgg(config, data, logger);
msg = "The dragon egg has been destroyed and has respawned in The End!";
if (oldOwner != null) { //prevent spamming of egg destruction
Announcement.announce(msg, logger);
}
} else {
msg = "The dragon egg has been destroyed! It will respawn the next time the Ender Dragon is defeated.";
Announcement.announce(msg, logger);
}
} else {
msg = "The dragon egg has been destroyed!";
Announcement.announce(msg, logger);
}
}
/**
* Returns the location above the end fountain where the dragon will respawn
@ -44,16 +75,6 @@ public class Egg {
return config.getEndWorld().getEnderDragonBattle().getEndPortalLocation().add(0, 4, 0);
}
/**
* Spawns the dragon egg in the end world specified by the given config.
* @return The new egg block.
*/
public static Block respawnEgg(Configuration config) {
Block newEggLoc = getEggRespawnLocation(config).getBlock();
newEggLoc.setType(Material.DRAGON_EGG);
return newEggLoc;
}
/**
* Drop the egg out of the given player's inventory
*/
@ -110,6 +131,16 @@ public class Egg {
return drop;
}
/**
* Respawns the dragon egg in the end
*/
public static void respawnEgg(Configuration config, Data data, LogHandler logger) {
Block eggBlock = getEggRespawnLocation(config).getBlock();
eggBlock.setType(Material.DRAGON_EGG);
data.updateEggLocation(eggBlock);
Announcement.ShowEggEffects(eggBlock.getLocation().add(0.5, 0, 0.5)); //target the center bottom of the block
}
/**
* Checks if the given ItemStack is a container that is holding the dragon egg. Also returns false if the provided stack is null.
* @param stack ItemStack to check
@ -184,9 +215,11 @@ public class Egg {
public static boolean removeEgg(Entity entity) {
//TODO update for containers
if (entity instanceof Player) {
if (hasEgg(((Player) entity).getInventory())) {
//TODO
Player player = (Player)entity;
player.getInventory().remove(egg);
}
} else if (entity instanceof LivingEntity) {
LivingEntity mob = (((LivingEntity) entity));

View file

@ -6,9 +6,6 @@ end: world_the_end
#Whether the egg teleporting causes the owner to no longer count as the owner of the egg
reset_owner: true
#Whether to tell players the new location of the egg (true) or the previous location of the egg (false) when it teleports
#!!! not yet implemented !!!
#accurate_loc: false
#Whether the egg should be invulnerable to damage
egg_inv: false