Add support for LWC LimitsV2 module (#293)

This adds a new OTHER_BREAK PreShopCreationEvent outcome to indicate
that the sign should get broken. To keep it backwards compatible with
other plugins setCancelled still sets OTHER and will not lead to a sign
break. The breaking logic is also moved out of the ErrorMessageSender
as it didn't make sense to have there. (It also wasted a tiny bit of CPU
by getting the block of the state again)

Do do this it now fully depend on LWCX, it's the only active version anways.

Also fixed a possible out of index error with the sign lines.
This commit is contained in:
Phoenix616 2020-05-04 22:33:34 +01:00
parent bb4eece790
commit 3bf54529db
5 changed files with 41 additions and 42 deletions

23
pom.xml
View File

@ -25,6 +25,10 @@
<id>codemc-repo</id> <id>codemc-repo</id>
<url>https://repo.codemc.org/repository/maven-public/</url> <url>https://repo.codemc.org/repository/maven-public/</url>
</repository> </repository>
<repository>
<id>lwcx-repo</id>
<url>https://ci.ender.zone/plugin/repository/everything/</url>
</repository>
<repository> <repository>
<id>vault-repo</id> <id>vault-repo</id>
<url>http://nexus.hc.to/content/repositories/pub_releases/</url> <url>http://nexus.hc.to/content/repositories/pub_releases/</url>
@ -159,27 +163,10 @@
</exclusions> </exclusions>
</dependency> </dependency>
<dependency>
<groupId>com.griefcraft</groupId>
<artifactId>lwc</artifactId>
<version>4.7.0-SNAPSHOT</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
</exclusion>
<exclusion>
<groupId>org.bukkit</groupId>
<artifactId>craftbukkit</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency> <dependency>
<groupId>com.griefcraft.lwc</groupId> <groupId>com.griefcraft.lwc</groupId>
<artifactId>LWCX</artifactId> <artifactId>LWCX</artifactId>
<version>2.2.0</version> <version>2.2.5</version>
<scope>provided</scope> <scope>provided</scope>
<exclusions> <exclusions>
<exclusion> <exclusion>

View File

@ -203,6 +203,10 @@ public class PreShopCreationEvent extends Event implements Cancellable {
* For plugin use * For plugin use
*/ */
OTHER, OTHER,
/**
* Break the sign
*/
OTHER_BREAK,
SHOP_CREATED_SUCCESSFULLY SHOP_CREATED_SUCCESSFULLY
} }

View File

@ -3,7 +3,6 @@ package com.Acrobot.ChestShop.Listeners.Block;
import com.Acrobot.Breeze.Utils.BlockUtil; import com.Acrobot.Breeze.Utils.BlockUtil;
import com.Acrobot.Breeze.Utils.StringUtil; import com.Acrobot.Breeze.Utils.StringUtil;
import com.Acrobot.ChestShop.ChestShop; import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Events.AccountQueryEvent;
import com.Acrobot.ChestShop.Events.PreShopCreationEvent; import com.Acrobot.ChestShop.Events.PreShopCreationEvent;
import com.Acrobot.ChestShop.Events.ShopCreatedEvent; import com.Acrobot.ChestShop.Events.ShopCreatedEvent;
import com.Acrobot.ChestShop.Signs.ChestShopSign; import com.Acrobot.ChestShop.Signs.ChestShopSign;
@ -48,7 +47,12 @@ public class SignCreate implements Listener {
PreShopCreationEvent preEvent = new PreShopCreationEvent(event.getPlayer(), sign, lines); PreShopCreationEvent preEvent = new PreShopCreationEvent(event.getPlayer(), sign, lines);
ChestShop.callEvent(preEvent); ChestShop.callEvent(preEvent);
for (byte i = 0; i < event.getLines().length; ++i) { if (preEvent.isCancelled() && preEvent.getOutcome() != PreShopCreationEvent.CreationOutcome.OTHER) {
signBlock.breakNaturally();
return;
}
for (byte i = 0; i < preEvent.getSignLines().length && i < 3; ++i) {
event.setLine(i, preEvent.getSignLine(i)); event.setLine(i, preEvent.getSignLine(i));
} }

View File

@ -68,7 +68,6 @@ public class ErrorMessageSender implements Listener {
if (message != null) { if (message != null) {
event.getPlayer().sendMessage(Messages.prefix(message)); event.getPlayer().sendMessage(Messages.prefix(message));
event.getSign().getBlock().breakNaturally();
} }
} }
} }

View File

