From b67472a95f4323c29b04338bf57970ccf1bc9715 Mon Sep 17 00:00:00 2001 From: test137e29b Date: Wed, 1 Nov 2023 15:04:49 +0000 Subject: [PATCH 1/2] Implement resource cooldown for Oil Pump --- .../implementation/items/geo/OilPump.java | 41 +++++++++++++++++-- 1 file changed, 37 insertions(+), 4 deletions(-) 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..2e6c0108b2 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,6 +1,8 @@ package io.github.thebusybiscuit.slimefun4.implementation.items.geo; +import java.time.Instant; import java.util.Arrays; +import java.util.Date; import java.util.List; import java.util.OptionalInt; @@ -12,6 +14,7 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; +import io.github.bakedlibs.dough.config.Config; import io.github.bakedlibs.dough.protection.Interaction; import io.github.thebusybiscuit.slimefun4.api.geo.GEOResource; import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup; @@ -24,6 +27,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 +76,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 +111,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 +137,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; } } From 5517ae22fc7b4c7d9ca35ec2fadc5d62ecdc4fa2 Mon Sep 17 00:00:00 2001 From: test137e29b Date: Wed, 1 Nov 2023 15:13:16 +0000 Subject: [PATCH 2/2] Unused Imports --- .../slimefun4/implementation/items/geo/OilPump.java | 2 -- 1 file changed, 2 deletions(-) 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 2e6c0108b2..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 @@ -2,7 +2,6 @@ import java.time.Instant; import java.util.Arrays; -import java.util.Date; import java.util.List; import java.util.OptionalInt; @@ -14,7 +13,6 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; -import io.github.bakedlibs.dough.config.Config; import io.github.bakedlibs.dough.protection.Interaction; import io.github.thebusybiscuit.slimefun4.api.geo.GEOResource; import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup;