Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
将一个一个充电改成一次充电的量平分
  • Loading branch information
mcchampions committed Jul 5, 2024
1 parent bf63932 commit 400ec02
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ default float getItemCharge(ItemStack item) {
return ChargeUtils.getCharge(item.getItemMeta());
}

default float getDistanceToMaxCharge(ItemStack item) {
ItemMeta meta = item.getItemMeta();
float currentCharge = ChargeUtils.getCharge(meta);
float maximum = getMaxItemCharge(item);

return maximum - currentCharge;
}

/**
* This method adds the given charge to the provided {@link ItemStack}.
* The method will also return whether this operation was successful.
Expand All @@ -100,7 +108,6 @@ default boolean addItemCharge(ItemStack item, float charge) {
float currentCharge = ChargeUtils.getCharge(meta);
float maximum = getMaxItemCharge(item);

// If the item is already fully charged, we abort.
if (currentCharge >= maximum) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack;
import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType;
import io.github.thebusybiscuit.slimefun4.core.attributes.Rechargeable;
import io.github.thebusybiscuit.slimefun4.core.debug.Debug;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.abstractItems.AContainer;
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu;
import org.bukkit.Material;
Expand Down Expand Up @@ -34,50 +36,23 @@ protected void tick(Block b) {
if (getCharge(b.getLocation()) < getEnergyConsumption()) {
return;
}

BlockMenu inv = StorageCacheUtils.getMenu(b.getLocation());

for (int slot : getInputSlots()) {
ItemStack item = inv.getItemInSlot(slot);
if (item.getAmount() != 1) {
if (chargeOne(b, inv, slot, item)) {
if (item != null) {
if (charge(b, inv, slot, item)) {
return;
}
}
if (charge(b, inv, slot, item)) {
return;
}
}
}


private boolean chargeOne(Block b, BlockMenu inv, int slot, ItemStack item) {
SlimefunItem sfItem = SlimefunItem.getByItem(item);

if (sfItem instanceof Rechargeable rechargeable) {
float charge = getEnergyConsumption() / 2F;

if (rechargeable.addItemCharge(item, charge)) {
removeCharge(b.getLocation(), getEnergyConsumption());
} else if (inv.fits(item, getOutputSlots())) {
inv.pushItem((SlimefunItemStack) sfItem.getItem(), getOutputSlots());
item.setAmount(item.getAmount() - 1);
}

return true;
} else if (sfItem != null && inv.fits(item, getOutputSlots())) {
inv.pushItem((SlimefunItemStack) sfItem.getItem(), getOutputSlots());
// 不可充电物品直接过
inv.replaceExistingItem(slot, null);
}
return false;
}

private boolean charge(Block b, BlockMenu inv, int slot, ItemStack item) {
SlimefunItem sfItem = SlimefunItem.getByItem(item);

if (sfItem instanceof Rechargeable rechargeable) {
float charge = getEnergyConsumption() / 2F;
float charge = getEnergyConsumption() / 2F / item.getAmount();

if (rechargeable.addItemCharge(item, charge)) {
removeCharge(b.getLocation(), getEnergyConsumption());
Expand Down

0 comments on commit 400ec02

Please sign in to comment.