Performance Optimize

Former-commit-id: 75f509a4634516d2eb0205e8e6b1c5e0e404b6c3
This commit is contained in:
Ghost_chu committed 2020-03-01 21:44:15 +08:00
1 parent f84623f1f3
commit 72c5775ed0
5 files changed
+107 -114

No files matched your search

@@ -35,33 +35,36 @@ import org.maxgamer.quickshop.Shop.Shop;
@AllArgsConstructor
public class ChunkListener implements Listener {
@NotNull private final QuickShop plugin;
@NotNull
private final QuickShop plugin;
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onChunkLoad(ChunkLoadEvent e) {
if (e.isNewChunk()) {
return;
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onChunkLoad(ChunkLoadEvent e) {
if (e.isNewChunk()) {
return;
}
final HashMap<Location, Shop> inChunk = plugin.getShopManager().getShops(e.getChunk());
if (inChunk == null || inChunk.isEmpty()) {
return;
}
Bukkit.getScheduler().runTaskLater(plugin, () -> {
for (Shop shop : new HashMap<>(inChunk).values()) {
shop.onLoad();
}
}, 1);
}
final HashMap<Location, Shop> inChunk = plugin.getShopManager().getShops(e.getChunk());
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onChunkUnload(ChunkUnloadEvent e) {
if (inChunk == null || inChunk.isEmpty()) {
return;
final HashMap<Location, Shop> inChunk = plugin.getShopManager().getShops(e.getChunk());
if (inChunk == null || inChunk.isEmpty()) {
return;
}
for (Shop shop : new HashMap<>(inChunk).values()) {
shop.onUnload();
}
}
Bukkit.getScheduler().runTaskLater(plugin,()-> {
//noinspection unchecked
for(Shop shop:((HashMap<Location, Shop>) inChunk.clone()).values()){shop.onLoad();}},1);
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onChunkUnload(ChunkUnloadEvent e) {
final HashMap<Location, Shop> inChunk = plugin.getShopManager().getShops(e.getChunk());
if (inChunk == null || inChunk.isEmpty()) {
return;
}
//noinspection unchecked
for(Shop shop:((HashMap<Location, Shop>) inChunk.clone()).values()){shop.onUnload();};
}
}
@@ -19,6 +19,13 @@
package org.maxgamer.quickshop;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.file.Files;
import java.sql.SQLException;
import java.util.*;
import java.util.Map.Entry;
import lombok.Getter;
import me.minebuilders.clearlag.Clearlag;
import me.minebuilders.clearlag.listeners.ItemMergeListener;
@@ -56,14 +63,6 @@ import org.maxgamer.quickshop.Util.Timer;
import org.maxgamer.quickshop.Util.ServerForkWrapper.SpigotWrapper;
import org.maxgamer.quickshop.Watcher.*;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.file.Files;
import java.sql.SQLException;
import java.util.*;
import java.util.Map.Entry;
@Getter
public class QuickShop extends JavaPlugin {
@@ -813,6 +812,11 @@ public class QuickShop extends JavaPlugin {
throw new RuntimeException("Snapshot cannot run when dev-mode is false in the config");
}
}
getLogger().info("Checking the tax account infos...");
String taxAccount = plugin.getConfig().getString("tax-account", "tax");
if (!Bukkit.getOfflinePlayer(taxAccount).hasPlayedBefore()) {
getLogger().warning("Tax account's player never played server before, that may cause server lagg or economy system error, you should change that name.");
}
}
/**
@@ -21,13 +21,8 @@ package org.maxgamer.quickshop.Shop;
import com.lishid.openinv.OpenInv;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.UUID;
import java.util.logging.Level;
import lombok.EqualsAndHashCode;
import org.bukkit.Bukkit;
@@ -45,13 +40,7 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.maxgamer.quickshop.Event.ShopClickEvent;
import org.maxgamer.quickshop.Event.ShopDeleteEvent;
import org.maxgamer.quickshop.Event.ShopLoadEvent;
import org.maxgamer.quickshop.Event.ShopModeratorChangedEvent;
import org.maxgamer.quickshop.Event.ShopPriceChangeEvent;
import org.maxgamer.quickshop.Event.ShopUnloadEvent;
import org.maxgamer.quickshop.Event.ShopUpdateEvent;
import org.maxgamer.quickshop.Event.*;
import org.maxgamer.quickshop.QuickShop;
import org.maxgamer.quickshop.Util.MsgUtil;
import org.maxgamer.quickshop.Util.Util;
@@ -903,13 +892,11 @@ public class ContainerShop implements Shop {
if (Util.fireCancellableEvent(shopLoadEvent)) {
return;
}
this.isLoaded = true;
Objects.requireNonNull(plugin.getShopManager().getLoadedShops()).add(this);
plugin.getShopContainerWatcher().scheduleCheck(this);
// check price restriction
Entry<Double, Double> priceRestriction = Util.getPriceRestriction(this.getMaterial());
if (priceRestriction != null) {
if (price < priceRestriction.getKey()) {
price = priceRestriction.getKey();
@@ -37,7 +37,6 @@ import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.plugin.RegisteredListener;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.maxgamer.quickshop.BootError;
import org.maxgamer.quickshop.Economy.Economy;
import org.maxgamer.quickshop.Event.ShopCreateEvent;
import org.maxgamer.quickshop.Event.ShopPreCreateEvent;
@@ -67,16 +66,8 @@ public class ShopManager {
public ShopManager(@NotNull QuickShop plugin) {
this.plugin = plugin;
this.useFastShopSearchAlgorithm = plugin.getConfig().getBoolean("shop.use-fast-shop-search-algorithm", false);
String taxAccount = plugin.getConfig().getString("tax-account");
for(OfflinePlayer player:Bukkit.getOfflinePlayers()){
if(player.getName()!=null&&player.getName().equalsIgnoreCase(taxAccount)){
cacheTaxAccount=player.getUniqueId();
}
}
if(cacheTaxAccount==null){
new BootError("Tax account never join the server","you should set a player which have joined the server in config.yml");
throw new RuntimeException("Tax account never join the server!");
}
//noinspection ConstantConditions
this.cacheTaxAccount = Bukkit.getOfflinePlayer(plugin.getConfig().getString("tax-account", "tax")).getUniqueId();
}
private void actionBuy(@NotNull Player p, @NotNull Economy eco, @NotNull HashMap<UUID, Info> actions2, @NotNull Info info, @NotNull String message, @NotNull Shop shop, int amount) {
@@ -152,9 +143,7 @@ public class ShopManager {
}
// Purchase successfully
if (tax != 0) {
if (cacheTaxAccount != null) {
eco.deposit(cacheTaxAccount, total * tax);
}
eco.deposit(cacheTaxAccount, total * tax);
}
// Notify the owner of the purchase.
String msg = MsgUtil.getMessage("player-sold-to-your-store", p, p.getName(), String.valueOf(amount), "##########" + Util.serialize(shop.getItem()) + "##########");
@@ -307,9 +296,7 @@ public class ShopManager {
return;
}
try {
if (cacheTaxAccount != null) {
plugin.getEconomy().deposit(cacheTaxAccount, createCost);
}
plugin.getEconomy().deposit(cacheTaxAccount, createCost);
} catch (Exception e2) {
e2.printStackTrace();
plugin.getLogger().log(Level.WARNING, "QuickShop can't pay tax to account in config.yml, Please set tax account name to a existing player!");
@@ -573,7 +560,7 @@ public class ShopManager {
* yourself
*
* @param world The name of the world
* @param shop The shop to add
* @param shop The shop to add
*/
public void addShop(@NotNull String world, @NotNull Shop shop) {
HashMap<ShopChunk, HashMap<Location, Shop>> inWorld = this.getShops().computeIfAbsent(world, k -> new HashMap<>(3));
@@ -597,8 +584,8 @@ public class ShopManager {
/**
* Checks other plugins to make sure they can use the chest they're making a shop.
*
* @param p The player to check
* @param b The block to check
* @param p The player to check
* @param b The block to check
* @param bf The blockface to check
* @return True if they're allowed to place a shop there.
*/
@@ -947,7 +934,7 @@ public class ShopManager {
* not use this method to create a shop.
*
* @param world The world the shop is in
* @param shop The shop to load
* @param shop The shop to load
*/
public void loadShop(@NotNull String world, @NotNull Shop shop) {
this.addShop(world, shop);
@@ -26,6 +26,13 @@ import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.wrappers.WrappedDataWatcher;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.atomic.AtomicInteger;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Location;
@@ -36,34 +43,37 @@ import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.maxgamer.quickshop.Util.Util;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.atomic.AtomicInteger;
public class VirtualDisplayItem extends DisplayItem {
private static ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager();
private volatile boolean isDisplay;
//counter for ensuring ID is unique
private static final AtomicInteger counter = new AtomicInteger(0);
//server main version (1_13_R2->13)
private static final int version = Integer.parseInt(Util.getNMSVersion().split("_")[1]);
//unique EntityID
private int entityID = counter.decrementAndGet();
//packets
private PacketContainer fakeItemPacket;
private PacketContainer fakeItemMetaPacket;
private PacketContainer fakeItemVelocityPacket;
private PacketContainer fakeItemDestroyPacket;
//packetListener
private PacketAdapter packetAdapter;
//cache chunk x and z
private ShopChunk chunkLocation;
//The List which store packet sender
private Set<UUID> packetSenders = new ConcurrentSkipListSet<>();
@@ -101,26 +111,28 @@ public class VirtualDisplayItem extends DisplayItem {
//chunk z
int z = event.getPacket().getIntegers().read(1);
//check later to prevent deadlock
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin,()->{
if(chunkLocation==null){
World world = shop.getLocation().getWorld();
Chunk chunk = shop.getLocation().getChunk();
chunkLocation=new ShopChunk(world.getName(),chunk.getX(),chunk.getZ());
}
if (chunkLocation.isSame(event.getPlayer().getWorld().getName(),x,z)) {
packetSenders.add(event.getPlayer().getUniqueId());
sendFakeItem(event.getPlayer());
}
},1);
}};
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, () -> {
if (chunkLocation == null) {
World world = shop.getLocation().getWorld();
Chunk chunk = shop.getLocation().getChunk();
chunkLocation = new ShopChunk(world.getName(), chunk.getX(), chunk.getZ());
}
if (chunkLocation.isSame(event.getPlayer().getWorld().getName(), x, z)) {
packetSenders.add(event.getPlayer().getUniqueId());
sendFakeItem(event.getPlayer());
}
}, 1);
}
};
}
protocolManager.addPacketListener(packetAdapter);
}
private void unload(){
private void unload() {
packetSenders.clear();
protocolManager.removePacketListener(packetAdapter);
}
private void initFakeDropItemPacket() {
//First, create a new packet to spawn item
@@ -132,18 +144,18 @@ public class VirtualDisplayItem extends DisplayItem {
//and add data based on packet class in NMS (global scope variable)
//Reference: https://wiki.vg/Protocol#Spawn_Object
fakeItemPacket.getIntegers()
//Entity ID
.write(0, entityID)
//Velocity x
.write(1, 0)
//Velocity y
.write(2, 0)
//Velocity z
.write(3, 0)
//Pitch
.write(4, 0)
//Yaw
.write(5, 0);
//Entity ID
.write(0, entityID)
//Velocity x
.write(1, 0)
//Velocity y
.write(2, 0)
//Velocity z
.write(3, 0)
//Pitch
.write(4, 0)
//Yaw
.write(5, 0);
if (version == 13) {
@@ -162,12 +174,12 @@ public class VirtualDisplayItem extends DisplayItem {
fakeItemPacket.getUUIDs().write(0, UUID.randomUUID());
//Location
fakeItemPacket.getDoubles()
//X
.write(0, location.getX())
//Y
.write(1, location.getY())
//Z
.write(2, location.getZ());
//X
.write(0, location.getX())
//Y
.write(1, location.getY())
//Z
.write(2, location.getZ());
//Next, create a new packet to update item data (default is empty)
fakeItemMetaPacket = protocolManager.createPacket(PacketType.Play.Server.ENTITY_METADATA);
@@ -186,14 +198,14 @@ public class VirtualDisplayItem extends DisplayItem {
//And, create a entity velocity packet to make it at a proper location (otherwise it will fly randomly)
fakeItemVelocityPacket = protocolManager.createPacket(PacketType.Play.Server.ENTITY_VELOCITY);
fakeItemVelocityPacket.getIntegers()
//Entity ID
.write(0, entityID)
//Velocity x
.write(1, 0)
//Velocity y
.write(2, 0)
//Velocity z
.write(3, 0);
//Entity ID
.write(0, entityID)
//Velocity x
.write(1, 0)
//Velocity y
.write(2, 0)
//Velocity z
.write(3, 0);
//Also make a DestroyPacket to remove it
fakeItemDestroyPacket = protocolManager.createPacket(PacketType.Play.Server.ENTITY_DESTROY);