Better sorting

This commit is contained in:
Brianna O'Keefe 2018-10-16 21:40:18 -04:00
parent d6e626d946
commit 384d4d2e5c
2 changed files with 62 additions and 57 deletions

View File

@ -9,7 +9,6 @@ import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.block.Beacon;
import org.bukkit.block.Block;
import org.bukkit.block.Hopper;
import org.bukkit.configuration.ConfigurationSection;
@ -20,7 +19,6 @@ import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* Created by songoda on 3/14/2017.
@ -85,16 +83,16 @@ public class HopHandler {
ItemStack[] is = hopperBlock.getInventory().getContents();
List<Material> materials = new ArrayList<>();
List<Material> blockedMaterials = new ArrayList<>();
for (Module module : hopper.getLevel().getRegisteredModules()) {
// Run Module
module.run(hopper);
// Add banned materials to list.
if (module.getBlockedItems(hopper) == null) continue;
materials.addAll(module.getBlockedItems(hopper));
List<Material> materials = module.getBlockedItems(hopper);
if (materials == null || materials.isEmpty()) continue;
blockedMaterials.addAll(materials);
}
if (hopper.getSyncedBlock() == null) continue;
@ -125,31 +123,30 @@ public class HopHandler {
List<Material> blackList = hopper.getFilter().getBlackList();
for (int i = 0; i < 5; i ++) {
for (int i = 0; i < 5; i++) {
ItemStack it = null;
if (is[i] != null) {
it = is[i].clone();
it.setAmount(1);
}
if(!hopper.getLocation().getBlock().isBlockPowered()) {
if (is[i] != null
&& materials.contains(is[i].getType())) {
i++;
continue;
}
if (is[i] != null
&& !whiteList.isEmpty()
&& !whiteList.contains(it.getType())) {
doBlacklist(hopperBlock, hopper, is[i].clone(), is, amt, i);
} else if (is[i] != null && !blackList.contains(it.getType())) {
int im = addItem(hopperBlock, hopper, b2, is[i], is, amt, i);
if (im != 10)
i = im;
} else if (is[i] != null && blackList.contains(it.getType())) {
doBlacklist(hopperBlock, hopper, is[i].clone(), is, amt, i);
}
if (!hopper.getLocation().getBlock().isBlockPowered()
|| is[i] != null && blockedMaterials.contains(is[i].getType())) {
i++;
continue;
}
if (is[i] != null
&& !whiteList.isEmpty()
&& !whiteList.contains(it.getType())) {
doBlacklist(hopperBlock, hopper, is[i].clone(), is, amt, i);
} else if (is[i] != null && !blackList.contains(it.getType())) {
int im = addItem(hopperBlock, hopper, b2, is[i], is, amt, i);
if (im != 10)
i = im;
} else if (is[i] != null && blackList.contains(it.getType())) {
doBlacklist(hopperBlock, hopper, is[i].clone(), is, amt, i);
}
}
}
} catch (Exception e) {
@ -212,14 +209,14 @@ public class HopHandler {
newItem.setAmount(amt);
if (b2.getType().equals(Material.ENDER_CHEST)) {
OfflinePlayer op = Bukkit.getOfflinePlayer(hopper.getPlacedBy());
if (op.isOnline() && canMove(op.getPlayer().getEnderChest(), newItem, amt)) {
if (!ovoid.contains(it.getType())) {
op.getPlayer().getEnderChest().addItem(newItem);
}
isS[place] = is;
hopperBlock.getInventory().setContents(isS);
OfflinePlayer op = Bukkit.getOfflinePlayer(hopper.getPlacedBy());
if (op.isOnline() && canMove(op.getPlayer().getEnderChest(), newItem, amt)) {
if (!ovoid.contains(it.getType())) {
op.getPlayer().getEnderChest().addItem(newItem);
}
isS[place] = is;
hopperBlock.getInventory().setContents(isS);
}
} else {
InventoryHolder ih = (InventoryHolder) b2.getState();
if (!canMove(ih.getInventory(), newItem, amt) || b2.getType() == Material.BREWING_STAND) {

View File

@ -49,7 +49,6 @@ public class ModuleAutoCrafting implements Module {
ingredientMap = new ArrayList<>(((ShapedRecipe) recipe).getIngredientMap().values());
if (hopperBlock.getInventory().getSize() == 0) continue;
Map<Material, Integer> items = new HashMap<>();
for (ItemStack item : ingredientMap) {
if (!items.containsKey(item.getType())) {
@ -60,14 +59,33 @@ public class ModuleAutoCrafting implements Module {
}
for (Material material : items.keySet()) {
int amt = 0;
ItemStack item = new ItemStack(material, items.get(material));
if (!hopperBlock.getInventory().contains(item)) {
for (ItemStack i : hopperBlock.getInventory().getContents()) {
if (i == null) continue;
if (i.getType() != material) continue;
amt += i.getAmount();
}
if (amt < item.getAmount()) {
continue main;
}
}
main2:
for (Material material : items.keySet()) {
ItemStack item = new ItemStack(material, items.get(material));
hopperBlock.getInventory().removeItem(item);
int amtRemoved = 0;
ItemStack toRemove = new ItemStack(material, items.get(material));
for (ItemStack i : hopperBlock.getInventory().getContents()) {
if (i == null || i.getType() != material) continue;
if (toRemove.getAmount() - amtRemoved <= i.getAmount()) {
toRemove.setAmount(toRemove.getAmount() - amtRemoved);
hopperBlock.getInventory().removeItem(toRemove);
continue main2;
} else {
amtRemoved += i.getAmount();
hopperBlock.getInventory().removeItem(i);
}
}
}
hopperBlock.getInventory().addItem(recipe.getResult());
}
@ -82,13 +100,20 @@ public class ModuleAutoCrafting implements Module {
if (!cachedRecipes.containsKey(material)) {
for (Recipe recipe : Bukkit.getServer().getRecipesFor(new ItemStack(material))) {
if (!(recipe instanceof ShapedRecipe)) continue;
cachedRecipes.put(material, recipe);
}
} else {
for (ItemStack itemStack : ((ShapedRecipe) cachedRecipes.get(material)).getIngredientMap().values()) {
if (itemStack == null) continue;
materials.add(itemStack.getType());
Recipe recipe = cachedRecipes.get(material);
if (recipe instanceof ShapedRecipe) {
for (ItemStack itemStack : ((ShapedRecipe) recipe).getIngredientMap().values()) {
if (itemStack == null) continue;
materials.add(itemStack.getType());
}
} else if (recipe instanceof ShapelessRecipe) {
for (ItemStack itemStack : ((ShapelessRecipe) recipe).getIngredientList()) {
if (itemStack == null) continue;
materials.add(itemStack.getType());
}
}
}
}
@ -100,23 +125,6 @@ public class ModuleAutoCrafting implements Module {
return EpicHoppersPlugin.getInstance().getLocale().getMessage("interface.hopper.crafting", true);
}
private List<ItemStack> stackItems(List<ItemStack> items) {
Map<Material, Integer> materials = new HashMap<>();
for (ItemStack itemStack : items) {
if (itemStack == null) continue;
if (materials.containsKey(itemStack.getType())) {
materials.put(itemStack.getType(), materials.get(itemStack.getType()) + itemStack.getAmount());
continue;
}
materials.put(itemStack.getType(), itemStack.getAmount());
}
List<ItemStack> stacked = new ArrayList<>();
for (Map.Entry<Material, Integer> entry : materials.entrySet()) {
stacked.add(new ItemStack(entry.getKey(), entry.getValue()));
}
return stacked;
}
private boolean canMove(Inventory inventory, ItemStack item) {
try {
if (inventory.firstEmpty() != -1) return true;