RevertChecker

This commit is contained in:
MistPhizzle 2014-06-26 23:08:41 -04:00
parent 5a9bde656d
commit f22b683b2a
6 changed files with 329 additions and 0 deletions

View file

@ -108,6 +108,19 @@ public class BendingManager implements Runnable {
AirSuction.progressAll();
Fireball.progressAll();
HealingWaters.heal(Bukkit.getServer());
for (Block block : RevertChecker.revertQueue.keySet()) {
// Tools.removeEarthbendedBlockByIndex(block);
// if (Tools.revertBlock(block))
Methods.revertBlock(block);
RevertChecker.revertQueue.remove(block);
}
for (int i : RevertChecker.airRevertQueue.keySet()) {
Methods.revertAirBlock(i);
RevertChecker.airRevertQueue.remove(i);
}
for (Player player : EarthTunnel.instances.keySet()) {
EarthTunnel.progress(player);
}

View file

@ -42,6 +42,8 @@ public class ConfigManager {
plugin.getConfig().addDefault("Properties.Water.NightFactor", 1.5);
config.addDefault("Properties.Earth.RevertEarthbending", true);
config.addDefault("Properties.Earth.SafeRevert", true);
config.addDefault("Properties.Earth.RevertCheckTime", 300000);
plugin.getConfig().addDefault("Properties.Earth.CanBendWithWeapons", true);
plugin.getConfig().addDefault("Properties.Earth.EarthbendableBlocks", earthbendable);

View file

@ -48,6 +48,8 @@ public class Methods {
Methods.plugin = plugin;
}
private static final ItemStack pickaxe = new ItemStack(
Material.DIAMOND_PICKAXE);
public static ConcurrentHashMap<Block, Information> movedearth = new ConcurrentHashMap<Block, Information>();
public static ConcurrentHashMap<Integer, Information> tempair = new ConcurrentHashMap<Integer, Information>();
public static ArrayList<Block> tempnophysics = new ArrayList<Block>();
@ -598,6 +600,137 @@ public class Methods {
}
}
public static void revertAirBlock(int i) {
revertAirBlock(i, false);
}
public static void revertAirBlock(int i, boolean force) {
if (!tempair.containsKey(i))
return;
Information info = tempair.get(i);
Block block = info.getState().getBlock();
if (block.getType() != Material.AIR && !block.isLiquid()) {
if (force || !movedearth.containsKey(block)) {
dropItems(
block,
getDrops(block, info.getState().getType(), info
.getState().getRawData(), pickaxe));
// ItemStack item = new ItemStack(info.getType());
// item.setData(new MaterialData(info.getType(),
// info.getData()));
// block.getWorld().dropItem(block.getLocation(), item);
tempair.remove(i);
} else {
info.setTime(info.getTime() + 10000);
}
return;
} else {
// block.setType(info.getType());
// block.setData(info.getData());
info.getState().update(true);
tempair.remove(i);
}
}
public static boolean revertBlock(Block block) {
byte full = 0x0;
if (movedearth.containsKey(block)) {
Information info = movedearth.get(block);
Block sourceblock = info.getState().getBlock();
if (info.getState().getType() == Material.AIR) {
movedearth.remove(block);
return true;
}
if (block.equals(sourceblock)) {
// verbose("Equals!");
// if (block.getType() == Material.SANDSTONE
// && info.getState().getType() == Material.SAND)
// block.setType(Material.SAND);
info.getState().update(true);
if (EarthColumn.blockInAllAffectedBlocks(sourceblock))
EarthColumn.revertBlock(sourceblock);
if (EarthColumn.blockInAllAffectedBlocks(block))
EarthColumn.revertBlock(block);
EarthColumn.resetBlock(sourceblock);
EarthColumn.resetBlock(block);
movedearth.remove(block);
return true;
}
if (movedearth.containsKey(sourceblock)) {
addTempAirBlock(block);
movedearth.remove(block);
return true;
// verbose("Block: " + block);
// verbose("Sourceblock: " + sourceblock);
// verbose("StartBlock: " + startblock);
// if (startblock != null) {
// if (startblock.equals(sourceblock)) {
// sourceblock.setType(info.getType());
// sourceblock.setData(info.getData());
// if (adjacentToThreeOrMoreSources(block)) {
// block.setType(Material.WATER);
// block.setData(full);
// } else {
// block.setType(Material.AIR);
// }
// movedearth.get(startblock).setInteger(10);
// if (EarthColumn
// .blockInAllAffectedBlocks(sourceblock))
// EarthColumn.revertBlock(sourceblock);
// if (EarthColumn.blockInAllAffectedBlocks(block))
// EarthColumn.revertBlock(block);
// EarthColumn.resetBlock(sourceblock);
// EarthColumn.resetBlock(block);
// movedearth.remove(block);
// return true;
// }
//
// } else {
// startblock = block;
// }
// revertBlock(sourceblock, startblock, true);
}
if (sourceblock.getType() == Material.AIR || sourceblock.isLiquid()) {
// sourceblock.setType(info.getType());
// sourceblock.setData(info.getData());
info.getState().update(true);
} else {
// if (info.getType() != Material.AIR) {
// ItemStack item = new ItemStack(info.getType());
// item.setData(new MaterialData(info.getType(), info
// .getData()));
// block.getWorld().dropItem(block.getLocation(), item);
dropItems(
block,
getDrops(block, info.getState().getType(), info
.getState().getRawData(), pickaxe));
// }
}
// if (info.getInteger() != 10) {
if (isAdjacentToThreeOrMoreSources(block)) {
block.setType(Material.WATER);
block.setData(full);
} else {
block.setType(Material.AIR);
}
// }
if (EarthColumn.blockInAllAffectedBlocks(sourceblock))
EarthColumn.revertBlock(sourceblock);
if (EarthColumn.blockInAllAffectedBlocks(block))
EarthColumn.revertBlock(block);
EarthColumn.resetBlock(sourceblock);
EarthColumn.resetBlock(block);
movedearth.remove(block);
}
return true;
}
public static void playFocusWaterEffect(Block block) {
block.getWorld().playEffect(block.getLocation(), Effect.SMOKE, 4, 20);

View file

@ -38,6 +38,7 @@ public class ProjectKorra extends JavaPlugin {
Methods.createBendingPlayer(player.getUniqueId(), player.getName());
}
getServer().getPluginManager().registerEvents(new PKListener(this), this);
getServer().getScheduler().runTaskTimerAsynchronously(this, new RevertChecker(this), 0, 200);
}
@Override

View file

@ -0,0 +1,178 @@
package com.projectkorra.ProjectKorra;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Future;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.bukkit.Chunk;
import org.bukkit.Server;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
public class RevertChecker implements Runnable {
static ConcurrentHashMap<Block, Block> revertQueue = new ConcurrentHashMap<Block, Block>();
static ConcurrentHashMap<Integer, Integer> airRevertQueue = new ConcurrentHashMap<Integer, Integer>();
private Future<ArrayList<Chunk>> returnFuture;
// static ConcurrentHashMap<Block, Material> movedEarthQueue = new
// ConcurrentHashMap<Block, Material>();
static ConcurrentHashMap<Chunk, Chunk> chunks = new ConcurrentHashMap<Chunk, Chunk>();
private ProjectKorra plugin;
private static final boolean safeRevert = ProjectKorra.plugin.getConfig().getBoolean("Properties.Earth.SafeRevert");
private long time;
public RevertChecker(ProjectKorra bending) {
plugin = bending;
}
private class getOccupiedChunks implements Callable<ArrayList<Chunk>> {
private Server server;
public getOccupiedChunks(Server server) {
this.server = server;
}
@Override
public ArrayList<Chunk> call() throws Exception {
ArrayList<Chunk> chunks = new ArrayList<Chunk>();
Player[] players = server.getOnlinePlayers();
for (Player player : players) {
Chunk chunk = player.getLocation().getChunk();
if (!chunks.contains(chunk))
chunks.add(chunk);
}
return chunks;
}
}
public void run() {
time = System.currentTimeMillis();
if (plugin.getConfig().getBoolean("Properties.Earth.RevertEarthbending")) {
// ArrayList<Chunk> chunks = new ArrayList<Chunk>();
// Player[] players = plugin.getServer().getOnlinePlayers();
//
// for (Player player : players) {
// Chunk chunk = player.getLocation().getChunk();
// if (!chunks.contains(chunk))
// chunks.add(chunk);
// }
try {
// Tools.verbose("Calling future at t="
// + System.currentTimeMillis());
returnFuture = plugin
.getServer()
.getScheduler()
.callSyncMethod(plugin,
new getOccupiedChunks(plugin.getServer()));
ArrayList<Chunk> chunks = returnFuture.get();
// Tools.verbose("Future called, t=" +
// System.currentTimeMillis());
Map<Block, Information> earth = new HashMap<Block, Information>();
earth.putAll(Methods.movedearth);
for (Block block : earth.keySet()) {
if (revertQueue.containsKey(block))
continue;
boolean remove = true;
Information info = earth.get(block);
if (time < info.getTime() + ProjectKorra.plugin.getConfig().getLong("Properties.Earth.RevertCheckTime")
|| (chunks.contains(block.getChunk()) && safeRevert)) {
remove = false;
}
if (remove) {
addToRevertQueue(block);
}
}
Map<Integer, Information> air = new HashMap<Integer, Information>();
air.putAll(Methods.tempair);
for (Integer i : air.keySet()) {
if (airRevertQueue.containsKey(i))
continue;
boolean remove = true;
Information info = air.get(i);
Block block = info.getBlock();
if (time < info.getTime() + ProjectKorra.plugin.getConfig().getLong("Properties.Earth.RevertCheckTime")
|| (chunks.contains(block.getChunk()) && safeRevert)) {
remove = false;
}
if (remove) {
addToAirRevertQueue(i);
}
}
} catch (Exception e) {
e.printStackTrace();
}
// for (Block block : Tools.tempearthblocks.keySet()) {
// if (revertQueue.containsKey(block))
// continue;
// boolean remove = true;
//
// Block index = Tools.tempearthblocks.get(block);
// if (Tools.movedearth.containsKey(index)) {
// Information info = Tools.movedearth.get(index);
// if (time < info.getTime() + ConfigManager.revertchecktime
// || (chunks.contains(index.getChunk()) && safeRevert)) {
// remove = false;
// }
// }
//
// if (remove)
// addToRevertQueue(block);
//
// }
// for (Block block : Tools.movedearth.keySet()) {
// if (movedEarthQueue.containsKey(block))
// continue;
// Information info = Tools.movedearth.get(block);
// if (time >= info.getTime() + ConfigManager.revertchecktime) {
// // if (Tools.tempearthblocks.containsKey(info.getBlock()))
// // Tools.verbose("PROBLEM!");
// // block.setType(info.getType());
// // Tools.movedearth.remove(block);
// addToMovedEarthQueue(block, info.getType());
// }
// }
// Tools.writeToLog("Still " + Tools.tempearthblocks.size()
// + " remaining.");
}
}
private void addToAirRevertQueue(int i) {
if (!airRevertQueue.containsKey(i))
airRevertQueue.put(i, i);
}
// void addToMovedEarthQueue(Block block, Material type) {
// if (!movedEarthQueue.containsKey(block))
// movedEarthQueue.put(block, type);
//
// }
void addToRevertQueue(Block block) {
if (!revertQueue.containsKey(block))
revertQueue.put(block, block);
}
}

View file

@ -16,6 +16,8 @@ Properties:
NightFactor: 1.5
Earth:
RevertEarthbending: true
SafeRevert: true
RevertCheckTime: 300000
CanBendWithWeapons: true
EarthbendableBlocks:
- STONE