From d543bafb200c2167fe027b4dce949e89860bec39 Mon Sep 17 00:00:00 2001 From: Sefiraat Date: Tue, 11 Jul 2023 16:55:09 +0100 Subject: [PATCH 1/2] Skip fits checks --- .../machines/entities/ExpCollector.java | 53 ++++++++++++------- 1 file changed, 33 insertions(+), 20 deletions(-) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/entities/ExpCollector.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/entities/ExpCollector.java index 18279f0b9..a023b151f 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/entities/ExpCollector.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/entities/ExpCollector.java @@ -123,46 +123,59 @@ public class ExpCollector extends SlimefunItem implements InventoryBlock, Energy }); } - protected void tick(Block b) { - Iterator iterator = b.getWorld().getNearbyEntities(b.getLocation(), 4.0, 4.0, 4.0, n -> n instanceof ExperienceOrb && n.isValid()).iterator(); + protected void tick(Block block) { + Iterator iterator = block.getWorld().getNearbyEntities(block.getLocation(), 4.0, 4.0, 4.0, n -> n instanceof ExperienceOrb && n.isValid()).iterator(); int experiencePoints = 0; while (iterator.hasNext() && experiencePoints == 0) { Entity entity = iterator.next(); - if (getCharge(b.getLocation()) < ENERGY_CONSUMPTION) { + if (getCharge(block.getLocation()) < ENERGY_CONSUMPTION) { return; } - experiencePoints = getStoredExperience(b) + ((ExperienceOrb) entity).getExperience(); + experiencePoints = getStoredExperience(block) + ((ExperienceOrb) entity).getExperience(); - removeCharge(b.getLocation(), ENERGY_CONSUMPTION); + removeCharge(block.getLocation(), ENERGY_CONSUMPTION); entity.remove(); - int withdrawn = 0; - BlockMenu menu = BlockStorage.getInventory(b); - - for (int level = 0; level < getStoredExperience(b); level = level + 10) { - if (menu.fits(SlimefunItems.FILLED_FLASK_OF_KNOWLEDGE, getOutputSlots())) { - withdrawn = withdrawn + 10; - menu.pushItem(SlimefunItems.FILLED_FLASK_OF_KNOWLEDGE.clone(), getOutputSlots()); - } - } - - BlockStorage.addBlockInfo(b, DATA_KEY, String.valueOf(experiencePoints - withdrawn)); + produceFlasks(block, experiencePoints); } } - private int getStoredExperience(Block b) { - Config cfg = BlockStorage.getLocationInfo(b.getLocation()); + /** + * Produces Flasks of Knowledge for the given block until it either uses all stored + * experience or runs out of room. + * + * @param block + * The {@link Block} to produce flasks in. + * @param experiencePoints + * The number of experience points to use during production. + */ + private void produceFlasks(@Nonnull Block block, int experiencePoints) { + int withdrawn = 0; + BlockMenu menu = BlockStorage.getInventory(block); + for (int level = 0; level < getStoredExperience(block); level = level + 10) { + if (menu.fits(SlimefunItems.FILLED_FLASK_OF_KNOWLEDGE, getOutputSlots())) { + withdrawn = withdrawn + 10; + menu.pushItem(SlimefunItems.FILLED_FLASK_OF_KNOWLEDGE.clone(), getOutputSlots()); + } else { + // There is no room for more bottles, so lets stop checking if more will fit. + break; + } + } + BlockStorage.addBlockInfo(block, DATA_KEY, String.valueOf(experiencePoints - withdrawn)); + } + + private int getStoredExperience(Block block) { + Config cfg = BlockStorage.getLocationInfo(block.getLocation()); String value = cfg.getString(DATA_KEY); if (value != null) { return Integer.parseInt(value); } else { - BlockStorage.addBlockInfo(b, DATA_KEY, "0"); + BlockStorage.addBlockInfo(block, DATA_KEY, "0"); return 0; } } - } From 378a78ee698db7adebf547c66db32fd6978c06f8 Mon Sep 17 00:00:00 2001 From: Sefiraat Date: Tue, 11 Jul 2023 17:29:05 +0100 Subject: [PATCH 2/2] Further code improvements --- .../machines/entities/ExpCollector.java | 35 ++++++++++--------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/entities/ExpCollector.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/entities/ExpCollector.java index a023b151f..3a6cb6d85 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/entities/ExpCollector.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/entities/ExpCollector.java @@ -5,6 +5,7 @@ import java.util.Iterator; import javax.annotation.Nonnull; import javax.annotation.ParametersAreNonnullByDefault; +import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.entity.Entity; @@ -124,22 +125,22 @@ public class ExpCollector extends SlimefunItem implements InventoryBlock, Energy } protected void tick(Block block) { - Iterator iterator = block.getWorld().getNearbyEntities(block.getLocation(), 4.0, 4.0, 4.0, n -> n instanceof ExperienceOrb && n.isValid()).iterator(); + Location location = block.getLocation(); + Iterator iterator = block.getWorld().getNearbyEntities(location, 4.0, 4.0, 4.0, n -> n instanceof ExperienceOrb && n.isValid()).iterator(); int experiencePoints = 0; while (iterator.hasNext() && experiencePoints == 0) { - Entity entity = iterator.next(); + ExperienceOrb orb = (ExperienceOrb) iterator.next(); - if (getCharge(block.getLocation()) < ENERGY_CONSUMPTION) { + if (getCharge(location) < ENERGY_CONSUMPTION) { return; } - experiencePoints = getStoredExperience(block) + ((ExperienceOrb) entity).getExperience(); + experiencePoints = getStoredExperience(location) + orb.getExperience(); - removeCharge(block.getLocation(), ENERGY_CONSUMPTION); - entity.remove(); - - produceFlasks(block, experiencePoints); + removeCharge(location, ENERGY_CONSUMPTION); + orb.remove(); + produceFlasks(location, experiencePoints); } } @@ -147,15 +148,15 @@ public class ExpCollector extends SlimefunItem implements InventoryBlock, Energy * Produces Flasks of Knowledge for the given block until it either uses all stored * experience or runs out of room. * - * @param block - * The {@link Block} to produce flasks in. + * @param location + * The {@link Location} of the {@link ExpCollector} to produce flasks in. * @param experiencePoints * The number of experience points to use during production. */ - private void produceFlasks(@Nonnull Block block, int experiencePoints) { + private void produceFlasks(@Nonnull Location location, int experiencePoints) { int withdrawn = 0; - BlockMenu menu = BlockStorage.getInventory(block); - for (int level = 0; level < getStoredExperience(block); level = level + 10) { + BlockMenu menu = BlockStorage.getInventory(location); + for (int level = 0; level < getStoredExperience(location); level = level + 10) { if (menu.fits(SlimefunItems.FILLED_FLASK_OF_KNOWLEDGE, getOutputSlots())) { withdrawn = withdrawn + 10; menu.pushItem(SlimefunItems.FILLED_FLASK_OF_KNOWLEDGE.clone(), getOutputSlots()); @@ -164,17 +165,17 @@ public class ExpCollector extends SlimefunItem implements InventoryBlock, Energy break; } } - BlockStorage.addBlockInfo(block, DATA_KEY, String.valueOf(experiencePoints - withdrawn)); + BlockStorage.addBlockInfo(location, DATA_KEY, String.valueOf(experiencePoints - withdrawn)); } - private int getStoredExperience(Block block) { - Config cfg = BlockStorage.getLocationInfo(block.getLocation()); + private int getStoredExperience(Location location) { + Config cfg = BlockStorage.getLocationInfo(location); String value = cfg.getString(DATA_KEY); if (value != null) { return Integer.parseInt(value); } else { - BlockStorage.addBlockInfo(block, DATA_KEY, "0"); + BlockStorage.addBlockInfo(location, DATA_KEY, "0"); return 0; } }