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;
|
2012-01-20 18:17:49 +00:00
|
|
|
|
2013-01-16 11:33:14 +00:00
|
|
|
import de.jaschastarke.bukkit.lib.Core;
|
|
|
|
import de.jaschastarke.bukkit.lib.CoreModule;
|
2012-01-20 18:17:49 +00:00
|
|
|
|
2013-01-16 11:33:14 +00:00
|
|
|
public class FeatureBlockItemSpawn extends CoreModule implements Listener {
|
|
|
|
public FeatureBlockItemSpawn(Core plugin) {
|
|
|
|
super(plugin);
|
|
|
|
}
|
2012-01-27 10:06:07 +00:00
|
|
|
public final static long TIME_OFFSET = 250;
|
2012-01-20 18:17:49 +00:00
|
|
|
|
|
|
|
private List<BlockItemDrop> list = new ArrayList<BlockItemDrop>();
|
|
|
|
|
|
|
|
public boolean isBlocked(Location l, Material type) {
|
|
|
|
cleanup();
|
|
|
|
for (BlockItemDrop block : list) {
|
2013-01-16 11:33:14 +00:00
|
|
|
if (block.getLocation().equals(l) && (block.getType() == null || block.getType().equals(type)))
|
2012-01-20 18:17:49 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
private void cleanup() {
|
|
|
|
Iterator<BlockItemDrop> i = list.iterator();
|
|
|
|
while (i.hasNext()) {
|
|
|
|
BlockItemDrop block = i.next();
|
|
|
|
if (block.getTimestamp() < System.currentTimeMillis() - TIME_OFFSET)
|
|
|
|
i.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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) {
|
|
|
|
block(block, null);
|
|
|
|
}
|
|
|
|
public void block(Location l, Material type) {
|
|
|
|
list.add(new BlockItemDrop(l, type));
|
|
|
|
}
|
2013-01-16 11:33:14 +00:00
|
|
|
|
|
|
|
@EventHandler
|
|
|
|
public void onItemSpawn(ItemSpawnEvent event) {
|
|
|
|
if (event.isCancelled())
|
|
|
|
return;
|
|
|
|
if (event.getEntity() instanceof Item) {
|
|
|
|
if (this.isBlocked(event.getLocation().getBlock().getLocation(), ((Item) event.getEntity()).getItemStack().getType())) {
|
|
|
|
event.setCancelled(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-20 18:17:49 +00:00
|
|
|
}
|