Add optional logging for captures (#11)

* Add optional logging for captures

* Add file logging for captures.
This commit is contained in:
Carter 2017-01-14 17:25:33 -05:00 committed by Shansen
parent 19b269d425
commit e3268ef46c
3 changed files with 62 additions and 1 deletions

View File

@ -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;
}
}

View File

@ -19,10 +19,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package me.shansen.EggCatcher.listeners; package me.shansen.EggCatcher.listeners;
import me.shansen.EggCatcher.EggCatcher; import me.shansen.EggCatcher.EggCatcher;
import me.shansen.EggCatcher.EggCatcherLogger;
import me.shansen.EggCatcher.EggType; import me.shansen.EggCatcher.EggType;
import me.shansen.EggCatcher.events.EggCaptureEvent; import me.shansen.EggCatcher.events.EggCaptureEvent;
import me.shansen.nbt.NbtReflection; import me.shansen.nbt.NbtReflection;
import org.bukkit.Effect; import org.bukkit.Effect;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
@ -37,6 +39,8 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
public class EggCatcherEntityListener implements Listener { public class EggCatcherEntityListener implements Listener {
private final boolean usePermissions; private final boolean usePermissions;
@ -58,8 +62,12 @@ public class EggCatcherEntityListener implements Listener {
private final boolean spawnChickenOnFail; private final boolean spawnChickenOnFail;
private final boolean spawnChickenOnSuccess; private final boolean spawnChickenOnSuccess;
private final boolean deleteVillagerInventoryOnCatch; private final boolean deleteVillagerInventoryOnCatch;
private final boolean logCaptures;
FileConfiguration config; FileConfiguration config;
JavaPlugin plugin; JavaPlugin plugin;
private final File captureLogFile;
private final EggCatcherLogger captureLogger;
public EggCatcherEntityListener(JavaPlugin plugin) { public EggCatcherEntityListener(JavaPlugin plugin) {
this.config = plugin.getConfig(); this.config = plugin.getConfig();
@ -83,6 +91,9 @@ public class EggCatcherEntityListener implements Listener {
this.spawnChickenOnSuccess = this.config.getBoolean("SpawnChickenOnSuccess", false); this.spawnChickenOnSuccess = this.config.getBoolean("SpawnChickenOnSuccess", false);
this.vaultTargetBankAccount = this.config.getString("VaultTargetBankAccount", ""); this.vaultTargetBankAccount = this.config.getString("VaultTargetBankAccount", "");
this.deleteVillagerInventoryOnCatch = this.config.getBoolean("DeleteVillagerInventoryOnCatch", false); 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) @EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
@ -300,5 +311,9 @@ public class EggCatcherEntityListener implements Listener {
EggCatcher.eggs.add(egg); 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()));
}
} }
} }

View File

@ -14,6 +14,7 @@ SpawnChickenOnSuccess: false
SpawnChickenOnFail: true SpawnChickenOnFail: true
VaultTargetBankAccount: "" VaultTargetBankAccount: ""
DeleteVillagerInventoryOnCatch: false DeleteVillagerInventoryOnCatch: false
LogEggCaptures: false
CatchChance: CatchChance:
Bat: 100.0 Bat: 100.0
Blaze: 100.0 Blaze: 100.0