Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement resource cooldown for Oil Pump #4010

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.thebusybiscuit.slimefun4.implementation.items.geo;

import java.time.Instant;
import java.util.Arrays;
import java.util.List;
import java.util.OptionalInt;
Expand All @@ -24,6 +25,7 @@

import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.abstractItems.AContainer;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.abstractItems.MachineRecipe;
import me.mrCookieSlime.Slimefun.api.BlockStorage;
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu;
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenuPreset;
import me.mrCookieSlime.Slimefun.api.item_transport.ItemTransportFlow;
Expand Down Expand Up @@ -72,6 +74,21 @@ public int[] getSlotsAccessedByItemTransport(ItemTransportFlow flow) {
};
}

private boolean isOnResourceCooldown(Block b) {
String resourceCooldownUntil = BlockStorage.getLocationInfo(b.getLocation(), "resource-cooldown-until");
if (resourceCooldownUntil == null) return false;
long cooldownUntilMillis = Long.parseLong(resourceCooldownUntil);
return cooldownUntilMillis > Instant.now().toEpochMilli();
}

private void setOnResourceCooldown(Block b) {
BlockStorage.addBlockInfo(b, "resource-cooldown-until", String.valueOf(Instant.now().plusSeconds(5).toEpochMilli()));
}

private void removeResourceCooldown(Block b) {
BlockStorage.addBlockInfo(b, "resource-cooldown-until", null);
}

@Override
public List<ItemStack> getDisplayRecipes() {
return Arrays.asList(emptyBucket, SlimefunItems.OIL_BUCKET);
Expand All @@ -92,6 +109,20 @@ protected MachineRecipe findNextRecipe(BlockMenu inv) {
if (inv.fits(SlimefunItems.OIL_BUCKET, getOutputSlots())) {
Block b = inv.getBlock();

if (this.isOnResourceCooldown(b)) {
/*
* Oil Pumps can cause lag getting supplies each tick if there are none.
* We add a cooldown to limit the amount of processing required.
*/
return null;
} else {
/*
* Not on resource cooldown, so remove the data if it exists.
*/
this.removeResourceCooldown(b);
}


for (int slot : getInputSlots()) {
if (SlimefunUtils.isItemSimilar(inv.getItemInSlot(slot), emptyBucket, true, false)) {
OptionalInt supplies = Slimefun.getGPSNetwork().getResourceManager().getSupplies(oil, b.getWorld(), b.getX() >> 4, b.getZ() >> 4);
Expand All @@ -104,12 +135,12 @@ protected MachineRecipe findNextRecipe(BlockMenu inv) {
return recipe;
} else {
/*
* Move the empty bucket to the output slot to prevent this
* Set the oil pump on cooldown to prevent it
* from immediately starting all over again (to prevent lag)
* We don't move the bucket into the output slots as it can cause the entire
* system to be filled with buckets.
*/
ItemStack item = inv.getItemInSlot(slot).clone();
inv.replaceExistingItem(slot, null);
inv.pushItem(item, getOutputSlots());
this.setOnResourceCooldown(b);
Comment on lines -110 to +143
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JustAHuman-xD as you can see here, this PR removes the bucket cycling already, which is what you're suggesting. It's already done.

return null;
}
}
Expand Down