feat: 优化代码

Signed-off-by: 502647092 <admin@yumc.pw>
dev
502647092 2017-05-15 00:29:54 +08:00
parent 833fe1ac43
commit 8916dc0d53
9 changed files with 69 additions and 74 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>org.maxgamer</groupId> <groupId>org.maxgamer</groupId>
<artifactId>QuickShop</artifactId> <artifactId>QuickShop</artifactId>
<version>2.1</version> <version>2.1.0</version>
<description>快捷商店重置版本...</description> <description>快捷商店重置版本...</description>
<build> <build>
<finalName>${project.name}</finalName> <finalName>${project.name}</finalName>

View File

@ -10,6 +10,7 @@ import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.maxgamer.QuickShop.Listeners.ChunkListener;
import org.maxgamer.QuickShop.QuickShop; import org.maxgamer.QuickShop.QuickShop;
import org.maxgamer.QuickShop.Shop.Item.DisplayItem; import org.maxgamer.QuickShop.Shop.Item.DisplayItem;
@ -89,11 +90,9 @@ public class ConfigManager {
this.guiTitle = config.getMessage("guititle", guiTitle); this.guiTitle = config.getMessage("guititle", guiTitle);
this.warnings = Collections.emptySet(); this.warnings = Collections.emptySet();
this.prevent = config.getStringList("prevent"); this.prevent = config.getStringList("prevent");
if (config.getBoolean("fakeitem", true)) { if (display) {
DisplayItem.init(); Bukkit.getServer().getPluginManager().registerEvents(new ChunkListener(), plugin);
} DisplayItem.init(config.getBoolean("fakeitem", true));
if (Bukkit.getVersion().contains("Paper") || Bukkit.getVersion().contains("Torch")) {
Log.d("辣鸡 Paper 毁我异步命令!");
} }
if (config.getBoolean("usemagiclib", true)) { if (config.getBoolean("usemagiclib", true)) {
try { try {

View File

@ -12,21 +12,17 @@ import org.bukkit.event.world.ChunkUnloadEvent;
import org.maxgamer.QuickShop.QuickShop; import org.maxgamer.QuickShop.QuickShop;
import org.maxgamer.QuickShop.Shop.Shop; import org.maxgamer.QuickShop.Shop.Shop;
public class ChunkListener implements Listener { import pw.yumc.YumCore.bukkit.P;
private QuickShop plugin;
public ChunkListener(QuickShop plugin) { public class ChunkListener implements Listener {
this.plugin = plugin; private QuickShop plugin = P.getPlugin();
}
@EventHandler(priority = EventPriority.HIGHEST) @EventHandler(priority = EventPriority.HIGHEST)
public void onChunkLoad(ChunkLoadEvent e) { public void onChunkLoad(ChunkLoadEvent e) {
Chunk c = e.getChunk(); Chunk c = e.getChunk();
if (plugin.getShopManager().getShops() == null) if (plugin.getShopManager().getShops() == null) return;
return;
HashMap<Location, Shop> inChunk = plugin.getShopManager().getShops(c); HashMap<Location, Shop> inChunk = plugin.getShopManager().getShops(c);
if (inChunk == null) if (inChunk == null) return;
return;
for (Shop shop : inChunk.values()) { for (Shop shop : inChunk.values()) {
shop.onLoad(); shop.onLoad();
} }
@ -36,8 +32,7 @@ public class ChunkListener implements Listener {
public void onChunkUnload(ChunkUnloadEvent e) { public void onChunkUnload(ChunkUnloadEvent e) {
Chunk c = e.getChunk(); Chunk c = e.getChunk();
HashMap<Location, Shop> inChunk = plugin.getShopManager().getShops(c); HashMap<Location, Shop> inChunk = plugin.getShopManager().getShops(c);
if (inChunk == null) if (inChunk == null) return;
return;
for (Shop shop : inChunk.values()) { for (Shop shop : inChunk.values()) {
shop.onUnload(); shop.onUnload();
} }

View File

@ -36,6 +36,7 @@ import org.maxgamer.QuickShop.Economy.EconomyCore;
import org.maxgamer.QuickShop.Economy.Economy_Vault; import org.maxgamer.QuickShop.Economy.Economy_Vault;
import org.maxgamer.QuickShop.Listeners.BlockListener; import org.maxgamer.QuickShop.Listeners.BlockListener;
import org.maxgamer.QuickShop.Listeners.ChatListener; import org.maxgamer.QuickShop.Listeners.ChatListener;
import org.maxgamer.QuickShop.Listeners.ChunkListener;
import org.maxgamer.QuickShop.Listeners.LockListener; import org.maxgamer.QuickShop.Listeners.LockListener;
import org.maxgamer.QuickShop.Listeners.PlayerListener; import org.maxgamer.QuickShop.Listeners.PlayerListener;
import org.maxgamer.QuickShop.Listeners.ProtectListener; import org.maxgamer.QuickShop.Listeners.ProtectListener;
@ -253,11 +254,11 @@ public class QuickShop extends JavaPlugin {
@Override @Override
public void onEnable() { public void onEnable() {
if (!loadEcon()) { return; } if (!loadEcon()) { return; }
configManager = new ConfigManager(this);
// Initialize Util
Util.initialize(); Util.initialize();
// Create the shop manager. // Create the shop manager.
shopManager = new ShopManager(this); shopManager = new ShopManager(this);
configManager = new ConfigManager(this);
// Initialize Util
if (configManager.isLogAction()) { if (configManager.isLogAction()) {
// Logger Handler // Logger Handler
this.logWatcher = new LogWatcher(this, new File(this.getDataFolder(), "qs.log")); this.logWatcher = new LogWatcher(this, new File(this.getDataFolder(), "qs.log"));

View File

@ -17,6 +17,7 @@ import org.maxgamer.QuickShop.Watcher.ItemWatcher;
import pw.yumc.YumCore.bukkit.Log; import pw.yumc.YumCore.bukkit.Log;
import pw.yumc.YumCore.bukkit.P; import pw.yumc.YumCore.bukkit.P;
import pw.yumc.YumCore.bukkit.compatible.C;
/** /**
* @author Netherfoam A display item, that spawns a block above the chest and * @author Netherfoam A display item, that spawns a block above the chest and
@ -25,39 +26,53 @@ import pw.yumc.YumCore.bukkit.P;
public abstract class DisplayItem { public abstract class DisplayItem {
private static QuickShop plugin = P.getPlugin(); private static QuickShop plugin = P.getPlugin();
private static Class<? extends DisplayItem> displayItemClass; private static Class<? extends DisplayItem> displayItemClass = NormalItem.class;
private static Constructor<? extends DisplayItem> constructor; private static Constructor<? extends DisplayItem> constructor;
public static void init() { public static void init(boolean fakeItem) {
List<Class<? extends DisplayItem>> fakeItems = Arrays.asList(FakeItem_19_111.class, FakeItem_18.class, FakeItem_17.class); if (fakeItem) {
Log.i("启用虚拟悬浮物 尝试启动中..."); Log.i("启用虚拟悬浮物 尝试启动中...");
FakeItem.register(plugin); String nms = C.getNMSVersion();
for (Class<? extends DisplayItem> c : fakeItems) { Class<? extends DisplayItem> c = null;
try { switch (nms) {
c.getConstructor(Location.class, ItemStack.class).newInstance(new Location(Bukkit.getWorlds().get(0), 0, 0, 0), new ItemStack(Material.STONE)).spawn(); case "v1_7_R4":
displayItemClass = c; c = FakeItem_17.class;
Log.i("虚拟悬浮物功能测试正常(%s)...", c.getSimpleName());
break; break;
} catch (Throwable e) { case "v1_8_R3":
Log.d(e); c = FakeItem_18.class;
break;
case "v1_9_R1":
case "v1_9_R2":
case "v1_10_R1":
case "v1_11_R1":
c = FakeItem_19_111.class;
break;
}
if (c != null) {
FakeItem.register(plugin);
try {
c.getConstructor(Location.class, ItemStack.class).newInstance(new Location(Bukkit.getWorlds().get(0), 0, 0, 0), new ItemStack(Material.STONE)).spawn();
displayItemClass = c;
Log.i("虚拟悬浮物功能测试正常(%s)...", c.getSimpleName());
} catch (Throwable e) {
Log.d(e);
Log.w("+=========================================");
Log.w("| 警告: 启动虚拟物品失败 使用原版悬浮物品...");
Log.w("+=========================================");
}
} else {
Log.i("没有可用的虚拟物品类 使用原版悬浮物品...");
} }
} }
if (displayItemClass == null) { try {
displayItemClass = NormalItem.class; constructor = displayItemClass.getConstructor(Location.class, ItemStack.class);
if (plugin.getConfigManager().isDisplay()) { if (displayItemClass == NormalItem.class) {
Bukkit.getServer().getPluginManager().registerEvents(new ChunkListener(plugin), plugin);
// Display item handler thread // Display item handler thread
Log.i("开启商店检查以及悬浮物刷新线程..."); Log.i("开启商店检查以及悬浮物刷新线程...");
final ItemWatcher itemWatcher = new ItemWatcher(plugin); final ItemWatcher itemWatcher = new ItemWatcher(plugin);
plugin.itemWatcherTask = Bukkit.getScheduler().runTaskTimer(plugin, itemWatcher, 20, 1800); plugin.itemWatcherTask = Bukkit.getScheduler().runTaskTimer(plugin, itemWatcher, 20, 1800);
} }
Log.w("+=========================================");
Log.w("| 警告: 启动虚拟物品失败 使用原版悬浮物品...");
Log.w("+=========================================");
}
try {
constructor = displayItemClass.getConstructor(Location.class, ItemStack.class);
} catch (NoSuchMethodException ignored) { } catch (NoSuchMethodException ignored) {
} }
} }

View File

@ -37,14 +37,12 @@ public abstract class FakeItem extends DisplayItem {
protected final ItemStack itemStack; protected final ItemStack itemStack;
protected final Location location; protected final Location location;
protected final int eid; protected final int eid;
protected final UUID uuid;
protected boolean created = false; protected boolean created = false;
public FakeItem(Location loc, final ItemStack item) { public FakeItem(Location loc, final ItemStack item) {
this.itemStack = item; this.itemStack = item;
this.location = loc.clone().add(0.5, 1, 0.5); this.location = loc.clone().add(0.5, 1, 0.5);
this.eid = getFakeEntityId(); this.eid = getFakeEntityId();
this.uuid = UUID.randomUUID();
} }
public static boolean isRegistered() { public static boolean isRegistered() {
@ -183,13 +181,13 @@ public abstract class FakeItem extends DisplayItem {
created = false; created = false;
} }
private PacketContainer getDestoryPacket() { protected PacketContainer getDestoryPacket() {
final PacketContainer fakePacket = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.ENTITY_DESTROY, true); final PacketContainer fakePacket = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.ENTITY_DESTROY, true);
fakePacket.getIntegerArrays().write(0, new int[] { eid }); fakePacket.getIntegerArrays().write(0, new int[] { eid });
return fakePacket; return fakePacket;
} }
private PacketContainer getVelocityPacket() { protected PacketContainer getVelocityPacket() {
final PacketContainer fakePacket = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.ENTITY_VELOCITY); final PacketContainer fakePacket = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.ENTITY_VELOCITY);
fakePacket.getIntegers().write(0, eid); fakePacket.getIntegers().write(0, eid);
return fakePacket; return fakePacket;

View File

@ -44,6 +44,17 @@ public class FakeItem_17 extends FakeItem {
is.write(1, getNormalizedDistance(location.getX())); is.write(1, getNormalizedDistance(location.getX()));
is.write(2, getNormalizedDistance(location.getY())); is.write(2, getNormalizedDistance(location.getY()));
is.write(3, getNormalizedDistance(location.getZ())); is.write(3, getNormalizedDistance(location.getZ()));
is.write(9, 2);
return fakePacket; return fakePacket;
} }
@Override
protected PacketContainer getVelocityPacket() {
PacketContainer packet = super.getVelocityPacket();
StructureModifier<Integer> pint = packet.getIntegers();
pint.write(1, 0);
pint.write(2, 0);
pint.write(3, 0);
return packet;
}
} }

View File

@ -1,15 +1,8 @@
package org.maxgamer.QuickShop.Shop.Item; package org.maxgamer.QuickShop.Shop.Item;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.reflect.StructureModifier;
import com.comphenix.protocol.wrappers.WrappedWatchableObject;
/** /**
* Minecraft * Minecraft
* depend ProtocolLib * depend ProtocolLib
@ -17,7 +10,7 @@ import com.comphenix.protocol.wrappers.WrappedWatchableObject;
* @author (chengzi) * @author (chengzi)
* @version 1.0.1 * @version 1.0.1
*/ */
public class FakeItem_18 extends FakeItem { public class FakeItem_18 extends FakeItem_17 {
public FakeItem_18(Location loc, final ItemStack item) { public FakeItem_18(Location loc, final ItemStack item) {
super(loc, item); super(loc, item);
@ -26,24 +19,4 @@ public class FakeItem_18 extends FakeItem {
private static int getNormalizedDistance(final double value) { private static int getNormalizedDistance(final double value) {
return (int) Math.floor(value * 32.0D); return (int) Math.floor(value * 32.0D);
} }
@Override
protected PacketContainer setMetadataPacket(PacketContainer fakePacket) {
fakePacket.getIntegers().write(0, eid);
final WrappedWatchableObject itemMeta = new WrappedWatchableObject(5, itemStack);
final List<WrappedWatchableObject> entityMetaList = new ArrayList<>(1);
entityMetaList.add(itemMeta);
fakePacket.getWatchableCollectionModifier().write(0, entityMetaList);
return fakePacket;
}
@Override
protected PacketContainer setSpawnPacket(PacketContainer fakePacket) {
StructureModifier<Integer> is = fakePacket.getIntegers();
is.write(0, eid);
is.write(1, getNormalizedDistance(location.getX()));
is.write(2, getNormalizedDistance(location.getY()));
is.write(3, getNormalizedDistance(location.getZ()));
return fakePacket;
}
} }

View File

@ -1,6 +1,6 @@
package org.maxgamer.QuickShop.Shop.Item; package org.maxgamer.QuickShop.Shop.Item;
import java.util.Optional; import java.util.UUID;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -10,6 +10,7 @@ import com.comphenix.protocol.reflect.StructureModifier;
import com.comphenix.protocol.wrappers.WrappedDataWatcher; import com.comphenix.protocol.wrappers.WrappedDataWatcher;
import com.comphenix.protocol.wrappers.WrappedDataWatcher.Serializer; import com.comphenix.protocol.wrappers.WrappedDataWatcher.Serializer;
import com.comphenix.protocol.wrappers.WrappedDataWatcher.WrappedDataWatcherObject; import com.comphenix.protocol.wrappers.WrappedDataWatcher.WrappedDataWatcherObject;
import com.google.common.base.Optional;
/** /**
* Minecraft * Minecraft
@ -19,9 +20,11 @@ import com.comphenix.protocol.wrappers.WrappedDataWatcher.WrappedDataWatcherObje
* @version 1.1.0 * @version 1.1.0
*/ */
public class FakeItem_19_111 extends FakeItem { public class FakeItem_19_111 extends FakeItem {
protected final UUID uuid;
public FakeItem_19_111(Location loc, final ItemStack item) { public FakeItem_19_111(Location loc, final ItemStack item) {
super(loc, item); super(loc, item);
uuid = UUID.randomUUID();
} }
@Override @Override