1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-20 03:35:51 +00:00

Merge pull request #1339 from mohkamfer/AutomatedCraftingChamberCraftLast

Enable Automated Crafting Chamber To Craft Last Shaped Recipe
This commit is contained in:
TheBusyBiscuit 2020-01-03 14:42:57 +01:00 committed by GitHub
commit 5b86ef1e7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -68,6 +68,12 @@ public abstract class AutomatedCraftingChamber extends SlimefunItem implements I
return false;
});
}
menu.replaceExistingItem(7, new CustomItem(new ItemStack(Material.CRAFTING_TABLE), "&7Craft Last", "", "&e> Click to craft the last shaped recipe", "&cOnly works with the last one"));
menu.addMenuClickHandler(7, (p, slot, item, action) -> {
tick(b, true);
return false;
});
}
@Override
@ -183,7 +189,7 @@ public abstract class AutomatedCraftingChamber extends SlimefunItem implements I
@Override
public void tick(Block b, SlimefunItem sf, Config data) {
AutomatedCraftingChamber.this.tick(b);
AutomatedCraftingChamber.this.tick(b, false);
}
@Override
@ -193,39 +199,58 @@ public abstract class AutomatedCraftingChamber extends SlimefunItem implements I
});
}
protected void tick(Block b) {
if (BlockStorage.getLocationInfo(b.getLocation(), "enabled").equals("false")) return;
if (ChargableBlock.getCharge(b) < getEnergyConsumption()) return;
protected void tick(Block block, boolean craftLast) {
if (!craftLast && BlockStorage.getLocationInfo(block.getLocation(), "enabled").equals("false")) return;
if (ChargableBlock.getCharge(block) < getEnergyConsumption()) return;
BlockMenu menu = BlockStorage.getInventory(b);
String input = getSerializedInput(block, craftLast);
testInputAgainstRecipes(block, input);
}
private String getSerializedInput(Block block, boolean craftLast) {
BlockMenu menu = BlockStorage.getInventory(block);
StringBuilder builder = new StringBuilder();
int i = 0;
boolean lastIteration = false;
for (int j = 0; j < 9; j++) {
if (i > 0) {
builder.append(" </slot> ");
}
ItemStack item = menu.getItemInSlot(getInputSlots()[j]);
if (item != null && item.getAmount() == 1) return;
if (item != null && item.getAmount() == 1) {
if (craftLast)
lastIteration = true;
else
return "";
}
builder.append(CustomItemSerializer.serialize(item, ItemFlag.MATERIAL, ItemFlag.ITEMMETA_DISPLAY_NAME, ItemFlag.ITEMMETA_LORE));
i++;
}
String input = builder.toString();
// we're only executing the last possible shaped recipe
// we don't want to allow this to be pressed instead of the default timer-based
// execution to prevent abuse and auto clickers
if (craftLast && !lastIteration) return "";
return builder.toString();
}
private void testInputAgainstRecipes(Block block, String input) {
BlockMenu menu = BlockStorage.getInventory(block);
if (SlimefunPlugin.getUtilities().automatedCraftingChamberRecipes.containsKey(input)) {
ItemStack output = SlimefunPlugin.getUtilities().automatedCraftingChamberRecipes.get(input).clone();
if (menu.fits(output, getOutputSlots())) {
menu.pushItem(output, getOutputSlots());
ChargableBlock.addCharge(b, -getEnergyConsumption());
ChargableBlock.addCharge(block, -getEnergyConsumption());
for (int j = 0; j < 9; j++) {
if (menu.getItemInSlot(getInputSlots()[j]) != null) menu.replaceExistingItem(getInputSlots()[j], InvUtils.decreaseItem(menu.getItemInSlot(getInputSlots()[j]), 1));
}
}
}
}
}