A feature-rich Minecraft plugin which enhances gaming experience
git clone git://soophie.de/poppy
blob: 1b150cbe7464e3cbab7ee523a9bb179ca0c7628d (4790 bytes)[doc][blame][raw]
1
package de.typable.minecrafthub.event;3
import java.util.ArrayList;4
import java.util.Iterator;5
import java.util.List;6
import java.util.ListIterator;8
import org.bukkit.Bukkit;9
import org.bukkit.block.Block;10
import org.bukkit.block.BlockFace;11
import org.bukkit.block.Dropper;12
import org.bukkit.entity.Entity;13
import org.bukkit.entity.ItemFrame;14
import org.bukkit.event.EventHandler;15
import org.bukkit.event.Listener;16
import org.bukkit.event.inventory.InventoryMoveItemEvent;17
import org.bukkit.inventory.Inventory;18
import org.bukkit.inventory.ItemStack;19
import org.bukkit.inventory.Recipe;20
import org.bukkit.inventory.ShapedRecipe;21
import org.bukkit.inventory.ShapelessRecipe;23
import de.typable.minecrafthub.util.Util;26
public class AutoWorkbenchListener implements Listener27
{28
@EventHandler29
public void onItemMove(InventoryMoveItemEvent event)30
{31
Inventory source = event.getSource();32
Inventory target = event.getDestination();34
if(source.getHolder() instanceof Dropper)35
{36
Dropper dropper = (Dropper) source.getHolder();38
ItemFrame frame = getItemFrame(dropper.getBlock());40
if(frame == null)41
{42
return;43
}45
ItemStack item = frame.getItem();47
Recipe recipe = getCraftingRecipe(item, source, target);49
if(recipe != null)50
{51
if(recipe instanceof ShapelessRecipe)52
{53
ShapelessRecipe shapless = (ShapelessRecipe) recipe;55
target.addItem(shapless.getResult());56
}58
if(recipe instanceof ShapedRecipe)59
{60
ShapedRecipe shaped = (ShapedRecipe) recipe;62
target.addItem(shaped.getResult());63
}64
}66
event.setCancelled(true);67
}68
}70
private Recipe getCraftingRecipe(ItemStack item, Inventory source, Inventory target)71
{72
if(Util.isEmpty(item))73
{74
return null;75
}77
Iterator<Recipe> iterator = Bukkit.recipeIterator();79
while(iterator.hasNext())80
{81
Recipe recipe = iterator.next();83
if(item.isSimilar(recipe.getResult()))84
{85
List<ItemStack> ingredients = null;87
if(recipe instanceof ShapelessRecipe)88
{89
ShapelessRecipe shapless = (ShapelessRecipe) recipe;91
ingredients = shapless.getIngredientList();92
}94
if(recipe instanceof ShapedRecipe)95
{96
ShapedRecipe shaped = (ShapedRecipe) recipe;98
ingredients = new ArrayList<>();100
for(ItemStack value : shaped.getIngredientMap().values())101
{102
if(ingredients.isEmpty())103
{104
if(!Util.isEmpty(value))105
{106
ingredients.add(value);107
}108
}109
else110
{111
appendUniqueIngredient(ingredients, value);112
}113
}114
}116
if(ingredients == null)117
{118
continue;119
}121
if(!hasIngredients(source, ingredients))122
{123
continue;124
}126
if(Util.isInventoryFull(target, recipe.getResult()))127
{128
return null;129
}131
removeIngredients(ingredients, source);133
return recipe;134
}135
}137
return null;138
}140
private void removeIngredients(List<ItemStack> ingredients, Inventory inventory)141
{142
for(ItemStack ingredient : ingredients)143
{144
if(Util.isEmpty(ingredient))145
{146
continue;147
}149
int amount = ingredient.getAmount();151
ListIterator<ItemStack> iterator = inventory.iterator();153
while(iterator.hasNext())154
{155
ItemStack item = iterator.next();157
if(Util.isEmpty(item))158
{159
continue;160
}162
if(ingredient.isSimilar(item))163
{164
if(item.getAmount() > amount)165
{166
item.setAmount(item.getAmount() - amount);167
amount = 0;169
break;170
}172
if(item.getAmount() == amount)173
{174
amount = 0;175
inventory.removeItem(item);177
break;178
}180
if(item.getAmount() < amount)181
{182
amount -= item.getAmount();183
inventory.removeItem(item);184
}185
}186
}187
}188
}190
private void appendUniqueIngredient(List<ItemStack> ingredients, ItemStack item)191
{192
for(ItemStack ingredient : ingredients)193
{194
if(ingredient.isSimilar(item))195
{196
ingredient.setAmount(ingredient.getAmount() + item.getAmount());198
return;199
}200
}202
if(!Util.isEmpty(item))203
{204
ingredients.add(item);205
}206
}208
private boolean hasIngredients(Inventory inventory, List<ItemStack> ingredients)209
{210
for(ItemStack ingredient : ingredients)211
{212
if(ingredient != null && !inventory.containsAtLeast(ingredient, ingredient.getAmount()))213
{214
return false;215
}216
}218
return true;219
}221
private ItemFrame getItemFrame(Block block)222
{223
for(Entity entity : block.getWorld().getNearbyEntities(block.getLocation(), 2, 2, 2))224
{225
if(entity instanceof ItemFrame)226
{227
ItemFrame frame = (ItemFrame) entity;229
if(frame.getFacing() == BlockFace.UP)230
{231
if(block.getLocation().add(0.5, 1.03125, 0.5).distance(frame.getLocation()) < 0.2D)232
{233
return frame;234
}235
}236
}237
}239
return null;240
}241
}