fix: update README and enhance AutoCrafter
This commit is contained in:
@@ -4,4 +4,5 @@ BUG: Autocrafter nicht von seite möglich
|
||||
IDEA: schere verschiedene versionen pferde/mule/cats/schafe/Axolotl/tropical etc?
|
||||
|
||||
TO RELEASE: waterbottle + sponge = empty bottle -> Warum nochmal?
|
||||
TO RELEASE: Autocrafter
|
||||
TO RELEASE: Autocrafter
|
||||
TO RELEASE: jukeboxes können mit Redstone gesteuert werden
|
||||
@@ -4,7 +4,9 @@ import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.BlockFace;
|
||||
import org.bukkit.block.Dispenser;
|
||||
import org.bukkit.block.data.Directional;
|
||||
@@ -18,6 +20,7 @@ import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.Recipe;
|
||||
import org.bukkit.inventory.ShapedRecipe;
|
||||
import org.bukkit.inventory.ShapelessRecipe;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
import org.bukkit.util.Vector;
|
||||
|
||||
public class AutoCrafter implements Listener {
|
||||
@@ -40,7 +43,7 @@ public class AutoCrafter implements Listener {
|
||||
Dispenser dispenser = (Dispenser) event.getBlock().getState();
|
||||
BlockFace facing = ((Directional) dispenser.getBlockData()).getFacing();
|
||||
Inventory tempInventory = dispenser.getInventory();
|
||||
tempInventory.addItem(event.getItem());
|
||||
|
||||
Collection<ItemFrame> frames = event.getBlock().getLocation().getNearbyEntitiesByType(ItemFrame.class, 1.5);
|
||||
if (frames.toArray().length == 0)
|
||||
return;
|
||||
@@ -48,19 +51,17 @@ public class AutoCrafter implements Listener {
|
||||
for (ItemFrame frame : frames) {
|
||||
if (frame.getItem().getType() == Material.AIR)
|
||||
return;
|
||||
|
||||
event.setCancelled(true);
|
||||
|
||||
String outputItemName = frame.getItem().getType().name();
|
||||
Recipe recipe = findRecipe(Material.matchMaterial(outputItemName));
|
||||
|
||||
if (recipe == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Map to store the required materials and their counts
|
||||
|
||||
Map<Material, Integer> required = new HashMap<>();
|
||||
|
||||
// Collect required materials from the recipe
|
||||
if (recipe instanceof ShapedRecipe) {
|
||||
ShapedRecipe shapedRecipe = (ShapedRecipe) recipe;
|
||||
shapedRecipe.getIngredientMap().values().forEach(item -> {
|
||||
@@ -75,41 +76,44 @@ public class AutoCrafter implements Listener {
|
||||
});
|
||||
}
|
||||
|
||||
// Check if dispenser contains the required materials
|
||||
if (!hasRequiredMaterials(tempInventory, required)) {
|
||||
App.main.log("nut enuff");
|
||||
return;
|
||||
}
|
||||
new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (!hasRequiredMaterials(tempInventory, required)) {
|
||||
return;
|
||||
}
|
||||
for (Map.Entry<Material, Integer> entry : required.entrySet()) {
|
||||
|
||||
// Remove the required items from the dispenser inventory
|
||||
for (Map.Entry<Material, Integer> entry : required.entrySet()) {
|
||||
|
||||
removeMaterialFromInventory(dispenser.getInventory(), entry.getKey(), entry.getValue());
|
||||
}
|
||||
// Drop the output item
|
||||
Item droppedItem = event.getBlock().getWorld().dropItem(event.getBlock().getLocation().add(0.5, 0.5, 0.5),
|
||||
new ItemStack(Material.DIAMOND));
|
||||
Vector velocity = new Vector(facing.getModX(), facing.getModY(), facing.getModZ()).multiply(0.3); // Speed
|
||||
droppedItem.setVelocity(velocity);
|
||||
removeMaterialFromInventory(dispenser.getInventory(), entry.getKey(), entry.getValue());
|
||||
}
|
||||
dispenseThroughFace(event.getBlock(), new ItemStack(Material.matchMaterial(outputItemName)),
|
||||
facing);
|
||||
}
|
||||
|
||||
}.runTaskLater(App.main, 0L);
|
||||
}
|
||||
}
|
||||
|
||||
// Method to check if the dispenser contains enough materials
|
||||
private void dispenseThroughFace(Block dispenser, ItemStack item, BlockFace facing) {
|
||||
Location dispenseLocation = dispenser.getRelative(facing).getLocation().add(0.5, 0.5, 0.5);
|
||||
Item droppedItem = dispenser.getWorld().dropItem(dispenseLocation, item);
|
||||
Vector velocity = new Vector(facing.getModX(), facing.getModY(), facing.getModZ()).multiply(0.3);
|
||||
droppedItem.setVelocity(velocity);
|
||||
}
|
||||
|
||||
private boolean hasRequiredMaterials(Inventory inventory, Map<Material, Integer> required) {
|
||||
// Go through each required material and check if there's enough
|
||||
|
||||
for (Map.Entry<Material, Integer> entry : required.entrySet()) {
|
||||
int materialCount = countMaterialInInventory(inventory, entry.getKey());
|
||||
if (materialCount < entry.getValue()) {
|
||||
// Log the shortage for debugging purposes
|
||||
App.main.log("Not enough " + entry.getKey() + ": required " + entry.getValue() + " but found " + materialCount);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Method to count materials in the inventory
|
||||
private int countMaterialInInventory(Inventory inventory, Material material) {
|
||||
|
||||
int count = 0;
|
||||
for (int i = 0; i < inventory.getSize(); i++) {
|
||||
if (inventory.getItem(i) != null && inventory.getItem(i).getType() == material) {
|
||||
@@ -119,17 +123,16 @@ public class AutoCrafter implements Listener {
|
||||
return count;
|
||||
}
|
||||
|
||||
// Method to remove specific material from the inventory
|
||||
private void removeMaterialFromInventory(Inventory inventory, Material material, int amount) {
|
||||
for (int i = 0; i < inventory.getSize(); i++) {
|
||||
if (inventory.getItem(i) != null && inventory.getItem(i).getType() == material) {
|
||||
int currentAmount = inventory.getItem(i).getAmount();
|
||||
if (currentAmount <= amount) {
|
||||
amount -= currentAmount;
|
||||
inventory.clear(i); // Remove all of this item
|
||||
inventory.clear(i);
|
||||
} else {
|
||||
inventory.getItem(i).setAmount(currentAmount - amount);
|
||||
break; // Stop once we've removed the required amount
|
||||
break;
|
||||
}
|
||||
if (amount <= 0) {
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user