@ -13,10 +13,11 @@ import com.Acrobot.ChestShop.Utils.uBlock;
import com.griefcraft.lwc.LWC; import com.griefcraft.lwc.LWC;
import com.griefcraft.model.Protection; import com.griefcraft.model.Protection;
import com.griefcraft.modules.limits.LimitsModule; import com.griefcraft.modules.limits.LimitsModule;
import com.griefcraft.scripting.Module; import com.griefcraft.modules.limits.LimitsV2;
import com.griefcraft.scripting.event.LWCProtectionRegisterEvent; import com.griefcraft.scripting.event.LWCProtectionRegisterEvent;
import com.griefcraft.scripting.event.LWCProtectionRegistrationPostEvent; import com.griefcraft.scripting.event.LWCProtectionRegistrationPostEvent;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Container; import org.bukkit.block.Container;
import org.bukkit.block.Sign; import org.bukkit.block.Sign;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -24,16 +25,20 @@ 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.ChestShop.Events.PreShopCreationEvent.CreationOutcome.OTHER_BREAK;
/** /**
* @author Acrobot * @author Acrobot
*/ */
public class LightweightChestProtection implements Listener { public class LightweightChestProtection implements Listener {
private final LWC lwc; private final LWC lwc;
private final LimitsModule limitsModule; private final LimitsModule limitsModule;
private final LimitsV2 limitsV2;
public LightweightChestProtection() { public LightweightChestProtection() {
this.lwc = LWC.getInstance(); this.lwc = LWC.getInstance();
this.limitsModule = (LimitsModule) lwc.getModuleLoader().getModule(LimitsModule.class); this.limitsModule = (LimitsModule) lwc.getModuleLoader().getModule(LimitsModule.class);
this.limitsV2 = (LimitsV2) lwc.getModuleLoader().getModule(LimitsV2.class);
try { try {
if (Properties.PROTECT_SIGN_WITH_LWC) if (Properties.PROTECT_SIGN_WITH_LWC)
Protection.Type.valueOf(Properties.LWC_SIGN_PROTECTION_TYPE.name()); Protection.Type.valueOf(Properties.LWC_SIGN_PROTECTION_TYPE.name());
@ -48,24 +53,29 @@ public class LightweightChestProtection implements Listener {
public void onPreShopCreation(PreShopCreationEvent event) { public void onPreShopCreation(PreShopCreationEvent event) {
if (Properties.LWC_LIMITS_BLOCK_CREATION) { if (Properties.LWC_LIMITS_BLOCK_CREATION) {
if (Properties.PROTECT_SIGN_WITH_LWC) { if (Properties.PROTECT_SIGN_WITH_LWC) {
if (limitsModule.hasReachedLimit(event.getPlayer(), event.getSign().getBlock())) { if (isAtLimit(event.getPlayer(), event.getSign())) {
event.setCancelled(true); event.setOutcome(OTHER_BREAK);
event.getPlayer().sendMessage(Messages.prefix(Messages.NOT_ENOUGH_PROTECTIONS));
return; return;
} }
} }
if (Properties.PROTECT_CHEST_WITH_LWC) { if (Properties.PROTECT_CHEST_WITH_LWC) {
Container container = uBlock.findConnectedContainer(event.getSign()); Container container = uBlock.findConnectedContainer(event.getSign());
if (container != null && limitsModule.hasReachedLimit(event.getPlayer(), container.getBlock())) { if (container != null && isAtLimit(event.getPlayer(), container)) {
event.setCancelled(true); event.setOutcome(OTHER_BREAK);
event.getPlayer().sendMessage(Messages.prefix(Messages.NOT_ENOUGH_PROTECTIONS));
return; return;
} }
} }
} }
} }
private boolean isAtLimit(Player player, BlockState blockState) {
LWCProtectionRegisterEvent protectionEvent = new LWCProtectionRegisterEvent(player, blockState.getBlock());
limitsModule.onRegisterProtection(protectionEvent);
limitsV2.onRegisterProtection(protectionEvent);
return protectionEvent.isCancelled();
}
@EventHandler @EventHandler
public static void onShopCreation(ShopCreatedEvent event) { public static void onShopCreation(ShopCreatedEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
@ -164,21 +174,16 @@ public class LightweightChestProtection implements Listener {
Protection protection = null; Protection protection = null;
try { try {
protection = lwc.getPhysicalDatabase().registerProtection(block.getType(), type, worldName, event.getProtectionOwner().toString(), "", x, y, z); int blockId = com.griefcraft.cache.BlockCache.getInstance().getBlockId(block);
} catch (LinkageError e) { if (blockId < 0) {
try { return;
int blockId = com.griefcraft.cache.BlockCache.getInstance().getBlockId(block);
if (blockId < 0) {
return;
}
protection = lwc.getPhysicalDatabase().registerProtection(blockId, type, worldName, event.getProtectionOwner().toString(), "", x, y, z);
} catch (LinkageError e2) {
ChestShop.getBukkitLogger().warning(
"Incompatible LWC version installed! (" + lwc.getPlugin().getName() + " v" + lwc.getVersion() + ") \n" +
"Material method error: " + e.getMessage() + "\n" +
"Block cache/type id error: " + e2.getMessage()
);
} }
protection = lwc.getPhysicalDatabase().registerProtection(blockId, type, worldName, event.getProtectionOwner().toString(), "", x, y, z);
} catch (LinkageError e) {
ChestShop.getBukkitLogger().warning(
"Incompatible LWC version installed! (" + lwc.getPlugin().getName() + " v" + lwc.getVersion() + ") \n" +
"Block cache/type id error: " + e.getMessage()
);
} }
if (protection != null) { if (protection != null) {