Fixed backwards compatibility with older server software/versions

This commit is contained in:
Intelli 2022-02-25 19:16:06 -07:00
parent 659fe649ed
commit f5d13f8818
8 changed files with 96 additions and 10 deletions

View File

@ -19,6 +19,7 @@ import net.coreprotect.language.Language;
import net.coreprotect.language.Phrase;
import net.coreprotect.listener.ListenerHandler;
import net.coreprotect.listener.player.PlayerQuitListener;
import net.coreprotect.paper.PaperAdapter;
import net.coreprotect.thread.CacheHandler;
import net.coreprotect.thread.NetworkHandler;
import net.coreprotect.utility.Chat;
@ -161,7 +162,7 @@ public final class CoreProtect extends JavaPlugin {
private static void safeShutdown(CoreProtect plugin) {
try {
/* if server is stopping, log disconnections of online players */
if (plugin.getServer().isStopping()) {
if (PaperAdapter.ADAPTER.isStopping(plugin.getServer())) {
for (Player player : plugin.getServer().getOnlinePlayers()) {
PlayerQuitListener.queuePlayerQuit(player);
}

View File

@ -1,17 +1,54 @@
package net.coreprotect.listener;
import org.bukkit.Material;
import org.bukkit.entity.AbstractArrow;
import org.bukkit.entity.Arrow;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerPickupArrowEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionType;
import net.coreprotect.listener.entity.EntityPickupItemListener;
public class PlayerPickupArrowListener extends EntityPickupItemListener implements Listener {
public static ItemStack getArrowType(AbstractArrow arrow) {
ItemStack itemStack = null;
switch (arrow.getType()) {
case SPECTRAL_ARROW:
itemStack = new ItemStack(Material.SPECTRAL_ARROW);
break;
default:
itemStack = new ItemStack(Material.ARROW);
break;
}
if (arrow instanceof Arrow) {
Arrow arrowEntity = (Arrow) arrow;
PotionData data = arrowEntity.getBasePotionData();
if (data.getType() != PotionType.UNCRAFTABLE) {
itemStack = new ItemStack(Material.TIPPED_ARROW);
PotionMeta meta = (PotionMeta) itemStack.getItemMeta();
meta.setBasePotionData(data);
for (PotionEffect effect : arrowEntity.getCustomEffects()) {
meta.addCustomEffect(effect, false);
}
itemStack.setItemMeta(meta);
}
}
return itemStack;
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
protected void onPlayerPickupArrowEvent(PlayerPickupArrowEvent event) {
EntityPickupItemListener.onItemPickup(event.getPlayer(), event.getArrow().getLocation(), event.getArrow().getItemStack());
ItemStack itemStack = getArrowType(event.getArrow());
EntityPickupItemListener.onItemPickup(event.getPlayer(), event.getArrow().getLocation(), itemStack);
}
}

View File

@ -660,7 +660,7 @@ public final class PlayerInteractListener extends Queue implements Listener {
}
if (event.useItemInHand() != Event.Result.DENY) {
List<Material> entityBlockTypes = Arrays.asList(Material.ARMOR_STAND, Material.END_CRYSTAL, Material.TRIDENT, Material.EXPERIENCE_BOTTLE, Material.SPLASH_POTION, Material.ENDER_PEARL, Material.FIREWORK_ROCKET);
List<Material> entityBlockTypes = Arrays.asList(Material.ARMOR_STAND, Material.END_CRYSTAL, Material.BOW, Material.CROSSBOW, Material.TRIDENT, Material.EXPERIENCE_BOTTLE, Material.SPLASH_POTION, Material.ENDER_PEARL, Material.FIREWORK_ROCKET, Material.EGG);
ItemStack handItem = null;
ItemStack mainHand = player.getInventory().getItemInMainHand();
ItemStack offHand = player.getInventory().getItemInOffHand();

View File

@ -1,34 +1,39 @@
package net.coreprotect.listener.player;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Egg;
import org.bukkit.entity.AbstractArrow;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityShootBowEvent;
import org.bukkit.event.entity.ProjectileLaunchEvent;
import org.bukkit.event.player.PlayerEggThrowEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.CrossbowMeta;
import net.coreprotect.config.Config;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.consumer.Queue;
import net.coreprotect.database.logger.ItemLogger;
import net.coreprotect.listener.PlayerPickupArrowListener;
import net.coreprotect.utility.Util;
public class ProjectileLaunchListener extends Queue implements Listener {
public static Set<Material> BOWS = new HashSet<>(Arrays.asList(Material.BOW, Material.CROSSBOW));
protected static void playerLaunchProjectile(Location location, String user, ItemStack itemStack, int amount, int delay, int offset, int action) {
if (!Config.getConfig(location.getWorld()).ITEM_DROPS || itemStack == null) {
return;
@ -66,15 +71,37 @@ public class ProjectileLaunchListener extends Queue implements Listener {
Map.Entry<UUID, Object[]> pair = it.next();
UUID uuid = pair.getKey();
Object[] data = pair.getValue();
if ((data[0].equals(key) || data[1].equals(key)) && Util.getEntityMaterial(event.getEntityType()) == ((ItemStack) data[2]).getType()) {
Player player = Bukkit.getServer().getPlayer(uuid);
ItemStack itemStack = (ItemStack) data[2];
playerLaunchProjectile(location, player.getName(), itemStack, 1, 1, 0, (itemStack.getType() != Material.FIREWORK_ROCKET ? ItemLogger.ITEM_THROW : ItemLogger.ITEM_SHOOT));
boolean isBow = BOWS.contains(itemStack.getType());
if ((data[0].equals(key) || data[1].equals(key)) && (Util.getEntityMaterial(event.getEntityType()) == itemStack.getType() || isBow)) {
Player player = Bukkit.getServer().getPlayer(uuid);
boolean thrownItem = (itemStack.getType() != Material.FIREWORK_ROCKET && !isBow);
if (isBow) {
if (itemStack.getType() == Material.CROSSBOW) {
CrossbowMeta meta = (CrossbowMeta) itemStack.getItemMeta();
for (ItemStack item : meta.getChargedProjectiles()) {
itemStack = item;
break;
}
}
else if (event.getEntity() instanceof AbstractArrow) {
itemStack = PlayerPickupArrowListener.getArrowType((AbstractArrow) event.getEntity());
}
if (itemStack == null || BOWS.contains(itemStack.getType())) {
return; // unnecessary under normal circumstances
}
}
playerLaunchProjectile(location, player.getName(), itemStack, 1, 1, 0, (thrownItem ? ItemLogger.ITEM_THROW : ItemLogger.ITEM_SHOOT));
it.remove();
}
}
}
// Requires Bukkit 1.17+
/*
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
protected void onEntityShootBow(EntityShootBowEvent event) {
if (!(event.getEntity() instanceof Player)) {
@ -84,12 +111,15 @@ public class ProjectileLaunchListener extends Queue implements Listener {
ItemStack itemStack = event.getConsumable();
playerLaunchProjectile(event.getEntity().getLocation(), event.getEntity().getName(), itemStack, 1, 1, 0, ItemLogger.ITEM_SHOOT);
}
*/
// Requires Bukkit 1.16+
/*
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
protected void onPlayerEggThrow(PlayerEggThrowEvent event) {
Egg egg = event.getEgg();
ItemStack itemStack = egg.getItem();
playerLaunchProjectile(event.getPlayer().getLocation(), event.getPlayer().getName(), itemStack, 0, 1, 0, ItemLogger.ITEM_THROW);
}
*/
}

View File

@ -1,5 +1,6 @@
package net.coreprotect.paper;
import org.bukkit.Server;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
@ -46,4 +47,9 @@ public class PaperAdapter implements PaperInterface {
return holder.getHolder();
}
@Override
public boolean isStopping(Server server) {
return false;
}
}

View File

@ -1,5 +1,12 @@
package net.coreprotect.paper;
import org.bukkit.Server;
public class PaperHandler extends PaperAdapter implements PaperInterface {
@Override
public boolean isStopping(Server server) {
return server.isStopping();
}
}

View File

@ -1,5 +1,6 @@
package net.coreprotect.paper;
import org.bukkit.Server;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
@ -7,4 +8,6 @@ public interface PaperInterface {
public InventoryHolder getHolder(Inventory holder, boolean useSnapshot);
public boolean isStopping(Server server);
}

View File

@ -811,6 +811,8 @@ public class Util extends Queue {
return Material.TRIDENT;
case FIREWORK:
return Material.FIREWORK_ROCKET;
case EGG:
return Material.EGG;
default:
return BukkitAdapter.ADAPTER.getFrameType(type);
}