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

Merge pull request #3907 from Sefiraat/fix/exp_collector_performance

This commit is contained in:
Sfiguz7 2023-07-11 18:40:11 +02:00 committed by GitHub
commit 6bbf46b1d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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;
@ -123,46 +124,59 @@ public class ExpCollector extends SlimefunItem implements InventoryBlock, Energy
});
}
protected void tick(Block b) {
Iterator<Entity> iterator = b.getWorld().getNearbyEntities(b.getLocation(), 4.0, 4.0, 4.0, n -> n instanceof ExperienceOrb && n.isValid()).iterator();
protected void tick(Block block) {
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;
while (iterator.hasNext() && experiencePoints == 0) {
Entity entity = iterator.next();
ExperienceOrb orb = (ExperienceOrb) iterator.next();
if (getCharge(b.getLocation()) < ENERGY_CONSUMPTION) {
if (getCharge(location) < ENERGY_CONSUMPTION) {
return;
}
experiencePoints = getStoredExperience(b) + ((ExperienceOrb) entity).getExperience();
experiencePoints = getStoredExperience(location) + orb.getExperience();
removeCharge(b.getLocation(), ENERGY_CONSUMPTION);
entity.remove();
removeCharge(location, ENERGY_CONSUMPTION);
orb.remove();
produceFlasks(location, experiencePoints);
}
}
/**
* Produces Flasks of Knowledge for the given block until it either uses all stored
* experience or runs out of room.
*
* @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 Location location, int experiencePoints) {
int withdrawn = 0;
BlockMenu menu = BlockStorage.getInventory(b);
for (int level = 0; level < getStoredExperience(b); 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());
} else {
// There is no room for more bottles, so lets stop checking if more will fit.
break;
}
}
BlockStorage.addBlockInfo(location, DATA_KEY, String.valueOf(experiencePoints - withdrawn));
}
BlockStorage.addBlockInfo(b, DATA_KEY, String.valueOf(experiencePoints - withdrawn));
}
}
private int getStoredExperience(Block b) {
Config cfg = BlockStorage.getLocationInfo(b.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(b, DATA_KEY, "0");
BlockStorage.addBlockInfo(location, DATA_KEY, "0");
return 0;
}
}
}