Skip to content

Commit

Permalink
Add Prism integration.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dumbo52 committed Jun 6, 2014
1 parent 86332e5 commit ce46fdd
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 52 deletions.
9 changes: 9 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,21 @@
<artifactId>logblock</artifactId>
<version>dev-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>me.botsko</groupId>
<artifactId>prism</artifactId>
<version>2.0.1</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>repobo-snap</id>
<url>http://repo.bukkit.org/content/groups/public</url>
</repository>
<repository>
<id>dhmc-repo</id>
<url>http://dhmc.us:8081/nexus/content/repositories/public/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
Expand Down
96 changes: 96 additions & 0 deletions src/nu/nerd/SafeBuckets/EventLogger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package nu.nerd.SafeBuckets;

import me.botsko.prism.actionlibs.ActionFactory;
import me.botsko.prism.actionlibs.RecordingQueue;

import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.entity.Player;

import de.diddiz.LogBlock.Consumer;
import de.diddiz.LogBlock.LogBlock;

public class EventLogger {

private final SafeBuckets plugin;
private Consumer lbConsumer;

private boolean useLogBlock;
private boolean usePrism;

public EventLogger(SafeBuckets pl) {
plugin = pl;
useLogBlock = false;
usePrism = false;
}

public void enableLogBlock(LogBlock lb) {
lbConsumer = lb.getConsumer();
useLogBlock = true;
}

public void enablePrism() {
usePrism = true;
}

public boolean canLog() {
return useLogBlock || usePrism;
}

public void logEvent(boolean log, Player player, BlockState before, Block after) {
if (log) {
logEvent(player, before, after);
}
}

public void logEvent(Player player, BlockState before, Block after) {
if (useLogBlock) {
logEventWithLogBlock(player, before, after);
}
if (usePrism) {
logEventWithPrism(player, before, after);
}
}

private void logEventWithLogBlock(Player player, BlockState before, Block after) {
Material mat = plugin.getStationaryMaterial(after.getType());

String name;
if (player == null) {
name = mat == Material.STATIONARY_WATER ? before.getType() == Material.ICE ? "SnowFade" : "WaterFlow" : "LavaFlow";
}
else {
name = player.getName();
}

lbConsumer.queueBlockReplace(name, before, after.getState());
}

private void logEventWithPrism(Player player, BlockState before, Block after) {
String type;
Material mat = plugin.getStationaryMaterial(after.getType());
Material beforeType = before.getType();

if (plugin.isBlockSafe(after) && (plugin.getStationaryMaterial(before.getType()) == after.getType())) {
type = "safebuckets-fluid-safe";
}
else if (!plugin.isBlockSafe(after) && plugin.getStationaryMaterial(before.getType()) == plugin.getStationaryMaterial(after.getType()) && before.getRawData() == 15) {
type = "safebuckets-fluid-unsafe";
beforeType = plugin.getStationaryMaterial(beforeType);
}
else {
type = ((plugin.getStationaryMaterial(before.getType()) == Material.STATIONARY_WATER || plugin.getStationaryMaterial(before.getType()) == Material.STATIONARY_LAVA) && after.getType() == Material.AIR) ? "bucket-fill" : before.getType() == Material.ICE && mat == Material.STATIONARY_WATER ? "block-fade" : mat == Material.STATIONARY_WATER ? "water-bucket" : mat == Material.STATIONARY_LAVA ? "lava-bucket" : "block-place";
}

String name;
if (player == null) {
name = mat == Material.STATIONARY_WATER ? before.getType() == Material.ICE ? "Environment" : "Water" : "Lava";
}
else {
name = player.getName();
}

RecordingQueue.addToQueue(ActionFactory.createBlockChange(type, after.getLocation(), beforeType.getId(), before.getRawData(), after.getTypeId(), after.getData(), name));
}
}
75 changes: 47 additions & 28 deletions src/nu/nerd/SafeBuckets/SafeBuckets.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import me.botsko.prism.Prism;
import me.botsko.prism.actionlibs.ActionType;
import net.minecraft.server.v1_7_R3.EntityHuman;
import net.minecraft.server.v1_7_R3.MathHelper;
import net.minecraft.server.v1_7_R3.MovingObjectPosition;
Expand All @@ -19,21 +21,22 @@
import org.bukkit.World.Environment;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
import org.bukkit.block.Dispenser;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.craftbukkit.v1_7_R3.block.CraftDispenser;
import org.bukkit.craftbukkit.v1_7_R3.inventory.CraftInventory;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;

import com.sk89q.worldedit.bukkit.WorldEditPlugin;
import com.sk89q.worldedit.bukkit.selections.CuboidSelection;
import com.sk89q.worldedit.bukkit.selections.Selection;

import de.diddiz.LogBlock.Consumer;
import de.diddiz.LogBlock.LogBlock;

