Async shop creation

This should fix timeouts when (re-)loading shops
This commit is contained in:
Eric 2018-07-30 18:31:08 +02:00
parent 16498424a8
commit 80626e823f
4 changed files with 45 additions and 36 deletions

View File

@ -73,9 +73,6 @@ public class ChestProtectListener implements Listener {
public void onResult(Void result) { public void onResult(Void result) {
newShop.create(true); newShop.create(true);
shopUtils.addShop(newShop, true); shopUtils.addShop(newShop, true);
for (Player player : shop.getLocation().getWorld().getPlayers()) {
shopUtils.updateShops(player, true);
}
} }
}); });
} else { } else {
@ -271,9 +268,6 @@ public class ChestProtectListener implements Listener {
newShop.create(true); newShop.create(true);
shopUtils.addShop(newShop, true); shopUtils.addShop(newShop, true);
plugin.debug(String.format("%s extended %s's shop (#%d)", p.getName(), shop.getVendor().getName(), shop.getID())); plugin.debug(String.format("%s extended %s's shop (#%d)", p.getName(), shop.getVendor().getName(), shop.getID()));
for (Player player : shop.getLocation().getWorld().getPlayers()) {
shopUtils.updateShops(player, true);
}
} }
}); });
} else { } else {

View File

@ -769,11 +769,6 @@ public class ShopInteractListener implements Listener {
} else { } else {
executor.sendMessage(LanguageUtils.getMessage(Message.SHOP_CREATED, placeholder)); executor.sendMessage(LanguageUtils.getMessage(Message.SHOP_CREATED, placeholder));
} }
// next update will display the new shop
for (Player player : location.getWorld().getPlayers()) {
plugin.getShopUtils().resetPlayerLocation(player);
}
} }
/** /**

View File

@ -10,6 +10,8 @@ import de.epiceric.shopchest.language.LanguageUtils;
import de.epiceric.shopchest.nms.Hologram; import de.epiceric.shopchest.nms.Hologram;
import de.epiceric.shopchest.utils.ItemUtils; import de.epiceric.shopchest.utils.ItemUtils;
import de.epiceric.shopchest.utils.Utils; import de.epiceric.shopchest.utils.Utils;
import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
@ -19,8 +21,10 @@ import org.bukkit.block.BlockFace;
import org.bukkit.block.Chest; import org.bukkit.block.Chest;
import org.bukkit.block.DoubleChest; import org.bukkit.block.DoubleChest;
import org.bukkit.block.data.Directional; import org.bukkit.block.data.Directional;
import org.bukkit.entity.Player;
import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.*; import java.util.*;
@ -109,8 +113,14 @@ public class Shop {
return false; return false;
} }
if (hologram == null || !hologram.exists()) createHologram(); new BukkitRunnable() {
if (item == null) createItem(); @Override
public void run() {
if (hologram == null || !hologram.exists()) {
createHologram();
}
}
}.runTaskAsynchronously(plugin);
created = true; created = true;
return true; return true;
@ -152,6 +162,13 @@ public class Shop {
itemStack.setAmount(1); itemStack.setAmount(1);
item = new ShopItem(plugin, itemStack, itemLocation); item = new ShopItem(plugin, itemStack, itemLocation);
// Shop done loading, update shops for everyone
plugin.getUpdater().beforeNext(() -> {
for (Player player : location.getWorld().getPlayers()) {
plugin.getShopUtils().resetPlayerLocation(player);
}
});
} }
} }
@ -188,10 +205,18 @@ public class Shop {
face = ((Directional) chests[0].getBlockData()).getFacing(); face = ((Directional) chests[0].getBlockData()).getFacing();
} }
String[] holoText = getHologramText(); final String[] holoText = getHologramText();
Location holoLocation = getHologramLocation(doubleChest, chests, face); final Location holoLocation = getHologramLocation(doubleChest, chests, face);
new BukkitRunnable(){
@Override
public void run() {
// Create hologram synchronously because the
// constructor is modifying world lists
hologram = new Hologram(plugin, holoText, holoLocation); hologram = new Hologram(plugin, holoText, holoLocation);
if (item == null) createItem();
}
}.runTask(plugin);
} }
/** /**
@ -279,41 +304,40 @@ public class Shop {
} }
private Location getHologramLocation(boolean doubleChest, Chest[] chests, BlockFace face) { private Location getHologramLocation(boolean doubleChest, Chest[] chests, BlockFace face) {
Block b = location.getBlock(); World w = location.getWorld();
Location holoLocation; int x = location.getBlockX();
int y = location.getBlockY();
int z = location.getBlockZ();
World w = b.getWorld(); Location holoLocation = new Location(w, x, y, z);
int x = b.getX();
int y = b.getY();
int z = b.getZ();
double subtractY = 0.6; double deltaY = -0.6;
if (Config.hologramFixedBottom) subtractY = 0.85; if (Config.hologramFixedBottom) deltaY = -0.85;
if (doubleChest) { if (doubleChest) {
Chest c1 = Utils.getMajorVersion() >= 13 && (face == BlockFace.NORTH || face == BlockFace.EAST) ? chests[1] : chests[0]; Chest c1 = Utils.getMajorVersion() >= 13 && (face == BlockFace.NORTH || face == BlockFace.EAST) ? chests[1] : chests[0];
Chest c2 = Utils.getMajorVersion() >= 13 && (face == BlockFace.NORTH || face == BlockFace.EAST) ? chests[0] : chests[1]; Chest c2 = Utils.getMajorVersion() >= 13 && (face == BlockFace.NORTH || face == BlockFace.EAST) ? chests[0] : chests[1];
if (b.getLocation().equals(c1.getLocation())) { if (holoLocation.equals(c1.getLocation())) {
if (c1.getX() != c2.getX()) { if (c1.getX() != c2.getX()) {
holoLocation = new Location(w, x, y - subtractY, z + 0.5); holoLocation.add(0, deltaY, 0.5);
} else if (c1.getZ() != c2.getZ()) { } else if (c1.getZ() != c2.getZ()) {
holoLocation = new Location(w, x + 0.5, y - subtractY, z); holoLocation.add(0.5, deltaY, 0);
} else { } else {
holoLocation = new Location(w, x + 0.5, y - subtractY, z + 0.5); holoLocation.add(0.5, deltaY, 0.5);
} }
} else { } else {
if (c1.getX() != c2.getX()) { if (c1.getX() != c2.getX()) {
holoLocation = new Location(w, x + 1, y - subtractY, z + 0.5); holoLocation.add(1, deltaY, 0.5);
} else if (c1.getZ() != c2.getZ()) { } else if (c1.getZ() != c2.getZ()) {
holoLocation = new Location(w, x + 0.5, y - subtractY, z + 1); holoLocation.add(0.5, deltaY, 1);
} else { } else {
holoLocation = new Location(w, x + 0.5, y - subtractY, z + 0.5); holoLocation.add(0.5, deltaY, 0.5);
} }
} }
} else { } else {
holoLocation = new Location(w, x + 0.5, y - subtractY, z + 0.5); holoLocation.add(0.5, deltaY, 0.5);
} }
holoLocation.add(0, Config.hologramLift, 0); holoLocation.add(0, Config.hologramLift, 0);

View File

@ -251,10 +251,6 @@ public class ShopUtils {
} }
} }
for (Player player : Bukkit.getOnlinePlayers()) {
updateShops(player, true);
}
if (callback != null) callback.callSyncResult(result.size()); if (callback != null) callback.callSyncResult(result.size());
} }