Skip to content

Instantly share code, notes, and snippets.

@stevommmm
Created August 28, 2013 20:32
Show Gist options
  • Select an option

  • Save stevommmm/6370872 to your computer and use it in GitHub Desktop.

Select an option

Save stevommmm/6370872 to your computer and use it in GitHub Desktop.
Old slow block rollback code
package com.c45y.slowblock;
import org.bukkit.Material;
import org.bukkit.block.Block;
/**
*
* @author c45y
*/
public class BlockEventNotice {
Block block;
Long time;
String action;
Material material;
public BlockEventNotice(Block block, String action) {
this.block = block;
this.material = block.getType();
this.time = System.currentTimeMillis();
this.action = action;
}
public Block getBlock() {
return this.block;
}
public Material getMaterial() {
return this.material;
}
public Long getTime() {
return this.time;
}
public String getAction() {
return this.action;
}
}
package com.c45y.slowblock;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
import org.bukkit.Material;
import org.bukkit.block.Block;
/**
*
* @author c45y
*/
public class ScoreHandler extends Thread {
/* Our consumer thread.
* Useds a blocking queue to pass jobs off the main bukkit thread,
* this is where we should do math and sql stuff, but make sure to
* finish all jobs before thread is allowed to die.
*/
private Map<Block, Long> scores = new HashMap<Block, Long>();
private LinkedBlockingQueue clq;
private SlowBlock plugin;
public ScoreHandler(SlowBlock plugin, LinkedBlockingQueue clq) {
this.clq = clq;
this.plugin = plugin;
}
@Override
public void run() {
while (true) {
try {
BlockEventNotice action = (BlockEventNotice) this.clq.take();
handleTask(action, true);
} catch (InterruptedException ignore) {
/* Shutting down the thread.
* Drain our thread safe queue to a simple LinkedList
* and process it before we return
*/
if (this.clq.size() > 0) {
LinkedList<BlockEventNotice> queue = new LinkedList() {
};
this.clq.drainTo(queue);
for (int i = 0; i <= queue.size(); i++) {
handleTask((BlockEventNotice) queue.pop(), false);
}
}
return;
}
}
}
private void handleTask(BlockEventNotice entry, boolean wait) {
if (entry != null) {
Block block = entry.getBlock(); // For some reason on break always showing material as AIR
Long time = entry.getTime();
String action = entry.getAction();
Material material = entry.getMaterial();
System.out.println("Block:" + block.getType().name() + " Time:" + time);
System.out.println("Material:" + material.name() + " Action:" + action);
if (wait) { // Sometimes we don't want a sleep timer (like shutdown time)
try {
this.sleep((time + 6000) - System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Material toSet = material;
if (action.equals("place")) {
toSet = Material.AIR;
}
synchronized (plugin.getServer()) {
block.setType(toSet);
}
}
}
}
package com.c45y.slowblock;
import com.sk89q.worldedit.Vector;
import java.util.concurrent.LinkedBlockingQueue;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.plugin.java.JavaPlugin;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.protection.ApplicableRegionSet;
import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import java.lang.String;
import java.util.logging.Level;
import org.bukkit.Location;
import org.bukkit.block.Block;
public class SlowBlock extends JavaPlugin implements Listener {
LinkedBlockingQueue clq = new LinkedBlockingQueue();
ScoreHandler sc = new ScoreHandler(this, clq);
WorldGuardPlugin wgPlugin = null;
public WorldGuardPlugin getWorldGuard() {
if (wgPlugin == null) {
wgPlugin = (WorldGuardPlugin) getServer().getPluginManager().getPlugin("WorldGuard");
if (wgPlugin != null) {
if (!wgPlugin.isEnabled()) {
getPluginLoader().enablePlugin(wgPlugin);
}
} else {
getLogger().log(Level.SEVERE, "Could not load worldguard, disabling");
wgPlugin = null;
}
}
return wgPlugin;
}
@Override
public void onDisable() {
try {
sc.interrupt();
sc.join();
System.out.println("ScoreHandler joined.");
} catch (InterruptedException iex) {
iex.printStackTrace();
}
}
@Override
public void onEnable() {
getServer().getPluginManager().registerEvents(this, this);
/* Manage what we will be listening to */
if (this.getConfig().getBoolean("score.pvp.enabled", true)) {
System.out.println("true");
}
sc.start();
if (sc.isAlive()) {
System.out.println("ScoreHandler started.");
}
}
public void enqueue(BlockEventNotice ben) {
try {
this.clq.put(ben);
} catch (InterruptedException iex) {
iex.printStackTrace();
}
}
public ApplicableRegionSet getRegions(Location l1) {
RegionManager mgr = this.getWorldGuard().getGlobalRegionManager().get(l1.getWorld());
Vector pt = new Vector(l1.getBlockX(), l1.getBlockY(), l1.getBlockZ());
return mgr.getApplicableRegions(pt);
}
public boolean inRegion(Location l1) {
ApplicableRegionSet set = getRegions(l1);
for (ProtectedRegion r : set) {
if (r.getId().endsWith("_slow")) {
return true;
}
}
return false;
}
public boolean isMember(Location l1, String owner) {
ApplicableRegionSet set = getRegions(l1);
for (ProtectedRegion r : set) {
if (r.getOwners().getPlayers().contains(owner)) {
return true;
}
if (r.getMembers().getPlayers().contains(owner)) {
return true;
}
}
return false;
}
/* EVENTS */
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockBreak(BlockBreakEvent event) {
Block b = event.getBlock();
System.out.println(b.getType().name());
Location l = event.getBlock().getLocation();
if (inRegion(l)) {
if (!isMember(l, event.getPlayer().getName())) {
BlockEventNotice ben = new BlockEventNotice(b, "break");
this.enqueue(ben);
}
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockPlace(BlockPlaceEvent event) {
Block b = event.getBlock();
Location l = b.getLocation();
if (inRegion(l)) {
if (!isMember(l, event.getPlayer().getName())) {
BlockEventNotice ben = new BlockEventNotice(b, "place");
this.enqueue(ben);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment