Improve performance by using Paper's non-snapshot states if available

This commit is contained in:
Phoenix616 2021-01-28 17:18:42 +01:00
parent 22d3f262f5
commit 65df4c40c6
No known key found for this signature in database
GPG Key ID: 40E2321E71738EB0
15 changed files with 134 additions and 26 deletions

26
pom.xml
View File

@ -449,7 +449,7 @@
<id>default</id> <id>default</id>
<activation> <activation>
<property> <property>
<name>!spigot</name> <name>!dontrundefault</name>
</property> </property>
</activation> </activation>
<repositories> <repositories>
@ -462,7 +462,7 @@
<dependency> <dependency>
<groupId>com.destroystokyo.paper</groupId> <groupId>com.destroystokyo.paper</groupId>
<artifactId>paper-api</artifactId> <artifactId>paper-api</artifactId>
<version>1.13.2-R0.1-SNAPSHOT</version> <version>1.16.5-R0.1-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
</dependencies> </dependencies>
@ -485,7 +485,7 @@
</dependency> </dependency>
</dependencies> </dependencies>
<properties> <properties>
<spigot>true</spigot> <dontrundefault>true</dontrundefault>
</properties> </properties>
<build> <build>
<finalName>${project.name}-Spigot</finalName> <finalName>${project.name}-Spigot</finalName>
@ -503,7 +503,7 @@
</profile> </profile>
<profile> <profile>
<id>latest</id> <id>backwards</id>
<repositories> <repositories>
<repository> <repository>
<id>paper-repo</id> <id>paper-repo</id>
@ -514,10 +514,26 @@
<dependency> <dependency>
<groupId>com.destroystokyo.paper</groupId> <groupId>com.destroystokyo.paper</groupId>
<artifactId>paper-api</artifactId> <artifactId>paper-api</artifactId>
<version>1.16.4-R0.1-SNAPSHOT</version> <version>1.13.2-R0.1-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
</dependencies> </dependencies>
<properties>
<dontrundefault>true</dontrundefault>
</properties>
<build>
<finalName>${project.name}-1.13.2</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/PaperLatest*.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</profile> </profile>
<profile> <profile>

View File

@ -0,0 +1,51 @@
package com.Acrobot.Breeze.Utils;
import com.Acrobot.Breeze.Utils.ImplementationFeatures.PaperLatestHolder;
import com.Acrobot.Breeze.Utils.ImplementationFeatures.PaperLatestState;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import java.util.function.BiFunction;
public class ImplementationAdapter {
private static BiFunction<Inventory, Boolean, InventoryHolder> HOLDER_PROVIDER;
private static BiFunction<Block, Boolean, BlockState> STATE_PROVIDER;
static {
try {
InventoryHolder.class.getMethod("getHolder", boolean.class);
HOLDER_PROVIDER = PaperLatestHolder.PROVIDER;
} catch (NoSuchMethodException e) {
HOLDER_PROVIDER = (inventory, useSnapshot) -> inventory.getHolder();
}
try {
Block.class.getMethod("getState", boolean.class);
STATE_PROVIDER = PaperLatestState.PROVIDER;
} catch (NoSuchMethodException e) {
STATE_PROVIDER = (block, useSnapshot) -> block.getState();
}
}
/**
* Get the inventory's holder.
* @param inventory The inventory
* @param useSnapshot Whether or not the holder should be a snapshot (if possible)
* @return The inventory's holder
*/
public static InventoryHolder getHolder(Inventory inventory, boolean useSnapshot) {
return HOLDER_PROVIDER.apply(inventory, useSnapshot);
}
/**
* Get a block state
* @param block The block to get the state from
* @param useSnapshot Whether or not the state should be a snapshot (if possible)
* @return The block's state
*/
public static BlockState getState(Block block, boolean useSnapshot) {
return STATE_PROVIDER.apply(block, useSnapshot);
}
}

View File

@ -0,0 +1,12 @@
package com.Acrobot.Breeze.Utils.ImplementationFeatures;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import java.util.function.BiFunction;
public class PaperLatestHolder {
public static final BiFunction<Inventory, Boolean, InventoryHolder> PROVIDER = Inventory::getHolder;
}

View File

@ -0,0 +1,12 @@
package com.Acrobot.Breeze.Utils.ImplementationFeatures;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import java.util.function.BiFunction;
public class PaperLatestState {
public static final BiFunction<Block, Boolean, BlockState> PROVIDER = Block::getState;
}

View File

