1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-19 19:25:48 +00:00

Further code improvements

This commit is contained in:
Sefiraat 2023-07-11 17:29:05 +01:00
parent d543bafb20
commit 378a78ee69

View File

@ -5,6 +5,7 @@ import java.util.Iterator;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault; import javax.annotation.ParametersAreNonnullByDefault;
import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
@ -124,22 +125,22 @@ public class ExpCollector extends SlimefunItem implements InventoryBlock, Energy
} }
protected void tick(Block block) { protected void tick(Block block) {
Iterator<Entity> iterator = block.getWorld().getNearbyEntities(block.getLocation(), 4.0, 4.0, 4.0, n -> n instanceof ExperienceOrb && n.isValid()).iterator(); Location location = block.getLocation();
Iterator<Entity> iterator = block.getWorld().getNearbyEntities(location, 4.0, 4.0, 4.0, n -> n instanceof ExperienceOrb && n.isValid()).iterator();
int experiencePoints = 0; int experiencePoints = 0;
while (iterator.hasNext() && 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; return;
} }
experiencePoints = getStoredExperience(block) + ((ExperienceOrb) entity).getExperience(); experiencePoints = getStoredExperience(location) + orb.getExperience();
removeCharge(block.getLocation(), ENERGY_CONSUMPTION); removeCharge(location, ENERGY_CONSUMPTION);
entity.remove(); orb.remove();
produceFlasks(location, experiencePoints);
produceFlasks(block, 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 * Produces Flasks of Knowledge for the given block until it either uses all stored
* experience or runs out of room. * experience or runs out of room.
* *
* @param block * @param location
* The {@link Block} to produce flasks in. * The {@link Location} of the {@link ExpCollector} to produce flasks in.
* @param experiencePoints * @param experiencePoints
* The number of experience points to use during production. * 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; int withdrawn = 0;
BlockMenu menu = BlockStorage.getInventory(block); BlockMenu menu = BlockStorage.getInventory(location);
for (int level = 0; level < getStoredExperience(block); level = level + 10) { for (int level = 0; level < getStoredExperience(location); level = level + 10) {
if (menu.fits(SlimefunItems.FILLED_FLASK_OF_KNOWLEDGE, getOutputSlots())) { if (menu.fits(SlimefunItems.FILLED_FLASK_OF_KNOWLEDGE, getOutputSlots())) {
withdrawn = withdrawn + 10; withdrawn = withdrawn + 10;
menu.pushItem(SlimefunItems.FILLED_FLASK_OF_KNOWLEDGE.clone(), getOutputSlots()); menu.pushItem(SlimefunItems.FILLED_FLASK_OF_KNOWLEDGE.clone(), getOutputSlots());
@ -164,17 +165,17 @@ public class ExpCollector extends SlimefunItem implements InventoryBlock, Energy
break; break;
} }
} }
BlockStorage.addBlockInfo(block, DATA_KEY, String.valueOf(experiencePoints - withdrawn)); BlockStorage.addBlockInfo(location, DATA_KEY, String.valueOf(experiencePoints - withdrawn));
} }
private int getStoredExperience(Block block) { private int getStoredExperience(Location location) {
Config cfg = BlockStorage.getLocationInfo(block.getLocation()); Config cfg = BlockStorage.getLocationInfo(location);
String value = cfg.getString(DATA_KEY); String value = cfg.getString(DATA_KEY);
if (value != null) { if (value != null) {
return Integer.parseInt(value); return Integer.parseInt(value);
} else { } else {
BlockStorage.addBlockInfo(block, DATA_KEY, "0"); BlockStorage.addBlockInfo(location, DATA_KEY, "0");
return 0; return 0;
} }
} }