Merge pull request #303 from DarkArc/testmaster

Added BlockChangePreLogEvent
This commit is contained in:
Ammar Askar
2012-12-30 19:54:52 -08:00
4 changed files with 293 additions and 125 deletions

View File

@@ -1,6 +1,7 @@
package de.diddiz.LogBlock; package de.diddiz.LogBlock;
import de.diddiz.LogBlock.config.Config; import de.diddiz.LogBlock.config.Config;
import de.diddiz.LogBlock.events.BlockChangePreLogEvent;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.BlockState; import org.bukkit.block.BlockState;
@@ -371,8 +372,24 @@ public class Consumer extends TimerTask
} }
private void queueBlock(String playerName, Location loc, int typeBefore, int typeAfter, byte data, String signtext, ChestAccess ca) { private void queueBlock(String playerName, Location loc, int typeBefore, int typeAfter, byte data, String signtext, ChestAccess ca) {
if (playerName == null || loc == null || typeBefore < 0 || typeAfter < 0 || typeBefore > 255 || typeAfter > 255 || hiddenPlayers.contains(playerName) || !isLogged(loc.getWorld()) || typeBefore != typeAfter && hiddenBlocks.contains(typeBefore) && hiddenBlocks.contains(typeAfter))
return; if (Config.fireCustomEvents) {
// Create and call the event
BlockChangePreLogEvent event = new BlockChangePreLogEvent(playerName, loc, typeBefore, typeAfter, data, signtext, ca);
logblock.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) return;
// Update variables
playerName = event.getOwner();
loc = event.getLocation();
typeBefore = event.getTypeBefore();
typeAfter = event.getTypeAfter();
data = event.getData();
signtext = event.getSignText();
ca = event.getChestAccess();
}
// Do this last so LogBlock still has final say in what is being added
if (playerName == null || loc == null || typeBefore < 0 || typeAfter < 0 || typeBefore > 255 || typeAfter > 255 || hiddenPlayers.contains(playerName) || !isLogged(loc.getWorld()) || typeBefore != typeAfter && hiddenBlocks.contains(typeBefore) && hiddenBlocks.contains(typeAfter)) return;
queue.add(new BlockRow(loc, playerName.replaceAll("[^a-zA-Z0-9_]", ""), typeBefore, typeAfter, data, signtext, ca)); queue.add(new BlockRow(loc, playerName.replaceAll("[^a-zA-Z0-9_]", ""), typeBefore, typeAfter, data, signtext, ca));
} }

View File

