1
0
mirror of https://e.coding.net/circlecloud/QuickShop.git synced 2024-11-22 01:58:54 +00:00

feat: 优化代码

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
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>
<groupId>org.maxgamer</groupId>
<artifactId>QuickShop</artifactId>
<version>2.1</version>
<version>2.1.0</version>
<description>快捷商店重置版本...</description>
<build>
<finalName>${project.name}</finalName>

View File

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

View File

@ -12,21 +12,17 @@ import org.bukkit.event.world.ChunkUnloadEvent;
import org.maxgamer.QuickShop.QuickShop;
import org.maxgamer.QuickShop.Shop.Shop;
public class ChunkListener implements Listener {
private QuickShop plugin;
import pw.yumc.YumCore.bukkit.P;
public ChunkListener(QuickShop plugin) {
this.plugin = plugin;
}
public class ChunkListener implements Listener {
private QuickShop plugin = P.getPlugin();
@EventHandler(priority = EventPriority.HIGHEST)
public void onChunkLoad(ChunkLoadEvent e) {
Chunk c = e.getChunk();
if (plugin.getShopManager().getShops() == null)
return;
if (plugin.getShopManager().getShops() == null) return;
HashMap<Location, Shop> inChunk = plugin.getShopManager().getShops(c);
if (inChunk == null)
return;
if (inChunk == null) return;
for (Shop shop : inChunk.values()) {
shop.onLoad();
}
@ -36,8 +32,7 @@ public class ChunkListener implements Listener {
public void onChunkUnload(ChunkUnloadEvent e) {
Chunk c = e.getChunk();
HashMap<Location, Shop> inChunk = plugin.getShopManager().getShops(c);
if (inChunk == null)
return;
if (inChunk == null) return;
for (Shop shop : inChunk.values()) {
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.Listeners.BlockListener;
import org.maxgamer.QuickShop.Listeners.ChatListener;
import org.maxgamer.QuickShop.Listeners.ChunkListener;
import org.maxgamer.QuickShop.Listeners.LockListener;
import org.maxgamer.QuickShop.Listeners.PlayerListener;
import org.maxgamer.QuickShop.Listeners.ProtectListener;
@ -253,11 +254,11 @@ public class QuickShop extends JavaPlugin {
@Override
public void onEnable() {
if (!loadEcon()) { return; }
configManager = new ConfigManager(this);
// Initialize Util
Util.initialize();
// Create the shop manager.
shopManager = new ShopManager(this);
configManager = new ConfigManager(this);
// Initialize Util
if (configManager.isLogAction()) {
// Logger Handler
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.P;
import pw.yumc.YumCore.bukkit.compatible.C;
/**
* @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 {
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;
public static void init() {
List<Class<? extends DisplayItem>> fakeItems = Arrays.asList(FakeItem_19_111.class, FakeItem_18.class, FakeItem_17.class);
public static void init(boolean fakeItem) {
if (fakeItem) {
Log.i("启用虚拟悬浮物 尝试启动中...");
String nms = C.getNMSVersion();
Class<? extends DisplayItem> c = null;
switch (nms) {
case "v1_7_R4":
c = FakeItem_17.class;
break;
case "v1_8_R3":
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);
for (Class<? extends DisplayItem> c : fakeItems) {
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());
break;
} catch (Throwable e) {
Log.d(e);
Log.w("+=========================================");
Log.w("| 警告: 启动虚拟物品失败 使用原版悬浮物品...");
Log.w("+=========================================");
}
} else {
Log.i("没有可用的虚拟物品类 使用原版悬浮物品...");
}
}
if (displayItemClass == null) {
displayItemClass = NormalItem.class;
if (plugin.getConfigManager().isDisplay()) {
Bukkit.getServer().getPluginManager().registerEvents(new ChunkListener(plugin), plugin);
try {
constructor = displayItemClass.getConstructor(Location.class, ItemStack.class);
if (displayItemClass == NormalItem.class) {
// Display item handler thread
Log.i("开启商店检查以及悬浮物刷新线程...");
final ItemWatcher itemWatcher = new ItemWatcher(plugin);
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) {
}
}

View File

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

View File

@ -44,6 +44,17 @@ public class FakeItem_17 extends FakeItem {
is.write(1, getNormalizedDistance(location.getX()));
is.write(2, getNormalizedDistance(location.getY()));
is.write(3, getNormalizedDistance(location.getZ()));
is.write(9, 2);
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;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Location;
import org.bukkit.inventory.ItemStack;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.reflect.StructureModifier;
import com.comphenix.protocol.wrappers.WrappedWatchableObject;
/**
* Minecraft 虚拟悬浮物品工具类
* 需要depend ProtocolLib
@ -17,7 +10,7 @@ import com.comphenix.protocol.wrappers.WrappedWatchableObject;
* @author 橙子(chengzi)
* @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) {
super(loc, item);
@ -26,24 +19,4 @@ public class FakeItem_18 extends FakeItem {
private static int getNormalizedDistance(final double value) {
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;
import java.util.Optional;
import java.util.UUID;
import org.bukkit.Location;
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.Serializer;
import com.comphenix.protocol.wrappers.WrappedDataWatcher.WrappedDataWatcherObject;
import com.google.common.base.Optional;
/**
* Minecraft 虚拟悬浮物品工具类
@ -19,9 +20,11 @@ import com.comphenix.protocol.wrappers.WrappedDataWatcher.WrappedDataWatcherObje
* @version 1.1.0
*/
public class FakeItem_19_111 extends FakeItem {
protected final UUID uuid;
public FakeItem_19_111(Location loc, final ItemStack item) {
super(loc, item);
uuid = UUID.randomUUID();
}
@Override