Added logging levels, small UX changes

Changed egg sound
Added distance to locateEgg
Started making detailed logging optional
Cancel velocity of dropped egg item
This commit is contained in:
J0nasL 2023-07-18 03:41:34 -04:00
parent 64fa2c68ef
commit f53e0b1e9f
11 changed files with 114 additions and 53 deletions

View file

@ -2,16 +2,18 @@ package io.github.J0hnL0cke.egghunt.Controller;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Particle;
import org.bukkit.Sound;
import org.bukkit.SoundCategory;
import org.bukkit.World;
import org.bukkit.entity.Player;
import io.github.J0hnL0cke.egghunt.Model.LogHandler;
/**
* Misc helper methods relating to player notification
*/
@ -36,11 +38,22 @@ public class Announcement {
int z = destination.getBlockZ();
World world = destination.getWorld();
String worldName = formatWorld(world, origin != null && origin.getWorld().equals(destination.getWorld()));
//whether the two locations are in the same world, or false if origin is null
boolean sameWorld = origin != null && origin.getWorld().equals(destination.getWorld());
String worldName = formatWorld(world, sameWorld);
String distanceStr = "";
if (sameWorld) {
double distance = destination.distance(origin);
if (!Double.isNaN(distance)) {
distanceStr = String.format(" (%d blocks away)", (int)distance);
}
}
String loc = String.format("[%d, %d, %d]", x, y, z);
return String.format("%s%s%s in %s", LOCATION_COLOR, loc, RESET_CODE, worldName);
return String.format("%s%s%s in %s%s", LOCATION_COLOR, loc, RESET_CODE, worldName, distanceStr);
}
public static String formatWorld(World world, boolean correctWorld) {
@ -64,7 +77,7 @@ public class Announcement {
return String.format("%s%s%s", color, worldName, RESET_CODE);
}
public static void announce(String message, Logger logger) {
public static void announce(String message, LogHandler logger) {
List<Player> players = new ArrayList<>(Bukkit.getOnlinePlayers());
int playersNotified = 0;
@ -74,7 +87,7 @@ public class Announcement {
sendMessage(player, message);
}
logger.info(String.format("Told %d player(s) \"%s\"", playersNotified, message));
logger.log(String.format("Told %d player(s) \"%s\"", playersNotified, message));
}
/**
@ -88,13 +101,12 @@ public class Announcement {
}
public static void ShowEggEffects(Player p) {
p.playSound(p.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 10, 0);
p.playSound(p.getLocation(), Sound.UI_TOAST_CHALLENGE_COMPLETE, SoundCategory.MASTER, 10, 0); //min pitch is 0.5
ShowEggEffects(p.getLocation());
}
private static String formatMessage(String message){
private static String formatMessage(String message) {
return String.format("%s%s%s%s", PREFIX_FORMAT_CODE, RAW_MESSAGE_PREFIX, RESET_CODE, message);
}
}

View file

@ -2,7 +2,6 @@ package io.github.J0hnL0cke.egghunt.Controller;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import org.bukkit.TreeType;
import org.bukkit.block.Block;
@ -28,18 +27,19 @@ import io.github.J0hnL0cke.egghunt.Model.Configuration;
import io.github.J0hnL0cke.egghunt.Model.Data;
import io.github.J0hnL0cke.egghunt.Model.Data.Egg_Storage_Type;
import io.github.J0hnL0cke.egghunt.Model.Egg;
import io.github.J0hnL0cke.egghunt.Model.LogHandler;
/**
* Listens for Bukkit events related to destruction of the dragon egg
* Prevents destruction or respawns the egg, depending on config settings
*/
public class EggDestroyListener implements Listener {
private Logger logger;
private LogHandler logger;
private Configuration config;
private Data data;
public EggDestroyListener(Logger logger, Configuration config, Data data) {
public EggDestroyListener(LogHandler logger, Configuration config, Data data) {
this.logger = logger;
this.config = config;
this.data = data;
@ -294,7 +294,7 @@ public class EggDestroyListener implements Listener {
}
private void log(String message) {
logger.info(message);
logger.log(message);
}
}

View file

@ -1,7 +1,5 @@
package io.github.J0hnL0cke.egghunt.Controller;
import java.util.logging.Logger;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
@ -16,6 +14,7 @@ import org.bukkit.scheduler.BukkitRunnable;
import io.github.J0hnL0cke.egghunt.Model.Configuration;
import io.github.J0hnL0cke.egghunt.Model.Data;
import io.github.J0hnL0cke.egghunt.Model.Egg;
import io.github.J0hnL0cke.egghunt.Model.LogHandler;
public class EventScheduler extends BukkitRunnable {
@ -24,9 +23,9 @@ public class EventScheduler extends BukkitRunnable {
private Data data;
private Configuration config;
private Logger logger;
private LogHandler logger;
public EventScheduler(Configuration config, Data data, Logger logger) {
public EventScheduler(Configuration config, Data data, LogHandler logger) {
this.data = data;
this.config = config;
this.logger = logger;

View file

@ -1,6 +1,5 @@
package io.github.J0hnL0cke.egghunt.Controller;
import java.util.logging.Logger;
import org.bukkit.GameMode;
import org.bukkit.Location;
@ -28,17 +27,18 @@ import org.bukkit.inventory.Merchant;
import io.github.J0hnL0cke.egghunt.Model.Configuration;
import io.github.J0hnL0cke.egghunt.Model.Data;
import io.github.J0hnL0cke.egghunt.Model.Egg;
import io.github.J0hnL0cke.egghunt.Model.LogHandler;
/**
* Listens for Bukkit events related to inventories
*/
public class InventoryListener implements Listener {
private Logger logger;
private LogHandler logger;
private Configuration config;
private Data data;
public InventoryListener(Logger logger, Configuration config, Data data) {
public InventoryListener(LogHandler logger, Configuration config, Data data) {
this.logger = logger;
this.config = config;
this.data = data;
@ -310,7 +310,7 @@ public class InventoryListener implements Listener {
}
private void log(String message) {
logger.info(message);
logger.log(message);
}

View file

@ -19,21 +19,20 @@ import org.bukkit.inventory.PlayerInventory;
import io.github.J0hnL0cke.egghunt.Model.Configuration;
import io.github.J0hnL0cke.egghunt.Model.Data;
import io.github.J0hnL0cke.egghunt.Model.Egg;
import io.github.J0hnL0cke.egghunt.Model.LogHandler;
import io.github.J0hnL0cke.egghunt.Model.Data.Egg_Storage_Type;
import java.util.logging.Logger;
/**
* Listens to miscellaneous Bukkit events
*/
public class MiscListener implements Listener {
private Logger logger;
private LogHandler logger;
private Configuration config;
private Data data;
public MiscListener(Logger logger, Configuration config, Data data) {
public MiscListener(LogHandler logger, Configuration config, Data data) {
this.logger = logger;
this.config = config;
this.data = data;
@ -168,9 +167,9 @@ public class MiscListener implements Listener {
if (!config.getAccurateLocation()) {
//TODO disable accuracy here
log("The egg teleported, showing players the egg's location before teleport");
//log("The egg teleported, showing players the egg's location before teleport");
} else {
log("The egg teleported, showing players the egg's up-to-date location");
//log("The egg teleported, showing players the egg's up-to-date location");
}
if (config.resetOwnerOnTeleport()) {
@ -237,7 +236,7 @@ public class MiscListener implements Listener {
}
private void log(String message) {
logger.info(message);
logger.log(message);
}
}

View file

@ -9,6 +9,7 @@ import io.github.J0hnL0cke.egghunt.Persistence.ConfigFileDAO;
* Retrieve settings for this plugin
*/
public class Configuration {
private boolean debug; /** whether to show debug information in console log */
private boolean eggInvulnerable; /** whether to make the egg immune to damage */
private boolean respawnEgg; /** whether the egg should respawn when destroyed */
private boolean respawnImmediately; /** if respawning the egg, whether it should immediately respawn in-place or whether it should generate after next dragon fight */
@ -32,6 +33,7 @@ public class Configuration {
private void loadData() {
//load config settings
debug = fileDao.readBool("debug");
eggInvulnerable = fileDao.readBool("egg_inv");
respawnEgg = fileDao.readBool("resp_egg");
respawnImmediately = fileDao.readBool("resp_imm");
@ -43,9 +45,12 @@ public class Configuration {
ownerTagName = fileDao.read("owner_tag_name", null);
endWorld = Bukkit.getServer().getWorld(fileDao.read("end", DEFAULT_END));
}
public boolean getDebugEnabled() {
return debug;
}
public boolean getEggInvulnerable() {
return eggInvulnerable;

View file

@ -2,7 +2,6 @@ package io.github.J0hnL0cke.egghunt.Model;
import java.time.LocalDateTime;
import java.util.UUID;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.Location;
@ -22,7 +21,7 @@ import io.github.J0hnL0cke.egghunt.Persistence.DataFileDAO;
public class Data {
private DataFileDAO dataDao;
private Logger logger;
private LogHandler logger;
public enum Egg_Storage_Type {
ENTITY,
@ -39,7 +38,7 @@ public class Data {
private UUID entityFallback;
public Data(DataFileDAO dataDao, Logger logger) {
public Data(DataFileDAO dataDao, LogHandler logger) {
this.dataDao = dataDao;
this.logger = logger;
loadData();
@ -323,7 +322,7 @@ public class Data {
}
private void log(String msg) {
logger.info(msg);
logger.log(msg);
}

View file

@ -8,8 +8,6 @@ import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Container;
import org.bukkit.block.ShulkerBox;
import org.bukkit.boss.DragonBattle.RespawnPhase;
import org.bukkit.entity.EnderDragon;
import org.bukkit.entity.Entity;
import org.bukkit.entity.FallingBlock;
import org.bukkit.entity.Item;
@ -69,9 +67,10 @@ public class Egg {
// Drop it on the floor and set its location
//TODO use drop egg method in EggRespawn
Item egg_drop = player.getWorld().dropItem(player.getLocation(),
Item eggDrop = player.getWorld().dropItem(player.getLocation(),
new ItemStack(Material.DRAGON_EGG));
data.updateEggLocation(egg_drop);
eggDrop.setVelocity(new Vector(0, 0, 0));
data.updateEggLocation(eggDrop);
}
}

View file

@ -0,0 +1,43 @@
package io.github.J0hnL0cke.egghunt.Model;
import java.util.logging.Level;
import java.util.logging.Logger;
public class LogHandler {
private Logger logger;
private boolean debug = false;
/**
* Provides loggging functionality
* Debugging requires increasing the logging level, since the console never listens to logs below INFO
* due to how parent loggers are hardcoded
*/
public LogHandler(Logger logger) {
this.logger = logger;
log("Logging ready");
}
public void setDebug(boolean debug) {
this.debug = debug;
if (debug) {
log("Debugging is ON");
}
}
public void log(String msg, Level loggingLevel) {
logger.log(loggingLevel, msg);
}
public void log(String msg) {
log(msg, debug ? Level.INFO : Level.CONFIG);
}
public void info(String msg) {
log(msg, Level.INFO);
}
public void warning(String msg) {
log(msg, Level.WARNING);
}
}

View file

@ -1,7 +1,5 @@
package io.github.J0hnL0cke.egghunt;
import java.util.logging.Logger;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
@ -19,6 +17,7 @@ import io.github.J0hnL0cke.egghunt.Controller.InventoryListener;
import io.github.J0hnL0cke.egghunt.Model.Configuration;
import io.github.J0hnL0cke.egghunt.Model.Data;
import io.github.J0hnL0cke.egghunt.Model.Egg;
import io.github.J0hnL0cke.egghunt.Model.LogHandler;
import io.github.J0hnL0cke.egghunt.Persistence.ConfigFileDAO;
import io.github.J0hnL0cke.egghunt.Persistence.DataFileDAO;
@ -29,27 +28,31 @@ public final class egghunt extends JavaPlugin {
CommandHandler commandHandler;
BukkitTask belowWorldTask;
Logger logger;
LogHandler logger;
@Override
public void onEnable() {
logger = getLogger();
log("Enabling EggHunt...");
//create logger instance
logger = new LogHandler(getLogger());
logger.info("Enabling EggHunt..."); //server already provides an enable message
saveDefaultConfig();
//create model instances using dependency injection
config = new Configuration(new ConfigFileDAO(this));
data = new Data(DataFileDAO.getDataDAO(this), getLogger());
data = new Data(DataFileDAO.getDataDAO(this), logger);
logger.setDebug(config.getDebugEnabled());
//create controller instances
MiscListener miscListener = new MiscListener(getLogger(), config, data);
MiscListener miscListener = new MiscListener(logger, config, data);
InventoryListener inventoryListener = new InventoryListener(logger, config, data);
EggDestroyListener destroyListener = new EggDestroyListener(logger, config, data);
EventScheduler scheduler = new EventScheduler(config, data, logger);
commandHandler = new CommandHandler(data);
//register event handlers
log("Registering event listeners...");
logger.log("Registering event listeners...");
PluginManager manager = getServer().getPluginManager();
manager.registerEvents(miscListener, this);
manager.registerEvents(inventoryListener, this);
@ -57,35 +60,36 @@ public final class egghunt extends JavaPlugin {
//schedule tasks
//TODO: disable task when not in use
log("Scheduling below world task...");
logger.log("Scheduling task...");
belowWorldTask = scheduler.runTaskTimer(this, 20, 20);
log("Done!");
logger.info("Done!");
}
@Override
public void onDisable() {
log("onDisable has been invoked, disabling eggHunt.");
logger.log("Preparing to disable EggHunt..."); //server already provides a disable message
if (data != null) {
//if a player has the egg in their inventory,
//drop it on the ground in case the server is closing
Entity eggHolder = data.getEggEntity();
if (eggHolder != null) {
if (eggHolder instanceof Player) {
log("egg is held by a player, dropping egg...");
logger.log("Egg is held by a player, dropping egg...");
Player p = (Player) eggHolder;
Egg.dropEgg(p, data, config);
//in case the server isn't restarting, let the player know what happend
Announcement.sendMessage(p, "The dragon egg was dropped from your inventory");
}
}
log("saving data...");
logger.log("Saving data...");
data.saveData();
}
logger.log("Disabling task...");
if (belowWorldTask!=null) {
belowWorldTask.cancel();
}
log("Plugin disabled.");
logger.info("EggHunt disabled.");
}
@Override
@ -93,8 +97,6 @@ public final class egghunt extends JavaPlugin {
return commandHandler.onCommand(sender, cmd, label, args);
}
private void log(String msg) {
logger.info(msg);
}
}

View file

@ -1,3 +1,6 @@
#If enabled, logs extra debugging information to the console. This information will appear in logs regardless of debug setting.
debug: false
#Name of your end world, usually to "world_the_end" on spigot
end: world_the_end