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:
parent
d543bafb20
commit
378a78ee69
@ -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<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;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user