public class SafeBuckets extends JavaPlugin {
Expand Down Expand Up @@ -66,7 +69,7 @@ public class SafeBuckets extends JavaPlugin {
private Set<String> toolPlayers = new HashSet<String>();
private Set<String> toolblockPlayers = new HashSet<String>();
private WorldEditPlugin worldedit;
private Consumer lbConsumer;
public EventLogger eventLogger;

public static final Logger log = Logger.getLogger("Minecraft");
public HashMap<Location, SafeEntry> blockCache = new HashMap<Location, SafeEntry>();
Expand Down Expand Up @@ -299,17 +302,36 @@ public void onEnable() {
PluginManager pm = this.getServer().getPluginManager();
pm.registerEvents(l, this);

try {
worldedit = (WorldEditPlugin) pm.getPlugin("WorldEdit");
} catch (Exception e) {
log.log(Level.WARNING, "WorldEdit could not be loaded!");
Plugin wepl = pm.getPlugin("WorldEdit");
if (wepl == null) {
log.log(Level.WARNING, "[" + getDescription().getName() + "] WorldEdit could not be loaded!");
}
else {
worldedit = (WorldEditPlugin) wepl;
}

eventLogger = new EventLogger(this);

Plugin prpl = pm.getPlugin("Prism");
if (prpl != null) {
try {
Prism.getActionRegistry().registerCustomAction(this, new ActionType("safebuckets-fluid-safe", true, true, true, "BlockChangeAction", "set safe"));
Prism.getActionRegistry().registerCustomAction(this, new ActionType("safebuckets-fluid-unsafe", true, true, true, "BlockChangeAction", "set unsafe"));
// pm.registerEvents(l, (Prism) prpl);
eventLogger.enablePrism();
log.log(Level.INFO, "[" + getDescription().getName() + "] Logging events using Prism.");
} catch (Exception e) {
}
}

Plugin lbpl = pm.getPlugin("LogBlock");
if (lbpl != null) {
eventLogger.enableLogBlock((LogBlock) lbpl);
log.log(Level.INFO, "[" + getDescription().getName() + "] Logging events using LogBlock.");
}

// TODO Support both LogBlock and Prism
try {
lbConsumer = ((LogBlock) pm.getPlugin("LogBlock")).getConsumer();
} catch (Exception e) {
log.log(Level.WARNING, "LogBlock could not be loaded!");
if (!eventLogger.canLog()) {
log.log(Level.WARNING, "[" + getDescription().getName() + "] Neither Prism nor LogBlock found - logging disabled!");
}

log.log(Level.INFO, "[" + getDescription().getName() + "] " + getDescription().getVersion() + " enabled.");
Expand Down Expand Up @@ -347,47 +369,44 @@ public boolean isBlockSafe(Block block) {
return false;
}

public int setBlockSafe(Block block, boolean safe, boolean log, String pName) {
public int setBlockSafe(Block block, boolean safe, boolean log) {
return setBlockSafe(block, safe, log, null);
}

public int setBlockSafe(Block block, boolean safe, boolean log, Player player) {
flag = true;
int changed = 0;
if (safe) {
if (block.getData() == 0) {
if (block.getType() == Material.WATER || block.getType() == Material.STATIONARY_WATER) {
if (log && lbConsumer != null) {
lbConsumer.queueBlockReplace(pName, block.getState(), 9, (byte) 15);
}
BlockState prev = block.getState();
changed += removeChildFlows(block, 0);
block.setType(Material.STATIONARY_WATER);
block.setData((byte) 15);
eventLogger.logEvent(log, player, prev, block);
}
if (block.getType() == Material.LAVA || block.getType() == Material.STATIONARY_LAVA) {
if (log && lbConsumer != null) {
lbConsumer.queueBlockReplace(pName, block.getState(), 11, (byte) 15);
}
BlockState prev = block.getState();
changed += removeChildFlows(block, 0);
block.setType(Material.STATIONARY_LAVA);
block.setData((byte) 15);
eventLogger.logEvent(log, player, prev, block);
}
}
}
else {
if (isBlockSafe(block)) {
if (log && lbConsumer != null) {
lbConsumer.queueBlockReplace(pName, block.getState(), getStationaryMaterial(block.getType()) == Material.STATIONARY_WATER ? 8 : 10, (byte) 0);
}
BlockState prev = block.getState();
changed++;
block.setType(getFlowingMaterial(block.getType()));
block.setData((byte) 0);
eventLogger.logEvent(log, player, prev, block);
}
}
flag = false;
return changed;
}

public Consumer getConsumer() {
return lbConsumer;
}

public int removeChildFlows(Block block, int depth) {
if (depth == FLOW_MAX_DEPTH) {
return 0;
Expand Down Expand Up @@ -686,15 +705,15 @@ public int setRegionSafe(Location p1, Location p2, boolean safe, Player issuer)
for (int y = minY; y <= maxY; y++) {
for (int z = minZ; z <= maxZ; z++) {
Block b = world.getBlockAt(x, y, z);
count += setBlockSafe(b, safe, LOG_REGION_FLOW, issuer.getName());
count += setBlockSafe(b, safe, LOG_REGION_FLOW, issuer);
}
}
}
return count;
}

public void queueSafeBlock(Block block, boolean log, String pName) {
blockCache.put(block.getLocation(), new SafeEntry(block.getWorld().getTime(), log, pName));
public void queueSafeBlock(Block block, boolean log, Player player) {
blockCache.put(block.getLocation(), new SafeEntry(block.getWorld().getTime(), log, player));
}

public void registerBlock(Block block, boolean reg) {
Expand Down
Loading

0 comments on commit ce46fdd

Please sign in to comment.