GAME: Play the number guessing game -> PLAY NOW

Added AutoWorkbench - poppy - A feature-rich Minecraft plugin which enhances gaming experience

poppy

A feature-rich Minecraft plugin which enhances gaming experience
git clone git://192.168.2.2/poppy
Log | Files | Refs | README

commit 4ea1a501485c57f509ffc631d5b2370380c578f3
parent 960ec323e91e6d3129de29c93cbb69e6fe0a7abb
Author: typable <typable.dev@gmail.com>
Date:   Mon,  7 Dec 2020 23:27:55 +0100

Added AutoWorkbench
Diffstat:
Msrc/de/typable/minecrafthub/Main.java | 5+++++
Asrc/de/typable/minecrafthub/event/AutoWorkbenchListener.java | 241+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/de/typable/minecrafthub/event/ChairListener.java | 7+++++++
Msrc/de/typable/minecrafthub/event/DoubleDoorListener.java | 22++++------------------
Msrc/de/typable/minecrafthub/event/EventListener.java | 10+++++++---
Asrc/de/typable/minecrafthub/util/Util.java | 34++++++++++++++++++++++++++++++++++
6 files changed, 298 insertions(+), 21 deletions(-)

diff --git a/src/de/typable/minecrafthub/Main.java b/src/de/typable/minecrafthub/Main.java @@ -13,6 +13,7 @@ import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; import de.typable.minecrafthub.constant.DefaultConstants; +import de.typable.minecrafthub.event.AutoWorkbenchListener; import de.typable.minecrafthub.event.ChairListener; import de.typable.minecrafthub.event.DoubleDoorListener; import de.typable.minecrafthub.event.EventListener; @@ -25,6 +26,7 @@ public class Main extends JavaPlugin private StandbyListener standbyListener; private DoubleDoorListener doubleDoorListener; private ChairListener chairListener; + private AutoWorkbenchListener autoWorkbenchListener; private EventListener eventListener; @Override @@ -40,6 +42,9 @@ public class Main extends JavaPlugin chairListener = new ChairListener(); pluginManager.registerEvents(chairListener, this); + + autoWorkbenchListener = new AutoWorkbenchListener(); + pluginManager.registerEvents(autoWorkbenchListener, this); eventListener = new EventListener(); pluginManager.registerEvents(eventListener, this); diff --git a/src/de/typable/minecrafthub/event/AutoWorkbenchListener.java b/src/de/typable/minecrafthub/event/AutoWorkbenchListener.java @@ -0,0 +1,241 @@ +package de.typable.minecrafthub.event; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; + +import org.bukkit.Bukkit; +import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; +import org.bukkit.block.Dropper; +import org.bukkit.entity.Entity; +import org.bukkit.entity.ItemFrame; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.inventory.InventoryMoveItemEvent; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.Recipe; +import org.bukkit.inventory.ShapedRecipe; +import org.bukkit.inventory.ShapelessRecipe; + +import de.typable.minecrafthub.util.Util; + + +public class AutoWorkbenchListener implements Listener +{ + @EventHandler + public void onItemMove(InventoryMoveItemEvent event) + { + Inventory source = event.getSource(); + Inventory target = event.getDestination(); + + if(source.getHolder() instanceof Dropper) + { + Dropper dropper = (Dropper) source.getHolder(); + + ItemFrame frame = getItemFrame(dropper.getBlock()); + + if(frame == null) + { + return; + } + + ItemStack item = frame.getItem(); + + Recipe recipe = getCraftingRecipe(item, source, target); + + if(recipe != null) + { + if(recipe instanceof ShapelessRecipe) + { + ShapelessRecipe shapless = (ShapelessRecipe) recipe; + + target.addItem(shapless.getResult()); + } + + if(recipe instanceof ShapedRecipe) + { + ShapedRecipe shaped = (ShapedRecipe) recipe; + + target.addItem(shaped.getResult()); + } + } + + event.setCancelled(true); + } + } + + private Recipe getCraftingRecipe(ItemStack item, Inventory source, Inventory target) + { + if(Util.isEmpty(item)) + { + return null; + } + + Iterator<Recipe> iterator = Bukkit.recipeIterator(); + + while(iterator.hasNext()) + { + Recipe recipe = iterator.next(); + + if(item.isSimilar(recipe.getResult())) + { + List<ItemStack> ingredients = null; + + if(recipe instanceof ShapelessRecipe) + { + ShapelessRecipe shapless = (ShapelessRecipe) recipe; + + ingredients = shapless.getIngredientList(); + } + + if(recipe instanceof ShapedRecipe) + { + ShapedRecipe shaped = (ShapedRecipe) recipe; + + ingredients = new ArrayList<>(); + + for(ItemStack value : shaped.getIngredientMap().values()) + { + if(ingredients.isEmpty()) + { + if(!Util.isEmpty(value)) + { + ingredients.add(value); + } + } + else + { + appendUniqueIngredient(ingredients, value); + } + } + } + + if(ingredients == null) + { + continue; + } + + if(!hasIngredients(source, ingredients)) + { + continue; + } + + if(Util.isInventoryFull(target, recipe.getResult())) + { + return null; + } + + removeIngredients(ingredients, source); + + return recipe; + } + } + + return null; + } + + private void removeIngredients(List<ItemStack> ingredients, Inventory inventory) + { + for(ItemStack ingredient : ingredients) + { + if(Util.isEmpty(ingredient)) + { + continue; + } + + int amount = ingredient.getAmount(); + + ListIterator<ItemStack> iterator = inventory.iterator(); + + while(iterator.hasNext()) + { + ItemStack item = iterator.next(); + + if(Util.isEmpty(item)) + { + continue; + } + + if(ingredient.isSimilar(item)) + { + if(item.getAmount() > amount) + { + item.setAmount(item.getAmount() - amount); + amount = 0; + + break; + } + + if(item.getAmount() == amount) + { + amount = 0; + inventory.removeItem(item); + + break; + } + + if(item.getAmount() < amount) + { + amount -= item.getAmount(); + inventory.removeItem(item); + } + } + } + } + } + + private void appendUniqueIngredient(List<ItemStack> ingredients, ItemStack item) + { + for(ItemStack ingredient : ingredients) + { + if(ingredient.isSimilar(item)) + { + ingredient.setAmount(ingredient.getAmount() + item.getAmount()); + + return; + } + } + + if(!Util.isEmpty(item)) + { + ingredients.add(item); + } + } + + private boolean hasIngredients(Inventory inventory, List<ItemStack> ingredients) + { + for(ItemStack ingredient : ingredients) + { + if(ingredient != null && !inventory.containsAtLeast(ingredient, ingredient.getAmount())) + { + return false; + } + } + + return true; + } + + private ItemFrame getItemFrame(Block block) + { + for(Entity entity : block.getWorld().getNearbyEntities(block.getLocation(), 2, 2, 2)) + { + if(entity instanceof ItemFrame) + { + ItemFrame frame = (ItemFrame) entity; + + if(frame.getFacing() == BlockFace.UP) + { + if(block.getLocation().add(0.5, 1.03125, 0.5).distance(frame.getLocation()) < 0.2D) + { + return frame; + } + } + } + } + + return null; + } +} diff --git a/src/de/typable/minecrafthub/event/ChairListener.java b/src/de/typable/minecrafthub/event/ChairListener.java @@ -32,6 +32,8 @@ public class ChairListener implements Listener @EventHandler public void onPlayerInteract(PlayerInteractEvent event) { + Player player = event.getPlayer(); + if(event.getAction() == Action.RIGHT_CLICK_BLOCK) { if(event.getClickedBlock() != null && !event.getPlayer().isSneaking()) @@ -40,6 +42,11 @@ public class ChairListener implements Listener if(isChair(block.getType())) { + if(block.getLocation().add(0.5, 0.5, 0.5).distance(player.getLocation()) > 2) + { + return; + } + if(blockMap.containsKey(block)) { return; diff --git a/src/de/typable/minecrafthub/event/DoubleDoorListener.java b/src/de/typable/minecrafthub/event/DoubleDoorListener.java @@ -11,7 +11,8 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.inventory.ItemStack; + +import de.typable.minecrafthub.util.Util; public class DoubleDoorListener implements Listener @@ -33,12 +34,12 @@ public class DoubleDoorListener implements Listener { if(player.isSneaking()) { - if(isHoldingItem(player.getInventory().getItemInMainHand())) + if(!Util.isEmpty(player.getInventory().getItemInMainHand())) { return; } - if(isHoldingItem(player.getInventory().getItemInOffHand())) + if(!Util.isEmpty(player.getInventory().getItemInOffHand())) { return; } @@ -91,21 +92,6 @@ public class DoubleDoorListener implements Listener } } - private boolean isHoldingItem(ItemStack item) - { - if(item == null) - { - return false; - } - - if(item.getType() == Material.AIR) - { - return false; - } - - return true; - } - private boolean isDoor(Material material) { for(Material door : DOOR_TYPE) diff --git a/src/de/typable/minecrafthub/event/EventListener.java b/src/de/typable/minecrafthub/event/EventListener.java @@ -17,10 +17,12 @@ public class EventListener implements Listener { if(event.getAction() == Action.PHYSICAL) { - if(event.getClickedBlock() != null && event.getClickedBlock() - .getType() == Material.FARMLAND) + if(event.getClickedBlock() != null) { - event.setCancelled(true); + if(event.getClickedBlock().getType() == Material.FARMLAND) + { + event.setCancelled(true); + } } } } @@ -34,6 +36,8 @@ public class EventListener implements Listener @EventHandler public void onChat(AsyncPlayerChatEvent event) { + // FIXME Conversion Exception on '% + String format = ChatColor.WHITE + event.getPlayer().getName() + ": " + ChatColor.GRAY + event .getMessage(); event.setFormat(format); diff --git a/src/de/typable/minecrafthub/util/Util.java b/src/de/typable/minecrafthub/util/Util.java @@ -0,0 +1,34 @@ +package de.typable.minecrafthub.util; + +import org.bukkit.Material; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; + +public class Util +{ + public static boolean isEmpty(ItemStack item) + { + return item == null || item.getType() == Material.AIR; + } + + public static boolean isInventoryFull(Inventory inventory, ItemStack result) + { + for(ItemStack item : inventory.getContents()) + { + if(Util.isEmpty(item)) + { + return false; + } + + if(result.isSimilar(item)) + { + if(result.getAmount() + item.getAmount() <= item.getMaxStackSize()) + { + return false; + } + } + } + + return true; + } +}