Added a crop cache to crops to massively improve performance

This commit is contained in:
Brianna O'Keefe 2018-10-26 18:05:40 -04:00
parent 5c5cf6838c
commit ef2ed38246
3 changed files with 49 additions and 24 deletions

View File

@ -1,13 +1,17 @@
package com.songoda.epicfarming.api.farming;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.inventory.Inventory;
import java.util.List;
import java.util.UUID;
public interface Farm {
Inventory getInventory();
List<Block> getCachedCrops();
Location getLocation();
UUID getPlacedBy();

View File

@ -25,10 +25,7 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.RegisteredServiceProvider;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.*;
public class EFarm implements Farm {
@ -37,6 +34,9 @@ public class EFarm implements Farm {
private Inventory inventory;
private UUID placedBy;
private long lastCached = 0;
private final List<Block> cachedCrops = new ArrayList<>();
public EFarm(Location location, Level level, UUID placedBy) {
this.location = location;
this.level = level;
@ -284,6 +284,27 @@ public class EFarm implements Farm {
return false;
}
public void addCachedCrop(Block block) {
cachedCrops.add(block);
}
@Override
public List<Block> getCachedCrops() {
return new ArrayList<>(cachedCrops);
}
public void clearCache() {
cachedCrops.clear();
}
public long getLastCached() {
return lastCached;
}
public void setLastCached(long lastCached) {
this.lastCached = lastCached;
}
@Override
public Location getLocation() {
return location.clone();

View File

@ -4,6 +4,7 @@ import com.songoda.epicfarming.EpicFarmingPlugin;
import com.songoda.epicfarming.api.farming.Farm;
import com.songoda.epicfarming.boost.BoostData;
import com.songoda.epicfarming.farming.Crop;
import com.songoda.epicfarming.farming.EFarm;
import com.songoda.epicfarming.utils.CropType;
import com.songoda.epicfarming.utils.Debugger;
import com.songoda.epicfarming.utils.Methods;
@ -17,9 +18,7 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.material.Crops;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.*;
public class FarmTask extends BukkitRunnable {
@ -115,30 +114,31 @@ public class FarmTask extends BukkitRunnable {
}
public List<Block> getCrops(Farm farm, boolean add) {
List<Block> crops = new ArrayList<>();
if (System.currentTimeMillis() - ((EFarm)farm).getLastCached() > 30 * 1000) {
((EFarm)farm).setLastCached(System.currentTimeMillis());
Block block = farm.getLocation().getBlock();
int radius = farm.getLevel().getRadius();
int bx = block.getX();
int by = block.getY();
int bz = block.getZ();
for (int fx = -radius; fx <= radius; fx++) {
for (int fy = -2; fy <= 1; fy++) {
for (int fz = -radius; fz <= radius; fz++) {
Block b2 = block.getWorld().getBlockAt(bx + fx, by + fy, bz + fz);
Block block = farm.getLocation().getBlock();
int radius = farm.getLevel().getRadius();
int bx = block.getX();
int by = block.getY();
int bz = block.getZ();
for (int fx = -radius; fx <= radius; fx++) {
for (int fy = -2; fy <= 1; fy++) {
for (int fz = -radius; fz <= radius; fz++) {
Block b2 = block.getWorld().getBlockAt(bx + fx, by + fy, bz + fz);
if (!(b2.getState().getData() instanceof Crops)) continue;
if (!(b2.getState().getData() instanceof Crops)) continue;
if (add) {
((EFarm)farm).addCachedCrop(b2);
continue;
}
plugin.getGrowthTask().removeCropByLocation(b2.getLocation());
if (add) {
crops.add(b2);
continue;
}
plugin.getGrowthTask().removeCropByLocation(b2.getLocation());
}
}
}
return crops;
return farm.getCachedCrops();
}
private boolean canMove(Inventory inventory, ItemStack item) {