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:
commit
6bbf46b1d9
@ -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;
|
||||||
@ -123,46 +124,59 @@ public class ExpCollector extends SlimefunItem implements InventoryBlock, Energy
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void tick(Block b) {
|
protected void tick(Block block) {
|
||||||
Iterator<Entity> iterator = b.getWorld().getNearbyEntities(b.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(b.getLocation()) < ENERGY_CONSUMPTION) {
|
if (getCharge(location) < ENERGY_CONSUMPTION) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
experiencePoints = getStoredExperience(b) + ((ExperienceOrb) entity).getExperience();
|
experiencePoints = getStoredExperience(location) + orb.getExperience();
|
||||||
|
|
||||||
removeCharge(b.getLocation(), ENERGY_CONSUMPTION);
|
removeCharge(location, ENERGY_CONSUMPTION);
|
||||||
entity.remove();
|
orb.remove();
|
||||||
|
produceFlasks(location, experiencePoints);
|
||||||
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));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 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(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));
|
||||||
|
}
|
||||||
|
|
||||||
|
private int getStoredExperience(Location location) {
|
||||||
|
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(b, DATA_KEY, "0");
|
BlockStorage.addBlockInfo(location, DATA_KEY, "0");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user