diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/geo/OilPump.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/geo/OilPump.java index 4422117526..784043a927 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/geo/OilPump.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/geo/OilPump.java @@ -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; @@ -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; @@ -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 getDisplayRecipes() { return Arrays.asList(emptyBucket, SlimefunItems.OIL_BUCKET); @@ -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); @@ -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); return null; } }