Bukkit API Wrapper to allow plugin use Paper API when using Paper

Former-commit-id: cc21a6e1cbd9b93358da35a1a85fbede0cfdddb9
This commit is contained in:
Ghost_chu committed 2019-12-30 22:48:05 +08:00
1 parent c0e203353d
commit bbeaed23d1
10 files changed
+244 -23

No files matched your search

@@ -34,6 +34,7 @@ import org.maxgamer.quickshop.Util.Util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CompletableFuture;
public class SubCommand_Find implements CommandProcesser {
@@ -70,8 +71,18 @@ public class SubCommand_Find implements CommandProcesser {
double minDistanceSquared = minDistance * minDistance;
final int chunkRadius = (int) minDistance / 16 + 1;
Shop closest = null;
final Chunk c = loc.getChunk();
CompletableFuture<Chunk> future = new CompletableFuture<>();
plugin.getBukkitAPIWrapper().getChunkAt(loc.getWorld(),loc, future);
final Chunk c;
try {
c = future.get();
}catch (Exception asyncErr){
sender.sendMessage("Cannot execute the command, see console for details.");
plugin.getSentryErrorReporter().sendError(asyncErr,"Unknown errors");
plugin.getSentryErrorReporter().ignoreThrow();
asyncErr.printStackTrace();
return;
}
for (int x = -chunkRadius + c.getX(); x < chunkRadius + c.getX(); x++) {
for (int z = -chunkRadius + c.getZ(); z < chunkRadius + c.getZ(); z++) {
final Chunk d = c.getWorld().getChunkAt(x, z);
@@ -103,7 +114,7 @@ public class SubCommand_Find implements CommandProcesser {
final Location lookat = closest.getLocation().clone().add(0.5, 0.5, 0.5);
// Hack fix to make /qs find not used by /back
p.teleport(Util.lookAt(loc, lookat).add(0, -1.62, 0), PlayerTeleportEvent.TeleportCause.UNKNOWN);
plugin.getBukkitAPIWrapper().teleportEntity(p,Util.lookAt(loc, lookat).add(0, -1.62, 0), PlayerTeleportEvent.TeleportCause.UNKNOWN);
p.sendMessage(
MsgUtil.getMessage("nearby-shop-this-way", sender, "" + (int) Math.floor(Math.sqrt(minDistanceSquared))));
}
@@ -76,6 +76,7 @@ public class WorldListener implements Listener {
}
}
// Done - Now we can store the new world dataz!
plugin.getShopManager().getShops().put(world.getName(), inWorld);
// This is a workaround, because I don't get parsed chunk events when a
// world first loads....
@@ -45,6 +45,9 @@ import org.maxgamer.quickshop.Shop.Shop;
import org.maxgamer.quickshop.Shop.ShopLoader;
import org.maxgamer.quickshop.Shop.ShopManager;
import org.maxgamer.quickshop.Util.*;
import org.maxgamer.quickshop.Util.ServerForkWrapper.BukkitAPIWrapper;
import org.maxgamer.quickshop.Util.ServerForkWrapper.PaperWrapper;
import org.maxgamer.quickshop.Util.ServerForkWrapper.SpigotWrapper;
import org.maxgamer.quickshop.Util.Timer;
import org.maxgamer.quickshop.Util.OneSkyAppPlatform.LanguageOTA;
import org.maxgamer.quickshop.Watcher.*;
@@ -188,6 +191,7 @@ public class QuickShop extends JavaPlugin {
private SignUpdateWatcher signUpdateWatcher;
private ShopContainerWatcher shopContainerWatcher;
private LanguageOTA languageOTA;
private BukkitAPIWrapper bukkitAPIWrapper;
/**
* Get the Player's Shop limit.
@@ -438,11 +442,27 @@ public class QuickShop extends JavaPlugin {
serverUniqueID = UUID.fromString(getConfig().getString("server-uuid", String.valueOf(UUID.randomUUID())));
sentryErrorReporter = new SentryErrorReporter(this);
// loadEcon();
switch (getConfig().getInt("server-platform",0)){
case 1:
bukkitAPIWrapper = new SpigotWrapper();
getLogger().info("Plugin now running under Spigot mode. Paper performance profile is disabled, if you switch to Paper, we can use a lot paper api to improve the server performance.");
case 2:
bukkitAPIWrapper = new PaperWrapper();
getLogger().info("Plugin now running under Paper mode.");
default: //AUTO
try {
Bukkit.spigot().getSpigotConfig();
bukkitAPIWrapper = new PaperWrapper();
getLogger().info("Plugin now running under Paper mode.");
}catch (Exception e){
bukkitAPIWrapper = new SpigotWrapper();
getLogger().info("Plugin now running under Spigot mode. Paper performance profile is disabled, if you switch to Paper, we can use a lot paper api to improve the server performance.");
}
}
/* Initalize the Utils */
itemMatcher = new ItemMatcher(this);
Util.initialize();
languageOTA = new LanguageOTA(this);
MsgUtil.loadItemi18n();
@@ -1120,8 +1140,11 @@ public class QuickShop extends JavaPlugin {
if(Objects.equals(getConfig().getString("language"), "en")){
getConfig().set("language","en-US");
}
getConfig().set("server-platform",0);
getConfig().set("config-version", 72);
}
saveConfig();
reloadConfig();
File file = new File(getDataFolder(), "example.config.yml");
@@ -24,7 +24,9 @@ import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -98,7 +100,7 @@ public class RealDisplayItem implements DisplayItem {
if (eItem.getUniqueId().equals(Objects.requireNonNull(this.item).getUniqueId())) {
Util.debugLog("Fixing moved Item displayItem " + eItem.getUniqueId() + " at " + eItem
.getLocation());
eItem.teleport(Objects.requireNonNull(getDisplayLocation()));
plugin.getBukkitAPIWrapper().teleportEntity(eItem,Objects.requireNonNull(getDisplayLocation()), PlayerTeleportEvent.TeleportCause.UNKNOWN);
return;
}
}
@@ -121,14 +123,31 @@ public class RealDisplayItem implements DisplayItem {
ShopDisplayItemDespawnEvent shopDisplayItemDepawnEvent = new ShopDisplayItemDespawnEvent(shop, originalItemStack, DisplayType.REALITEM);
Bukkit.getPluginManager().callEvent(shopDisplayItemDepawnEvent);
}
@Override
public boolean removeDupe() {
if (this.item == null) {
Util.debugLog("Warning: Trying to removeDupe for a null display shop.");
return false;
}
boolean removed = false;
Boolean removed = null;
try { //We use async remove to remove the drops, it have better performance.
new BukkitRunnable() {
@Override
public void run() {
removeDupeMethod(removed);
}
}.runTaskAsynchronously(plugin);
}catch (Exception e){ //But we know, bukkit not recommend run any method on off-thread, if bukkit have any exceptions, we fallback to sync way.
removeDupeMethod(removed);
}
return removed;
}
public boolean removeDupeMethod(Boolean removed) {
if (this.item == null) {
Util.debugLog("Warning: Trying to removeDupe for a null display shop.");
return false;
}
//Chunk chunk = shop.getLocation().getChunk();
for (Entity entity : item.getNearbyEntities(1, 1, 1)) {
if (!(entity instanceof Item)) {
@@ -136,8 +155,6 @@ public class RealDisplayItem implements DisplayItem {
}
Item eItem = (Item) entity;
if (!DisplayItem.checkIsGuardItemStack(eItem.getItemStack())) {
Util.debugLog(Util
.getItemStackName(eItem.getItemStack()) + " not a shop displayItem: Failed check guardedItemStack");
continue;
}
if (!eItem.getUniqueId().equals(this.item.getUniqueId())) {
@@ -146,15 +163,11 @@ public class RealDisplayItem implements DisplayItem {
.getLocation());
entity.remove();
removed = true;
} else {
Util.debugLog(Util
.getItemStackName(eItem.getItemStack()) + " not a shop displayItem: Failed check shop display.");
}
}
}
return removed;
}
@Override
public void respawn() {
remove();
@@ -207,7 +220,6 @@ public class RealDisplayItem implements DisplayItem {
Util.debugLog("Canceled the displayItem spawning because a plugin setCancelled the spawning event, usually this is a QuickShop Add on");
return;
}
this.item = this.shop.getLocation().getWorld().dropItem(getDisplayLocation(), originalItemStack);
this.item.setPickupDelay(Integer.MAX_VALUE);
this.item.setSilent(true);
@@ -0,0 +1,38 @@
/*
* This file is a part of project QuickShop, the name is BukkitAPIWrapper.java
* Copyright (C) Ghost_chu <https://github.com/Ghost-chu>
* Copyright (C) Bukkit Commons Studio and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.maxgamer.quickshop.Util.ServerForkWrapper;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.concurrent.CompletableFuture;
public interface BukkitAPIWrapper {
void teleportEntity(@NotNull Entity entity, @NotNull Location location, @Nullable PlayerTeleportEvent.TeleportCause cause);
void getChunkAt(@NotNull World world,@NotNull Location location, @NotNull CompletableFuture<Chunk> futureTask);
void getChunkAt(@NotNull World world,int x,int z, @NotNull CompletableFuture<Chunk> futureTask);
void getChunkAt(@NotNull World world, @NotNull Block block, @NotNull CompletableFuture<Chunk> futureTask);
}
@@ -0,0 +1,70 @@
/*
* This file is a part of project QuickShop, the name is PaperWrapper.java
* Copyright (C) Ghost_chu <https://github.com/Ghost-chu>
* Copyright (C) Bukkit Commons Studio and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.maxgamer.quickshop.Util.ServerForkWrapper;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public class PaperWrapper implements BukkitAPIWrapper{
@Override
public void teleportEntity(@NotNull Entity entity, @NotNull Location location, @Nullable PlayerTeleportEvent.TeleportCause cause) {
if(cause == null) {
entity.teleportAsync(location);
}else {
entity.teleportAsync(location,cause);
}
}
@Override
public void getChunkAt(@NotNull World world, @NotNull Location location, @NotNull CompletableFuture<Chunk> futureTask) {
try {
futureTask.complete(world.getChunkAtAsync(location).get());
} catch (InterruptedException | ExecutionException e) {
futureTask.complete(world.getChunkAt(location));
}
}
@Override
public void getChunkAt(@NotNull World world, int x, int z, @NotNull CompletableFuture<Chunk> futureTask) {
try {
futureTask.complete(world.getChunkAtAsync(x,z).get());
} catch (InterruptedException | ExecutionException e) {
futureTask.complete(world.getChunkAt(x,z));
}
}
@Override
public void getChunkAt(@NotNull World world, @NotNull Block block, @NotNull CompletableFuture<Chunk> futureTask) {
try {
futureTask.complete(world.getChunkAtAsync(block).get());
} catch (InterruptedException | ExecutionException e) {
futureTask.complete(world.getChunkAt(block));
}
}
}
@@ -0,0 +1,58 @@
/*
* This file is a part of project QuickShop, the name is SpigotWrapper.java
* Copyright (C) Ghost_chu <https://github.com/Ghost-chu>
* Copyright (C) Bukkit Commons Studio and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.maxgamer.quickshop.Util.ServerForkWrapper;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.concurrent.CompletableFuture;
public class SpigotWrapper implements BukkitAPIWrapper {
@Override
public void teleportEntity(@NotNull Entity entity, @NotNull Location location, @Nullable PlayerTeleportEvent.TeleportCause cause) {
if(cause == null){
entity.teleport(location);
}else{
entity.teleport(location,cause);
}
}
@Override
public void getChunkAt(@NotNull World world, @NotNull Location location, @NotNull CompletableFuture<Chunk> futureTask) {
futureTask.complete(world.getChunkAt(location));
}
@Override
public void getChunkAt(@NotNull World world, int x, int z, @NotNull CompletableFuture<Chunk> futureTask) {
futureTask.complete(world.getChunkAt(x,z));
}
@Override
public void getChunkAt(@NotNull World world, @NotNull Block block, @NotNull CompletableFuture<Chunk> futureTask) {
futureTask.complete(world.getChunkAt(block));
}
}
@@ -740,11 +740,9 @@ public class Util {
public static boolean isDoubleChest(@Nullable Block b) {
if (b == null) {
Util.debugLog("The block is NULL.");
return false;
}
if (!(b.getState() instanceof Container)) {
Util.debugLog("Target block not a Container.");
return false;
}
Container container = (Container) b.getState();
+7 -1
View File
@@ -1,7 +1,7 @@
#This is example config, don't direct modify this file, all changes will revert when you reload the plugin.
#Do not touch if you not clear know about this
config-version: 71
config-version: 72
#Select the language you want to use, (e.g de), use only supported language codes from the list below.
#If you use a not exist/not support language, plugin will move to use en_US to keep everything working.
@@ -82,6 +82,12 @@ send-display-item-protection-alert: true
#Disable the debug silent logger. This should can fix heavy lagging on some overloaded server, but AFTER ENABLE IT, YOU WILL LOSE OUR SUPPORT UNTIL YOU RE-ENABLE IT.
disable-debuglogger: false
#Force plugin running under a platform
#0=AUTO
#1=SPIGOT
#2=PAPER
server-platform: 0
#MySQL database settings.
database:
# false = use local SQLite database, true = use local/remote MySQL database.