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:
commit
5b86ef1e7e
@ -68,6 +68,12 @@ public abstract class AutomatedCraftingChamber extends SlimefunItem implements I
|
|||||||
return false;
|
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
|
@Override
|
||||||
@ -183,7 +189,7 @@ public abstract class AutomatedCraftingChamber extends SlimefunItem implements I
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void tick(Block b, SlimefunItem sf, Config data) {
|
public void tick(Block b, SlimefunItem sf, Config data) {
|
||||||
AutomatedCraftingChamber.this.tick(b);
|
AutomatedCraftingChamber.this.tick(b, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -192,40 +198,59 @@ public abstract class AutomatedCraftingChamber extends SlimefunItem implements I
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void tick(Block b) {
|
protected void tick(Block block, boolean craftLast) {
|
||||||
if (BlockStorage.getLocationInfo(b.getLocation(), "enabled").equals("false")) return;
|
if (!craftLast && BlockStorage.getLocationInfo(block.getLocation(), "enabled").equals("false")) return;
|
||||||
if (ChargableBlock.getCharge(b) < getEnergyConsumption()) 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();
|
StringBuilder builder = new StringBuilder();
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
boolean lastIteration = false;
|
||||||
for (int j = 0; j < 9; j++) {
|
for (int j = 0; j < 9; j++) {
|
||||||
if (i > 0) {
|
if (i > 0) {
|
||||||
builder.append(" </slot> ");
|
builder.append(" </slot> ");
|
||||||
}
|
}
|
||||||
|
|
||||||
ItemStack item = menu.getItemInSlot(getInputSlots()[j]);
|
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));
|
builder.append(CustomItemSerializer.serialize(item, ItemFlag.MATERIAL, ItemFlag.ITEMMETA_DISPLAY_NAME, ItemFlag.ITEMMETA_LORE));
|
||||||
|
|
||||||
i++;
|
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)) {
|
if (SlimefunPlugin.getUtilities().automatedCraftingChamberRecipes.containsKey(input)) {
|
||||||
ItemStack output = SlimefunPlugin.getUtilities().automatedCraftingChamberRecipes.get(input).clone();
|
ItemStack output = SlimefunPlugin.getUtilities().automatedCraftingChamberRecipes.get(input).clone();
|
||||||
|
|
||||||
if (menu.fits(output, getOutputSlots())) {
|
if (menu.fits(output, getOutputSlots())) {
|
||||||
menu.pushItem(output, getOutputSlots());
|
menu.pushItem(output, getOutputSlots());
|
||||||
ChargableBlock.addCharge(b, -getEnergyConsumption());
|
ChargableBlock.addCharge(block, -getEnergyConsumption());
|
||||||
for (int j = 0; j < 9; j++) {
|
for (int j = 0; j < 9; j++) {
|
||||||
if (menu.getItemInSlot(getInputSlots()[j]) != null) menu.replaceExistingItem(getInputSlots()[j], InvUtils.decreaseItem(menu.getItemInSlot(getInputSlots()[j]), 1));
|
if (menu.getItemInSlot(getInputSlots()[j]) != null) menu.replaceExistingItem(getInputSlots()[j], InvUtils.decreaseItem(menu.getItemInSlot(getInputSlots()[j]), 1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user