@ -28,6 +28,7 @@ import java.util.Collections;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import static com.Acrobot.Breeze.Utils.ImplementationAdapter.getState;
import static com.Acrobot.Breeze.Utils.BlockUtil.getAttachedBlock; import static com.Acrobot.Breeze.Utils.BlockUtil.getAttachedBlock;
import static com.Acrobot.Breeze.Utils.BlockUtil.isSign; import static com.Acrobot.Breeze.Utils.BlockUtil.isSign;
import static com.Acrobot.ChestShop.Permission.OTHER_NAME_DESTROY; import static com.Acrobot.ChestShop.Permission.OTHER_NAME_DESTROY;
@ -55,11 +56,11 @@ public class SignBreak implements Listener {
return; return;
} }
Sign sign = (Sign) block.getState(); Sign sign = (Sign) getState(block, false);
Block attachedBlock = BlockUtil.getAttachedBlock(sign); Block attachedBlock = BlockUtil.getAttachedBlock(sign);
if (attachedBlock.getType() == Material.AIR && ChestShopSign.isValid(sign)) { if (attachedBlock.getType() == Material.AIR && ChestShopSign.isValid(sign)) {
sendShopDestroyedEvent(sign, block.hasMetadata(METADATA_NAME) sendShopDestroyedEvent((Sign) block.getState(), block.hasMetadata(METADATA_NAME)
? (Player) block.getMetadata(METADATA_NAME).get(0).value() ? (Player) block.getMetadata(METADATA_NAME).get(0).value()
: null); : null);
} }

View File

