Add support for Paper's BlockDestroyEvent for sign breaks (Fixes #214)

Instead of using different modules we use a bukkit profile to test backwards compatibility with pure-Bukkit servers and use the paper-api in the default profile. This should really be done with modules in the future. The actual selection which listener to use is handled by checking if the event class exists on event registration.
This commit is contained in:
Phoenix616 2019-11-04 17:26:43 +01:00
parent 35dd13f917
commit 40467e3522
5 changed files with 102 additions and 16 deletions

64
pom.xml
View File

@ -17,10 +17,6 @@
</scm>
<repositories>
<repository>
<id>spigotmc-repo</id>
<url>https://hub.spigotmc.org/nexus/content/groups/public</url>
</repository>
<repository>
<id>sk89q-repo</id>
<url>http://maven.sk89q.com/repo/</url>
@ -74,13 +70,6 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>1.13-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mcstats.bukkit</groupId>
<artifactId>metrics</artifactId>
@ -392,6 +381,58 @@
</properties>
<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>paper-repo</id>
<url>https://papermc.io/repo/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.destroystokyo.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.13.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
</profile>
<profile>
<id>bukkit</id>
<repositories>
<repository>
<id>spigotmc-repo</id>
<url>https://hub.spigotmc.org/nexus/content/groups/public</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>1.13.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.name}-Bukkit</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/Paper*.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>static_build_number</id>
<activation>
@ -404,6 +445,7 @@
<buildDescription>(compiled at ${maven.build.timestamp})</buildDescription>
</properties>
</profile>
<profile>
<id>dynamic_build_number</id>
<activation>

View File

@ -76,6 +76,10 @@ public class AdminInventory implements Inventory {
return new HashMap<>();
}
public HashMap<Integer, ItemStack> removeItemAnySlot(ItemStack... items) throws IllegalArgumentException {
return new HashMap<>();
}
@Override
public ItemStack[] getContents() {
return content;

View File

@ -0,0 +1,16 @@
package com.Acrobot.ChestShop.Listeners.Block.Break.Attached;
import com.destroystokyo.paper.event.block.BlockDestroyEvent;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import static com.Acrobot.ChestShop.Listeners.Block.Break.SignBreak.handlePhysicsBreak;
public class PaperBlockDestroy implements Listener {
@EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
public static void onSign(BlockDestroyEvent event) {
handlePhysicsBreak(event.getBlock());
}
}

View File

@ -0,0 +1,16 @@
package com.Acrobot.ChestShop.Listeners.Block.Break.Attached;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPhysicsEvent;
import static com.Acrobot.ChestShop.Listeners.Block.Break.SignBreak.handlePhysicsBreak;
public class PhysicsBreak implements Listener {
@EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
public static void onSign(BlockPhysicsEvent event) {
handlePhysicsBreak(event.getBlock());
}
}

View File

@ -4,6 +4,7 @@ import com.Acrobot.Breeze.Utils.BlockUtil;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Events.ShopDestroyedEvent;
import com.Acrobot.ChestShop.Listeners.Block.Break.Attached.PhysicsBreak;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.Acrobot.ChestShop.UUIDs.NameManager;
import com.Acrobot.ChestShop.Utils.uBlock;
@ -36,12 +37,19 @@ import static com.Acrobot.ChestShop.Signs.ChestShopSign.NAME_LINE;
*/
public class SignBreak implements Listener {
private static final BlockFace[] SIGN_CONNECTION_FACES = {BlockFace.SOUTH, BlockFace.NORTH, BlockFace.EAST, BlockFace.WEST, BlockFace.UP};
private static final String METADATA_NAME = "shop_destroyer";
public static final String METADATA_NAME = "shop_destroyer";
@EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
public static void onSign(BlockPhysicsEvent event) {
Block block = event.getBlock();
public SignBreak() {
try {
Class.forName("com.destroystokyo.paper.event.block.BlockDestroyEvent");
ChestShop.getPlugin().registerEvent((Listener) Class.forName("com.Acrobot.ChestShop.Listeners.Block.Break.Attached.PaperBlockDestroy").newInstance());
ChestShop.getBukkitLogger().info("Using Paper's BlockDestroyEvent instead of the BlockPhysicsEvent!");
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
ChestShop.getPlugin().registerEvent(new PhysicsBreak());
}
}
public static void handlePhysicsBreak(Block block) {
if (!BlockUtil.isSign(block)) {
return;
}
@ -148,7 +156,7 @@ public class SignBreak implements Listener {
return player != null && NameManager.canUseName(player, OTHER_NAME_DESTROY, name);
}
private static void sendShopDestroyedEvent(Sign sign, Player player) {
public static void sendShopDestroyedEvent(Sign sign, Player player) {
Container connectedContainer = null;
if (!ChestShopSign.isAdminShop(sign)) {