From e3268ef46c09014e1d97d78acbf1ff67d1f7fea8 Mon Sep 17 00:00:00 2001 From: Carter Date: Sat, 14 Jan 2017 17:25:33 -0500 Subject: [PATCH] Add optional logging for captures (#11) * Add optional logging for captures * Add file logging for captures. --- .../shansen/EggCatcher/EggCatcherLogger.java | 45 +++++++++++++++++++ .../listeners/EggCatcherEntityListener.java | 17 ++++++- src/main/resources/config.yml | 1 + 3 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 src/main/java/me/shansen/EggCatcher/EggCatcherLogger.java diff --git a/src/main/java/me/shansen/EggCatcher/EggCatcherLogger.java b/src/main/java/me/shansen/EggCatcher/EggCatcherLogger.java new file mode 100644 index 0000000..92b79bc --- /dev/null +++ b/src/main/java/me/shansen/EggCatcher/EggCatcherLogger.java @@ -0,0 +1,45 @@ +package me.shansen.EggCatcher; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; +import java.text.DateFormat; +import java.text.SimpleDateFormat; + +//Created 11/11/2016 2:21 AM +public class EggCatcherLogger{ + private final DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); + private File file; + + public EggCatcherLogger(File file){ + this.file = file; + } + + public void logToFile(String message){ + if (!file.exists()){ + try{ + file.createNewFile(); + }catch(IOException e){ + e.printStackTrace(); + } + } + PrintWriter pw = null; + try{ + pw = new PrintWriter(new FileWriter(file, true)); + }catch(IOException e){ + e.printStackTrace(); + } + pw.println(dateFormat.format(System.currentTimeMillis()) + " " + message); + pw.flush(); + pw.close(); + } + + public File getFile(){ + return file; + } + + public void setFile(File file){ + this.file = file; + } +} diff --git a/src/main/java/me/shansen/EggCatcher/listeners/EggCatcherEntityListener.java b/src/main/java/me/shansen/EggCatcher/listeners/EggCatcherEntityListener.java index 5154b7f..83a3ee7 100644 --- a/src/main/java/me/shansen/EggCatcher/listeners/EggCatcherEntityListener.java +++ b/src/main/java/me/shansen/EggCatcher/listeners/EggCatcherEntityListener.java @@ -19,10 +19,12 @@ along with this program. If not, see . package me.shansen.EggCatcher.listeners; import me.shansen.EggCatcher.EggCatcher; +import me.shansen.EggCatcher.EggCatcherLogger; import me.shansen.EggCatcher.EggType; import me.shansen.EggCatcher.events.EggCaptureEvent; import me.shansen.nbt.NbtReflection; + import org.bukkit.Effect; import org.bukkit.Material; import org.bukkit.configuration.file.FileConfiguration; @@ -37,6 +39,8 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.plugin.java.JavaPlugin; +import java.io.File; + public class EggCatcherEntityListener implements Listener { private final boolean usePermissions; @@ -58,8 +62,12 @@ public class EggCatcherEntityListener implements Listener { private final boolean spawnChickenOnFail; private final boolean spawnChickenOnSuccess; private final boolean deleteVillagerInventoryOnCatch; + private final boolean logCaptures; FileConfiguration config; JavaPlugin plugin; + private final File captureLogFile; + private final EggCatcherLogger captureLogger; + public EggCatcherEntityListener(JavaPlugin plugin) { this.config = plugin.getConfig(); @@ -83,7 +91,10 @@ public class EggCatcherEntityListener implements Listener { this.spawnChickenOnSuccess = this.config.getBoolean("SpawnChickenOnSuccess", false); this.vaultTargetBankAccount = this.config.getString("VaultTargetBankAccount", ""); this.deleteVillagerInventoryOnCatch = this.config.getBoolean("DeleteVillagerInventoryOnCatch", false); - } + this.logCaptures = this.config.getBoolean("LogEggCaptures", false); + this.captureLogFile = new File(plugin.getDataFolder(), "captures.txt"); + this.captureLogger = new EggCatcherLogger(captureLogFile); + } @EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR) public void onEntityHitByEgg(EntityDamageEvent event) { @@ -300,5 +311,9 @@ public class EggCatcherEntityListener implements Listener { EggCatcher.eggs.add(egg); } } + + if (this.logCaptures){ + captureLogger.logToFile("Player " + ((Player) egg.getShooter()).getName() + " caught " + entity.getType() + " at X" + Math.round(entity.getLocation().getX()) + ",Y" + Math.round(entity.getLocation().getY()) + ",Z" + Math.round(entity.getLocation().getZ())); + } } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 8e75352..19afd14 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -14,6 +14,7 @@ SpawnChickenOnSuccess: false SpawnChickenOnFail: true VaultTargetBankAccount: "" DeleteVillagerInventoryOnCatch: false +LogEggCaptures: false CatchChance: Bat: 100.0 Blaze: 100.0