2012-01-20 18:17:49 +00:00
|
|
|
package de.jaschastarke.minecraft.limitedcreative;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import org.bukkit.Location;
|
|
|
|
import org.bukkit.Material;
|
|
|
|
import org.bukkit.block.Block;
|
|
|
|
import org.bukkit.enchantments.Enchantment;
|
2013-01-16 11:33:14 +00:00
|
|
|
import org.bukkit.entity.Item;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.event.EventHandler;
|
|
|
|
import org.bukkit.event.Listener;
|
|
|
|
import org.bukkit.event.entity.ItemSpawnEvent;
|
2012-01-27 10:06:07 +00:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
2013-08-29 12:19:59 +00:00
|
|
|
import org.bukkit.scheduler.BukkitRunnable;
|
2012-01-20 18:17:49 +00:00
|
|
|
|
2013-01-16 11:33:14 +00:00
|
|
|
import de.jaschastarke.bukkit.lib.CoreModule;
|
2012-01-20 18:17:49 +00:00
|
|
|
|
2013-01-18 21:51:08 +00:00
|
|
|
public class FeatureBlockItemSpawn extends CoreModule<LimitedCreative> implements Listener {
|
|
|
|
public FeatureBlockItemSpawn(LimitedCreative plugin) {
|
2013-01-16 11:33:14 +00:00
|
|
|
super(plugin);
|
|
|
|
}
|
2013-08-29 12:19:59 +00:00
|
|
|
private CleanUp cleanup = new CleanUp();
|
|
|
|
public final static long TICK_OFFSET = 1;
|
2012-01-20 18:17:49 +00:00
|
|
|
|
|
|
|
private List<BlockItemDrop> list = new ArrayList<BlockItemDrop>();
|
|
|
|
|
|
|
|
public boolean isBlocked(Location l, Material type) {
|
2013-08-12 13:50:05 +00:00
|
|
|
if (isDebug())
|
|
|
|
getLog().debug("Checking ItemBlocked: " + l.toString() + " - " + type.toString());
|
2012-01-20 18:17:49 +00:00
|
|
|
for (BlockItemDrop block : list) {
|
2013-08-12 13:50:05 +00:00
|
|
|
if (isDebug())
|
|
|
|
getLog().debug(" - " + block.toString());
|
|
|
|
if (block.getLocation().equals(l) && (block.getType() == null || block.getType().equals(type))) {
|
|
|
|
if (isDebug())
|
|
|
|
getLog().debug(" blocked!");
|
2012-01-20 18:17:49 +00:00
|
|
|
return true;
|
2013-08-12 13:50:05 +00:00
|
|
|
}
|
2012-01-20 18:17:49 +00:00
|
|
|
}
|
2013-08-29 12:19:59 +00:00
|
|
|
if (isDebug())
|
2013-08-12 13:50:05 +00:00
|
|
|
getLog().debug(" allowed");
|
2012-01-20 18:17:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-08-29 12:19:59 +00:00
|
|
|
|
|
|
|
private void scheduleCleanUp() {
|
|
|
|
if (cleanup.maxTime == 0) { // if not scheduled yet
|
|
|
|
cleanup.maxTime = System.currentTimeMillis();
|
2015-04-10 22:03:42 +00:00
|
|
|
cleanup.runTaskLater(plugin, TICK_OFFSET);
|
2012-01-20 18:17:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class BlockItemDrop {
|
|
|
|
public BlockItemDrop(Location l, Material type) {
|
|
|
|
this.l = l;
|
|
|
|
this.type = type;
|
|
|
|
this.timestamp = System.currentTimeMillis();
|
|
|
|
}
|
|
|
|
private Location l;
|
|
|
|
private Material type;
|
|
|
|
private long timestamp;
|
|
|
|
public Location getLocation() {
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
public Material getType() {
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
public long getTimestamp() {
|
|
|
|
return timestamp;
|
|
|
|
}
|
2013-08-12 13:50:05 +00:00
|
|
|
public String toString() {
|
|
|
|
return Long.toString(timestamp) + ": " + l.toString() + (type != null ? " - " + type.toString() : "");
|
|
|
|
}
|
2012-01-20 18:17:49 +00:00
|
|
|
}
|
|
|
|
|
2013-01-16 11:33:14 +00:00
|
|
|
public void block(Block block, Player player) {
|
|
|
|
if (player.getItemInHand().containsEnchantment(Enchantment.SILK_TOUCH)) {
|
2012-01-27 10:06:07 +00:00
|
|
|
block(block.getLocation(), block.getType());
|
|
|
|
} else {
|
|
|
|
// doesn't include silktouch
|
2013-01-16 11:33:14 +00:00
|
|
|
for (ItemStack i : block.getDrops(player.getItemInHand())) {
|
2012-01-27 10:06:07 +00:00
|
|
|
block(block.getLocation(), i.getType());
|
|
|
|
}
|
2012-01-20 18:17:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void block(Block block) {
|
2013-08-29 12:19:59 +00:00
|
|
|
block(block.getLocation());
|
2012-01-20 18:17:49 +00:00
|
|
|
}
|
2013-08-12 13:50:05 +00:00
|
|
|
public void block(Location l) {
|
|
|
|
list.add(new BlockItemDrop(l, null));
|
2013-08-29 12:19:59 +00:00
|
|
|
scheduleCleanUp();
|
2013-08-12 13:50:05 +00:00
|
|
|
}
|
2012-01-20 18:17:49 +00:00
|
|
|
public void block(Location l, Material type) {
|
|
|
|
list.add(new BlockItemDrop(l, type));
|
2013-08-29 12:19:59 +00:00
|
|
|
scheduleCleanUp();
|
2012-01-20 18:17:49 +00:00
|
|
|
}
|
2013-01-16 11:33:14 +00:00
|
|
|
|
2013-10-01 13:57:04 +00:00
|
|
|
@EventHandler(ignoreCancelled = true)
|
2013-01-16 11:33:14 +00:00
|
|
|
public void onItemSpawn(ItemSpawnEvent event) {
|
|
|
|
if (event.getEntity() instanceof Item) {
|
|
|
|
if (this.isBlocked(event.getLocation().getBlock().getLocation(), ((Item) event.getEntity()).getItemStack().getType())) {
|
|
|
|
event.setCancelled(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-29 12:19:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Don't default Plugin-debug to this mod. Because it is too spammy.
|
2013-09-14 11:53:15 +00:00
|
|
|
*/
|
2013-08-29 12:19:59 +00:00
|
|
|
public boolean isDebug() {
|
|
|
|
return debug;
|
2013-09-14 11:53:15 +00:00
|
|
|
}
|
2013-08-29 12:19:59 +00:00
|
|
|
|
|
|
|
private class CleanUp extends BukkitRunnable {
|
|
|
|
public long maxTime = 0;
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2013-09-22 09:04:09 +00:00
|
|
|
if (plugin.isDebug())
|
|
|
|
plugin.getLog().debug("Scheduler: Synchronous Task run: Cleanup");
|
2013-08-29 12:19:59 +00:00
|
|
|
Iterator<BlockItemDrop> i = list.iterator();
|
|
|
|
while (i.hasNext()) {
|
|
|
|
BlockItemDrop block = i.next();
|
|
|
|
if (block.getTimestamp() <= maxTime) {
|
|
|
|
if (isDebug())
|
|
|
|
getLog().debug("Removing outdated BlokItemDrop: " + block.toString());
|
|
|
|
i.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
maxTime = 0;
|
|
|
|
if (list.size() > 0)
|
|
|
|
scheduleCleanUp();
|
|
|
|
}
|
|
|
|
}
|
2012-01-20 18:17:49 +00:00
|
|
|
}
|