@@ -1,34 +1,22 @@
package de.diddiz.LogBlock.config; package de.diddiz.LogBlock.config;
import static de.diddiz.util.BukkitUtils.friendlyWorldname; import de.diddiz.LogBlock.*;
import static de.diddiz.util.Utils.parseTimeSpec;
import static org.bukkit.Bukkit.getConsoleSender;
import static org.bukkit.Bukkit.getLogger;
import static org.bukkit.Bukkit.getWorlds;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.logging.Level;
import java.util.zip.DataFormatException;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.permissions.PermissionDefault; import org.bukkit.permissions.PermissionDefault;
import de.diddiz.LogBlock.LogBlock;
import de.diddiz.LogBlock.Logging; import java.io.File;
import de.diddiz.LogBlock.QueryParams; import java.io.IOException;
import de.diddiz.LogBlock.Tool; import java.text.SimpleDateFormat;
import de.diddiz.LogBlock.ToolBehavior; import java.util.*;
import de.diddiz.LogBlock.ToolMode; import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.zip.DataFormatException;
import static de.diddiz.util.BukkitUtils.friendlyWorldname;
import static de.diddiz.util.Utils.parseTimeSpec;
import static org.bukkit.Bukkit.*;
public class Config public class Config
{ {
@@ -36,6 +24,7 @@ public class Config
private static Map<String, WorldConfig> worldConfigs; private static Map<String, WorldConfig> worldConfigs;
public static String url, user, password; public static String url, user, password;
public static int delayBetweenRuns, forceToProcessAtLeast, timePerRun; public static int delayBetweenRuns, forceToProcessAtLeast, timePerRun;
public static boolean fireCustomEvents;
public static boolean useBukkitScheduler; public static boolean useBukkitScheduler;
public static boolean enableAutoClearLog; public static boolean enableAutoClearLog;
public static List<String> autoClearLog; public static List<String> autoClearLog;
@@ -82,6 +71,7 @@ public class Config
def.put("consumer.delayBetweenRuns", 6); def.put("consumer.delayBetweenRuns", 6);
def.put("consumer.forceToProcessAtLeast", 20); def.put("consumer.forceToProcessAtLeast", 20);
def.put("consumer.timePerRun", 200); def.put("consumer.timePerRun", 200);
def.put("consumer.fireCustomEvents", false);
def.put("consumer.useBukkitScheduler", true); def.put("consumer.useBukkitScheduler", true);
def.put("clearlog.dumpDeletedLog", false); def.put("clearlog.dumpDeletedLog", false);
def.put("clearlog.enableAutoClearLog", false); def.put("clearlog.enableAutoClearLog", false);
@@ -141,6 +131,7 @@ public class Config
delayBetweenRuns = config.getInt("consumer.delayBetweenRuns", 6); delayBetweenRuns = config.getInt("consumer.delayBetweenRuns", 6);
forceToProcessAtLeast = config.getInt("consumer.forceToProcessAtLeast", 0); forceToProcessAtLeast = config.getInt("consumer.forceToProcessAtLeast", 0);
timePerRun = config.getInt("consumer.timePerRun", 100); timePerRun = config.getInt("consumer.timePerRun", 100);
fireCustomEvents = config.getBoolean("consumer.fireCustomEvents", false);
useBukkitScheduler = config.getBoolean("consumer.useBukkitScheduler", true); useBukkitScheduler = config.getBoolean("consumer.useBukkitScheduler", true);
enableAutoClearLog = config.getBoolean("clearlog.enableAutoClearLog"); enableAutoClearLog = config.getBoolean("clearlog.enableAutoClearLog");
autoClearLog = config.getStringList("clearlog.auto"); autoClearLog = config.getStringList("clearlog.auto");

View File

@@ -0,0 +1,116 @@
package de.diddiz.LogBlock.events;
import de.diddiz.LogBlock.ChestAccess;
import org.apache.commons.lang.Validate;
import org.bukkit.Location;
import org.bukkit.event.HandlerList;
public class BlockChangePreLogEvent extends PreLogEvent {
private static final HandlerList handlers = new HandlerList();
private Location location;
private int typeBefore, typeAfter;
private byte data;
private String signText;
private ChestAccess chestAccess;
public BlockChangePreLogEvent(String owner, Location location, int typeBefore, int typeAfter, byte data,
String signText, ChestAccess chestAccess) {
super(owner);
this.location = location;
this.typeBefore = typeBefore;
this.typeAfter = typeAfter;
this.data = data;
this.signText = signText;
this.chestAccess = chestAccess;
}
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
public int getTypeBefore() {
return typeBefore;
}
public void setTypeBefore(int typeBefore) {
this.typeBefore = typeBefore;
}
public int getTypeAfter() {
return typeAfter;
}
public void setTypeAfter(int typeAfter) {
this.typeAfter = typeAfter;
}
public byte getData() {
return data;
}
public void setData(byte data) {
this.data = data;
}
public String getSignText() {
return signText;
}
public void setSignText(String[] signText) {
if (signText != null) {
// Check for block
Validate.isTrue(isValidSign(), "Must be valid sign block");
// Check for problems
Validate.noNullElements(signText, "No null lines");
Validate.isTrue(signText.length == 4, "Sign text must be 4 strings");
this.signText = signText[0] + "\0" + signText[1] + "\0" + signText[2] + "\0" + signText[3];
} else {
this.signText = null;
}
}
private boolean isValidSign() {
if ((typeAfter == 63 || typeAfter == 68) && typeBefore == 0) return true;
if ((typeBefore == 63 || typeBefore == 68) && typeAfter == 0) return true;
if ((typeAfter == 63 || typeAfter == 68) && typeBefore == typeAfter) return true;
return false;
}
public ChestAccess getChestAccess() {
return chestAccess;
}
public void setChestAccess(ChestAccess chestAccess) {
this.chestAccess = chestAccess;
}
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}

View File

@@ -0,0 +1,44 @@
package de.diddiz.LogBlock.events;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
public abstract class PreLogEvent extends Event implements Cancellable {
protected boolean cancelled = false;
protected String owner;
public PreLogEvent(String owner) {
this.owner = owner.replaceAll("[^a-zA-Z0-9_]", "");
}
/**
* Returns the player/monster/cause involved in this event
*
* @return Player/monster/cause who is involved in this event
*/
public String getOwner() {
return owner;
}
/**
* Sets the player/monster/cause involved in this event
*
* @param owner The player/monster/cause who is involved in this event
*/
public void setOwner(String owner) {
this.owner = owner.replaceAll("[^a-zA-Z0-9_]", "");
}
public boolean isCancelled() {
return cancelled;
}
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
}