@ -7,6 +7,8 @@ import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryMoveItemEvent; import org.bukkit.event.inventory.InventoryMoveItemEvent;
import static com.Acrobot.Breeze.Utils.ImplementationAdapter.getHolder;
/** /**
* @author Acrobot * @author Acrobot
*/ */
@ -14,11 +16,11 @@ public class ItemMoveListener implements Listener {
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
public static void onItemMove(InventoryMoveItemEvent event) { public static void onItemMove(InventoryMoveItemEvent event) {
if (event.getSource() == null || event.getDestination().getHolder() instanceof BlockState) { if (event.getSource() == null || getHolder(event.getDestination(), false) instanceof BlockState) {
return; return;
} }
if (!ChestShopSign.isShopBlock(event.getSource().getHolder())) { if (!ChestShopSign.isShopBlock(getHolder(event.getSource(), false))) {
return; return;
} }

View File

@ -22,6 +22,7 @@ import org.bukkit.inventory.ItemStack;
import java.util.IllegalFormatException; import java.util.IllegalFormatException;
import static com.Acrobot.Breeze.Utils.ImplementationAdapter.getHolder;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.NAME_LINE; import static com.Acrobot.ChestShop.Signs.ChestShopSign.NAME_LINE;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.ITEM_LINE; import static com.Acrobot.ChestShop.Signs.ChestShopSign.ITEM_LINE;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.QUANTITY_LINE; import static com.Acrobot.ChestShop.Signs.ChestShopSign.QUANTITY_LINE;
@ -67,11 +68,11 @@ public class StockCounterModule implements Listener {
@EventHandler @EventHandler
public static void onInventoryClose(InventoryCloseEvent event) { public static void onInventoryClose(InventoryCloseEvent event) {
if (event.getInventory().getType() == InventoryType.ENDER_CHEST || event.getInventory().getLocation() == null || !ChestShopSign.isShopBlock(event.getInventory().getLocation().getBlock())) { if (event.getInventory().getType() == InventoryType.ENDER_CHEST || event.getInventory().getLocation() == null || !uBlock.couldBeShopContainer(event.getInventory().getLocation().getBlock())) {
return; return;
} }
for (Sign shopSign : uBlock.findConnectedShopSigns(event.getInventory().getHolder())) { for (Sign shopSign : uBlock.findConnectedShopSigns(getHolder(event.getInventory(), false))) {
if (!Properties.USE_STOCK_COUNTER if (!Properties.USE_STOCK_COUNTER
|| (Properties.FORCE_UNLIMITED_ADMIN_SHOP && ChestShopSign.isAdminShop(shopSign))) { || (Properties.FORCE_UNLIMITED_ADMIN_SHOP && ChestShopSign.isAdminShop(shopSign))) {
if (QuantityUtil.quantityLineContainsCounter(shopSign.getLine(QUANTITY_LINE))) { if (QuantityUtil.quantityLineContainsCounter(shopSign.getLine(QUANTITY_LINE))) {
@ -113,7 +114,7 @@ public class StockCounterModule implements Listener {
return; return;
} }
for (Sign shopSign : uBlock.findConnectedShopSigns(event.getOwnerInventory().getHolder())) { for (Sign shopSign : uBlock.findConnectedShopSigns( getHolder(event.getOwnerInventory(), false))) {
updateCounterOnQuantityLine(shopSign, event.getOwnerInventory()); updateCounterOnQuantityLine(shopSign, event.getOwnerInventory());
} }
} }

View File

@ -37,6 +37,7 @@ import java.math.MathContext;
import java.util.Arrays; import java.util.Arrays;
import java.util.logging.Level; import java.util.logging.Level;
import static com.Acrobot.Breeze.Utils.ImplementationAdapter.getState;
import static com.Acrobot.Breeze.Utils.BlockUtil.isSign; import static com.Acrobot.Breeze.Utils.BlockUtil.isSign;
import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType; import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType;
import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType.BUY; import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType.BUY;
@ -76,7 +77,7 @@ public class PlayerInteract implements Listener {
if (!isSign(block) || player.getInventory().getItemInMainHand().getType().name().contains("SIGN")) // Blocking accidental sign edition if (!isSign(block) || player.getInventory().getItemInMainHand().getType().name().contains("SIGN")) // Blocking accidental sign edition
return; return;
Sign sign = (Sign) block.getState(); Sign sign = (Sign) getState(block, false);
if (!ChestShopSign.isValid(sign)) { if (!ChestShopSign.isValid(sign)) {
return; return;
} }

View File

@ -16,6 +16,8 @@ import org.bukkit.inventory.InventoryHolder;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static com.Acrobot.Breeze.Utils.ImplementationAdapter.getHolder;
/** /**
* @author Acrobot * @author Acrobot
*/ */
@ -30,7 +32,7 @@ public class PlayerInventory implements Listener {
return; return;
} }
InventoryHolder holder = event.getInventory().getHolder(); InventoryHolder holder = getHolder(event.getInventory(), false);
if (!(holder instanceof BlockState) && !(holder instanceof DoubleChest)) { if (!(holder instanceof BlockState) && !(holder instanceof DoubleChest)) {
return; return;
} }

View File

@ -9,6 +9,8 @@ import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerTeleportEvent; import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.InventoryHolder;
import static com.Acrobot.Breeze.Utils.ImplementationAdapter.getHolder;
/** /**
* A fix for a CraftBukkit bug. * A fix for a CraftBukkit bug.
* *
@ -18,7 +20,7 @@ public class PlayerTeleport implements Listener {
@EventHandler @EventHandler
public static void onPlayerTeleport(PlayerTeleportEvent event) { public static void onPlayerTeleport(PlayerTeleportEvent event) {
InventoryHolder holder = event.getPlayer().getOpenInventory().getTopInventory().getHolder(); InventoryHolder holder = getHolder(event.getPlayer().getOpenInventory().getTopInventory(), false);
if (!(holder instanceof BlockState)) { if (!(holder instanceof BlockState)) {
return; return;
} }

View File

@ -12,6 +12,7 @@ import org.bukkit.event.Event;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import static com.Acrobot.Breeze.Utils.ImplementationAdapter.getState;
import static com.Acrobot.Breeze.Utils.BlockUtil.isSign; import static com.Acrobot.Breeze.Utils.BlockUtil.isSign;
/** /**
@ -44,7 +45,7 @@ public class ChestShop implements Listener {
} }
if (isSign(block)) { if (isSign(block)) {
Sign sign = (Sign) block.getState(); Sign sign = (Sign) getState(block, false);
if (!ChestShopSign.isValid(sign)) { if (!ChestShopSign.isValid(sign)) {
return true; return true;

View File

@ -17,6 +17,8 @@ import org.bukkit.event.Event;
import java.util.UUID; import java.util.UUID;
import static com.Acrobot.Breeze.Utils.ImplementationAdapter.getState;
/** /**
* @author Acrobot * @author Acrobot
*/ */
@ -83,7 +85,7 @@ public class Security {
continue; continue;
} }
Sign sign = (Sign) block.getState(); Sign sign = (Sign) getState(block, false);
if (!ChestShopSign.isValid(sign) || !BlockUtil.getAttachedBlock(sign).equals(baseBlock)) { if (!ChestShopSign.isValid(sign) || !BlockUtil.getAttachedBlock(sign).equals(baseBlock)) {
continue; continue;

View File

@ -22,6 +22,8 @@ import org.bukkit.inventory.InventoryHolder;
import java.util.Locale; import java.util.Locale;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import static com.Acrobot.Breeze.Utils.ImplementationAdapter.getState;
/** /**
* @author Acrobot * @author Acrobot
*/ */
@ -66,7 +68,7 @@ public class ChestShopSign {
} }
public static boolean isValid(Block sign) { public static boolean isValid(Block sign) {
return BlockUtil.isSign(sign) && isValid((Sign) sign.getState()); return BlockUtil.isSign(sign) && isValid((Sign) getState(sign, false));
} }
/** /**
@ -78,7 +80,7 @@ public class ChestShopSign {
return false; return false;
} }
return uBlock.getConnectedSign((Chest) chest.getState()) != null; return uBlock.getConnectedSign(chest) != null;
} }
public static boolean isShopBlock(Block block) { public static boolean isShopBlock(Block block) {

View File

@ -15,6 +15,7 @@ import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.SignChangeEvent; import org.bukkit.event.block.SignChangeEvent;
import static com.Acrobot.Breeze.Utils.ImplementationAdapter.getState;
import static com.Acrobot.ChestShop.Events.PreTransactionEvent.TransactionOutcome.SHOP_IS_RESTRICTED; import static com.Acrobot.ChestShop.Events.PreTransactionEvent.TransactionOutcome.SHOP_IS_RESTRICTED;
import static com.Acrobot.ChestShop.Permission.ADMIN; import static com.Acrobot.ChestShop.Permission.ADMIN;
@ -56,7 +57,7 @@ public class RestrictedSign implements Listener {
return; return;
} }
Sign sign = (Sign) connectedSign.getState(); Sign sign = (Sign) getState(connectedSign, false);
if (!ChestShopSign.hasPermission(player, Permission.OTHER_NAME_DESTROY, sign)) { if (!ChestShopSign.hasPermission(player, Permission.OTHER_NAME_DESTROY, sign)) {
dropSignAndCancelEvent(event); dropSignAndCancelEvent(event);
@ -84,7 +85,7 @@ public class RestrictedSign implements Listener {
Block currentBlock = location.getBlock(); Block currentBlock = location.getBlock();
if (BlockUtil.isSign(currentBlock)) { if (BlockUtil.isSign(currentBlock)) {
Sign sign = (Sign) currentBlock.getState(); Sign sign = (Sign) getState(currentBlock, false);
if (isRestricted(sign)) { if (isRestricted(sign)) {
return sign; return sign;
@ -100,7 +101,7 @@ public class RestrictedSign implements Listener {
continue; continue;
} }
Sign sign = (Sign) relative.getState(); Sign sign = (Sign) getState(relative, false);
if (!BlockUtil.getAttachedBlock(sign).equals(currentBlock)) { if (!BlockUtil.getAttachedBlock(sign).equals(currentBlock)) {
continue; continue;
@ -116,7 +117,7 @@ public class RestrictedSign implements Listener {
public static boolean isRestrictedShop(Sign sign) { public static boolean isRestrictedShop(Sign sign) {
Block blockUp = sign.getBlock().getRelative(BlockFace.UP); Block blockUp = sign.getBlock().getRelative(BlockFace.UP);
return BlockUtil.isSign(blockUp) && isRestricted(((Sign) blockUp.getState()).getLines()); return BlockUtil.isSign(blockUp) && isRestricted(((Sign) getState(blockUp, false)).getLines());
} }
public static boolean isRestricted(String[] lines) { public static boolean isRestricted(String[] lines) {
@ -129,7 +130,7 @@ public class RestrictedSign implements Listener {
public static boolean canAccess(Sign sign, Player player) { public static boolean canAccess(Sign sign, Player player) {
Block blockUp = sign.getBlock().getRelative(BlockFace.UP); Block blockUp = sign.getBlock().getRelative(BlockFace.UP);
return !BlockUtil.isSign(blockUp) || hasPermission(player, ((Sign) blockUp.getState()).getLines()); return !BlockUtil.isSign(blockUp) || hasPermission(player, ((Sign) getState(blockUp, false)).getLines());
} }
public static boolean canDestroy(Player player, Sign sign) { public static boolean canDestroy(Player player, Sign sign) {
@ -139,7 +140,7 @@ public class RestrictedSign implements Listener {
public static Sign getAssociatedSign(Sign restricted) { public static Sign getAssociatedSign(Sign restricted) {
Block down = restricted.getBlock().getRelative(BlockFace.DOWN); Block down = restricted.getBlock().getRelative(BlockFace.DOWN);
return BlockUtil.isSign(down) ? (Sign) down.getState() : null; return BlockUtil.isSign(down) ? (Sign) getState(down, false) : null;
} }
public static boolean hasPermission(Player p, String[] lines) { public static boolean hasPermission(Player p, String[] lines) {

View File

@ -18,6 +18,8 @@ import org.bukkit.inventory.InventoryHolder;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static com.Acrobot.Breeze.Utils.ImplementationAdapter.getState;
/** /**
* @author Acrobot * @author Acrobot
*/ */
@ -223,7 +225,7 @@ public class uBlock {
continue; continue;
} }
Sign sign = (Sign) faceBlock.getState(); Sign sign = (Sign) getState(faceBlock, false);
if (ChestShopSign.isValid(sign)) { if (ChestShopSign.isValid(sign)) {
return sign; return sign;