Optimizing Barrel retrieve

This commit is contained in:
Sn0wStorm 2019-11-26 21:29:41 +01:00
parent 16c03f9da1
commit 40da30bf8e
2 changed files with 36 additions and 14 deletions

View File

@ -82,13 +82,21 @@ public class Barrel implements InventoryHolder {
// Minecraft day is 20 min, so add 1/20 to the time every minute // Minecraft day is 20 min, so add 1/20 to the time every minute
barrel.time += (1.0 / 20.0); barrel.time += (1.0 / 20.0);
} }
if (check == 0 && barrels.size() > 0) { int numBarrels = barrels.size();
Barrel random = barrels.get((int) Math.floor(Math.random() * barrels.size())); if (check == 0 && numBarrels > 0) {
Barrel random = barrels.get((int) Math.floor(Math.random() * numBarrels));
if (random != null) { if (random != null) {
// You have been selected for a random search // You have been selected for a random search
// We want to check at least one barrel every time // We want to check at least one barrel every time
random.checked = false; random.checked = false;
} }
if (numBarrels > 50) {
Barrel randomInTheBack = barrels.get(numBarrels - 1 - (int) (Math.random() * (numBarrels >>> 2)));
if (randomInTheBack != null) {
// Prioritize checking one of the less recently used barrels as well
randomInTheBack.checked = false;
}
}
new BarrelCheck().runTaskTimer(P.p, 1, 1); new BarrelCheck().runTaskTimer(P.p, 1, 1);
} }
} }
@ -273,6 +281,7 @@ public class Barrel implements InventoryHolder {
signoffset = (byte) (sign.getY() - spigot.getY()); signoffset = (byte) (sign.getY() - spigot.getY());
} }
int i = 0;
for (Barrel barrel : barrels) { for (Barrel barrel : barrels) {
if (barrel.body.isSignOfBarrel(signoffset)) { if (barrel.body.isSignOfBarrel(signoffset)) {
if (barrel.spigot.equals(spigot)) { if (barrel.spigot.equals(spigot)) {
@ -280,9 +289,11 @@ public class Barrel implements InventoryHolder {
// Barrel has no signOffset even though we clicked a sign, may be old // Barrel has no signOffset even though we clicked a sign, may be old
barrel.body.setSignoffset(signoffset); barrel.body.setSignoffset(signoffset);
} }
moveMRU(i);
return barrel; return barrel;
} }
} }
i++;
} }
return null; return null;
} }
@ -293,15 +304,27 @@ public class Barrel implements InventoryHolder {
@Nullable @Nullable
public static Barrel getByWood(Block wood) { public static Barrel getByWood(Block wood) {
if (LegacyUtil.isWoodPlanks(wood.getType()) || LegacyUtil.isWoodStairs(wood.getType())) { if (LegacyUtil.isWoodPlanks(wood.getType()) || LegacyUtil.isWoodStairs(wood.getType())) {
int i = 0;
for (Barrel barrel : barrels) { for (Barrel barrel : barrels) {
if (barrel.getSpigot().getWorld().equals(wood.getWorld()) && barrel.body.getBounds().contains(wood)) { if (barrel.getSpigot().getWorld().equals(wood.getWorld()) && barrel.body.getBounds().contains(wood)) {
moveMRU(i);
return barrel; return barrel;
} }
i++;
} }
} }
return null; return null;
} }
// Move Barrel that was recently used more towards the front of the List
// Optimizes retrieve by Block over time
private static void moveMRU(int index) {
if (index > 0) {
// Swap entry at the index with the one next to it
barrels.set(index - 1, barrels.set(index, barrels.get(index - 1)));
}
}
/** /**
* creates a new Barrel out of a sign * creates a new Barrel out of a sign
*/ */
@ -331,7 +354,7 @@ public class Barrel implements InventoryHolder {
BarrelCreateEvent createEvent = new BarrelCreateEvent(barrel, player); BarrelCreateEvent createEvent = new BarrelCreateEvent(barrel, player);
P.p.getServer().getPluginManager().callEvent(createEvent); P.p.getServer().getPluginManager().callEvent(createEvent);
if (!createEvent.isCancelled()) { if (!createEvent.isCancelled()) {
barrels.add(barrel); barrels.add(0, barrel);
return true; return true;
} }
} }
@ -517,14 +540,15 @@ public class Barrel implements InventoryHolder {
if (!barrel.checked) { if (!barrel.checked) {
Block broken = barrel.body.getBrokenBlock(false); Block broken = barrel.body.getBrokenBlock(false);
if (broken != null) { if (broken != null) {
P.p.debugLog("Barrel at " + broken.getWorld().getName() + "/" + broken.getX() + "/" + broken.getY() + "/" + broken.getZ() P.p.debugLog("Barrel at "
+ " has been destroyed unexpectedly, contents will drop"); + broken.getWorld().getName() + "/" + broken.getX() + "/" + broken.getY() + "/" + broken.getZ()
+ " has been destroyed unexpectedly, contents will drop");
// remove the barrel if it was destroyed // remove the barrel if it was destroyed
barrel.remove(broken, null, true); barrel.remove(broken, null, true);
} else { } else {
// Dont check this barrel again, its enough to check it once after every restart // Dont check this barrel again, its enough to check it once after every restart (and when randomly chosen)
// as now this is only the backup if we dont register the barrel breaking, as sample // as now this is only the backup if we dont register the barrel breaking,
// when removing it with some world editor // for example when removing it with some world editor
barrel.checked = true; barrel.checked = true;
} }
repeat = false; repeat = false;

View File

@ -52,12 +52,10 @@ public class PlayerListener implements Listener {
barrel = Barrel.getByWood(clickedBlock); barrel = Barrel.getByWood(clickedBlock);
} }
} else if (LegacyUtil.isWoodStairs(type)) { } else if (LegacyUtil.isWoodStairs(type)) {
for (Barrel barrel2 : Barrel.barrels) { barrel = Barrel.getByWood(clickedBlock);
if (barrel2.getBody().hasBlock(clickedBlock)) { if (barrel != null) {
if (BConfig.openEverywhere || !barrel2.isLarge()) { if (!BConfig.openEverywhere && barrel.isLarge()) {
barrel = barrel2; barrel = null;
}
break;
} }
} }
} else if (LegacyUtil.isFence(type) || LegacyUtil.isSign(type)) { } else if (LegacyUtil.isFence(type) || LegacyUtil.isSign(type)) {