QUOTE: Be the change, make a difference.

AutoPlacerModule.java - poppy - A feature-rich Minecraft plugin which enhances gaming experience

poppy

A feature-rich Minecraft plugin which enhances gaming experience
git clone git@soophie.de:/srv/git/poppy
log | files | refs | readme

AutoPlacerModule.java (2457B)


      1 package poppy.modules;
      2 
      3 import java.util.Set;
      4 
      5 import org.bukkit.Bukkit;
      6 import org.bukkit.Material;
      7 import org.bukkit.Tag;
      8 import org.bukkit.block.Block;
      9 import org.bukkit.block.BlockState;
     10 import org.bukkit.block.Dispenser;
     11 import org.bukkit.block.data.Directional;
     12 import org.bukkit.event.EventHandler;
     13 import org.bukkit.event.Listener;
     14 import org.bukkit.event.block.BlockDispenseEvent;
     15 import org.bukkit.inventory.ItemStack;
     16 import org.bukkit.inventory.meta.BlockStateMeta;
     17 import org.bukkit.inventory.meta.ItemMeta;
     18 import org.bukkit.plugin.Plugin;
     19 
     20 import poppy.Utils;
     21 
     22 public class AutoPlacerModule implements Listener
     23 {
     24 	private Plugin plugin;
     25 
     26 	public AutoPlacerModule(Plugin plugin)
     27 	{
     28 		this.plugin = plugin;
     29 	}
     30 
     31 	@EventHandler
     32 	public void onBlockDispenseEvent(BlockDispenseEvent event)
     33 	{
     34 		final Block block = event.getBlock();
     35 		final BlockState blockState = block.getState();
     36 		final ItemStack item = event.getItem();
     37 
     38 		if(blockState instanceof Dispenser)
     39 		{
     40 			Dispenser dispenser = (Dispenser) blockState;
     41 
     42 			if(dispenser.getCustomName() != null && dispenser.getCustomName().toLowerCase().equals("placer"))
     43 			{
     44 				event.setCancelled(true);
     45 
     46 				if(block.getBlockData() instanceof Directional)
     47 				{
     48 					final Directional direction = (Directional) dispenser.getBlockData();
     49 					final Block faceBlock = dispenser.getBlock().getRelative(direction.getFacing());
     50 
     51 					if(item.getType().isBlock() && Utils.isAir(faceBlock.getType()) && !isShulkerboxContent(item))
     52 					{
     53 						faceBlock.setType(item.getType());
     54 						removeSnapshotItem(dispenser, item.getType(), 1);
     55 					}
     56 
     57 					if(Tag.ITEMS_HOES.isTagged(item.getType()) && faceBlock.getType() == Material.COARSE_DIRT)
     58 					{
     59 						faceBlock.setType(Material.DIRT);
     60 					}
     61 				}
     62 
     63 				if(isShulkerboxContent(item))
     64 				{
     65 					event.setCancelled(false);
     66 				}
     67 			}
     68 		}
     69 	}
     70 
     71 	public boolean isShulkerboxContent(ItemStack itemStack)
     72 	{
     73 		final ItemMeta itemMeta = itemStack.getItemMeta();
     74 		final Set<Material> shulkerboxes = Tag.SHULKER_BOXES.getValues();
     75 
     76 		for(Material material : shulkerboxes)
     77 		{
     78 			if(itemStack.getType().equals(material) && itemMeta instanceof BlockStateMeta)
     79 			{
     80 				return true;
     81 			}
     82 		}
     83 		return false;
     84 	}
     85 	
     86 	public void removeSnapshotItem(Dispenser dispenser, Material material, int amount)
     87 	{
     88 		Bukkit.getScheduler().runTaskLater(plugin, () -> {
     89 			dispenser.getSnapshotInventory().removeItem(new ItemStack(material, 1));
     90 			dispenser.update();
     91 		}, 1);
     92 	}
     93 }