From 4228bbc02c76eb05b6bbfcb1c864b652de3fef0f Mon Sep 17 00:00:00 2001 From: Sn0wStorm Date: Tue, 3 Nov 2020 21:28:50 +0100 Subject: [PATCH] New general Barrel open perms checker + Towny --- pom.xml | 8 ++- resources/config/v12/de/config.yml | 5 ++ resources/config/v12/en/config.yml | 5 ++ resources/config/v12/fr/config.yml | 5 ++ resources/config/v12/it/config.yml | 5 ++ resources/config/v13/de/config.yml | 6 ++ resources/config/v13/en/config.yml | 5 ++ resources/config/v13/es/config.yml | 5 ++ resources/config/v13/fr/config.yml | 5 ++ resources/config/v13/it/config.yml | 5 ++ resources/config/v13/zh/config.yml | 5 ++ resources/plugin.yml | 2 +- src/com/dre/brewery/BDistiller.java | 9 ++- src/com/dre/brewery/Barrel.java | 2 +- .../api/events/barrel/BarrelAccessEvent.java | 14 ++++ src/com/dre/brewery/filedata/BConfig.java | 4 ++ .../integration/IntegrationListener.java | 69 +++++++++++++++++++ .../integration/barrel/TownyBarrel.java | 23 +++++++ 18 files changed, 176 insertions(+), 6 deletions(-) create mode 100644 src/com/dre/brewery/integration/barrel/TownyBarrel.java diff --git a/pom.xml b/pom.xml index e4835fd..ff0ad00 100644 --- a/pom.xml +++ b/pom.xml @@ -89,7 +89,7 @@ http://maven.sk89q.com/repo/ - + jitpack.io https://jitpack.io @@ -233,6 +233,12 @@ 3.11 provided + + com.github.TownyAdvanced + Towny + 0.96.3.0 + provided + org.bstats bstats-bukkit diff --git a/resources/config/v12/de/config.yml b/resources/config/v12/de/config.yml index 1817390..518461f 100644 --- a/resources/config/v12/de/config.yml +++ b/resources/config/v12/de/config.yml @@ -426,9 +426,14 @@ recipes: useWorldGuard: true useLWC: true useGriefPrevention: true +useTowny: true useGMInventories: true useCitadel: true +# Beim öffnen eine virtuelle Kiste nutzen um Rechte bei alle anderen Plugins abzufragen +# Könnte Anti-Cheat plugins verwirren aber sonst ok zu aktivieren +useVirtualChestPerms: false + # Änderungen an Fassinventaren mit LogBlock aufzeichen [true] useLogBlock: true diff --git a/resources/config/v12/en/config.yml b/resources/config/v12/en/config.yml index 9171cda..493e236 100644 --- a/resources/config/v12/en/config.yml +++ b/resources/config/v12/en/config.yml @@ -428,9 +428,14 @@ recipes: useWorldGuard: true useLWC: true useGriefPrevention: true +useTowny: true useGMInventories: true useCitadel: true +# Use a virtual chest when opening a Barrel to check with all other protection plugins +# This could confuse Anti-Cheat plugins, but is otherwise good to use +useVirtualChestPerms: false + # Enable the Logging of Barrel Inventories to LogBlock [true] useLogBlock: true diff --git a/resources/config/v12/fr/config.yml b/resources/config/v12/fr/config.yml index 1c12c29..a0a6000 100644 --- a/resources/config/v12/fr/config.yml +++ b/resources/config/v12/fr/config.yml @@ -423,9 +423,14 @@ recipes: useWorldGuard: true useLWC: true useGriefPrevention: true +useTowny: true useGMInventories: true useCitadel: true +# Use a virtual chest when opening a Barrel to check with all other protection plugins +# This could confuse Anti-Cheat plugins, but is otherwise good to use +useVirtualChestPerms: false + # Activer l'historique du contenu des tonneaux avec LogBlock [true] useLogBlock: true diff --git a/resources/config/v12/it/config.yml b/resources/config/v12/it/config.yml index 78496c4..7923227 100644 --- a/resources/config/v12/it/config.yml +++ b/resources/config/v12/it/config.yml @@ -415,9 +415,14 @@ recipes: useWorldGuard: true useLWC: true useGriefPrevention: true +useTowny: true useGMInventories: true useCitadel: true +# Use a virtual chest when opening a Barrel to check with all other protection plugins +# This could confuse Anti-Cheat plugins, but is otherwise good to use +useVirtualChestPerms: false + # Abilita il logging degli inventari dei barili [true] useLogBlock: true diff --git a/resources/config/v13/de/config.yml b/resources/config/v13/de/config.yml index 3c179e2..96f7d0c 100644 --- a/resources/config/v13/de/config.yml +++ b/resources/config/v13/de/config.yml @@ -786,8 +786,14 @@ recipes: useWorldGuard: true useLWC: true useGriefPrevention: true +useTowny: true useGMInventories: true +# Beim Fass öffnen eine virtuelle Kiste nutzen um Rechte bei alle anderen Plugins abzufragen +# Könnte Anti-Cheat plugins verwirren aber sonst ok zu aktivieren +useVirtualChestPerms: false + + # Änderungen an Fassinventaren mit LogBlock aufzeichen [true] useLogBlock: true diff --git a/resources/config/v13/en/config.yml b/resources/config/v13/en/config.yml index 79df4da..15c5bcd 100644 --- a/resources/config/v13/en/config.yml +++ b/resources/config/v13/en/config.yml @@ -780,8 +780,13 @@ recipes: useWorldGuard: true useLWC: true useGriefPrevention: true +useTowny: true useGMInventories: true +# Use a virtual chest when opening a Barrel to check with all other protection plugins +# This could confuse Anti-Cheat plugins, but is otherwise good to use +useVirtualChestPerms: false + # Enable the Logging of Barrel Inventories to LogBlock [true] useLogBlock: true diff --git a/resources/config/v13/es/config.yml b/resources/config/v13/es/config.yml index 9f5b0d8..fa8e30d 100644 --- a/resources/config/v13/es/config.yml +++ b/resources/config/v13/es/config.yml @@ -780,8 +780,13 @@ recipes: useWorldGuard: true useLWC: true useGriefPrevention: true +useTowny: true useGMInventories: true +# Use a virtual chest when opening a Barrel to check with all other protection plugins +# This could confuse Anti-Cheat plugins, but is otherwise good to use +useVirtualChestPerms: false + # Enable the Logging of Barrel Inventories to LogBlock [true] useLogBlock: true diff --git a/resources/config/v13/fr/config.yml b/resources/config/v13/fr/config.yml index 413972f..0e1fb9f 100644 --- a/resources/config/v13/fr/config.yml +++ b/resources/config/v13/fr/config.yml @@ -786,8 +786,13 @@ recipes: useWorldGuard: true useLWC: true useGriefPrevention: true +useTowny: true useGMInventories: true +# Use a virtual chest when opening a Barrel to check with all other protection plugins +# This could confuse Anti-Cheat plugins, but is otherwise good to use +useVirtualChestPerms: false + # Activer l'historique du contenu des tonneaux avec LogBlock [true] useLogBlock: true diff --git a/resources/config/v13/it/config.yml b/resources/config/v13/it/config.yml index 91666ec..c02f374 100644 --- a/resources/config/v13/it/config.yml +++ b/resources/config/v13/it/config.yml @@ -780,8 +780,13 @@ eggnog: useWorldGuard: true useLWC: true useGriefPrevention: true +useTowny: true useGMInventories: true +# Use a virtual chest when opening a Barrel to check with all other protection plugins +# This could confuse Anti-Cheat plugins, but is otherwise good to use +useVirtualChestPerms: false + # Abilita il logging degli inventari dei barili [true] useLogBlock: true diff --git a/resources/config/v13/zh/config.yml b/resources/config/v13/zh/config.yml index 9ebfdec..0626f22 100644 --- a/resources/config/v13/zh/config.yml +++ b/resources/config/v13/zh/config.yml @@ -784,8 +784,13 @@ recipes: useWorldGuard: true useLWC: true useGriefPrevention: true +useTowny: true useGMInventories: true +# Use a virtual chest when opening a Barrel to check with all other protection plugins +# This could confuse Anti-Cheat plugins, but is otherwise good to use +useVirtualChestPerms: false + # 对LogBlock启用方块数据录入[true] useLogBlock: true diff --git a/resources/plugin.yml b/resources/plugin.yml index 6c01b34..ab99610 100644 --- a/resources/plugin.yml +++ b/resources/plugin.yml @@ -1,7 +1,7 @@ name: Brewery version: 2.1.2 main: com.dre.brewery.P -softdepend: [LWC, LogBlock, WorldGuard, GriefPrevention, Vault, ChestShop] +softdepend: [LWC, LogBlock, WorldGuard, GriefPrevention, Vault, ChestShop, Towny] authors: [Milan Albrecht, Frank Baumann, ProgrammerDan, Daniel Saukel] api-version: 1.13 commands: diff --git a/src/com/dre/brewery/BDistiller.java b/src/com/dre/brewery/BDistiller.java index 6eb20b3..37ea858 100644 --- a/src/com/dre/brewery/BDistiller.java +++ b/src/com/dre/brewery/BDistiller.java @@ -176,7 +176,9 @@ public class BDistiller { if (now instanceof BrewingStand) { BrewingStand stand = (BrewingStand) now; if (brewTime == -1) { // only check at the beginning (and end) for distillables - prepareForDistillables(stand); + if (!prepareForDistillables(stand)) { + return; + } } brewTime--; // count down. @@ -204,7 +206,7 @@ public class BDistiller { } } - private void prepareForDistillables(BrewingStand stand) { + private boolean prepareForDistillables(BrewingStand stand) { BrewerInventory inventory = stand.getInventory(); if (contents == null) { contents = getDistillContents(inventory); @@ -234,13 +236,14 @@ public class BDistiller { trackedDistillers.remove(standBlock); showAlc(inventory, contents); P.p.debugLog("nothing to distill"); - return; + return false; default: runTime = getLongestDistillTime(contents); brewTime = runTime; P.p.debugLog("using brewtime: " + runTime); } + return true; } } } diff --git a/src/com/dre/brewery/Barrel.java b/src/com/dre/brewery/Barrel.java index 0cc0c01..0ba060b 100644 --- a/src/com/dre/brewery/Barrel.java +++ b/src/com/dre/brewery/Barrel.java @@ -124,7 +124,7 @@ public class Barrel implements InventoryHolder { } // Call event - BarrelAccessEvent accessEvent = new BarrelAccessEvent(this, player, event.getClickedBlock()); + BarrelAccessEvent accessEvent = new BarrelAccessEvent(this, player, event.getClickedBlock(), event.getBlockFace()); // Listened to by WGBarrel7, WGBarrelNew, WGBarrelOld, GriefPreventionBarrel (IntegrationListener) P.p.getServer().getPluginManager().callEvent(accessEvent); if (accessEvent.isCancelled()) { diff --git a/src/com/dre/brewery/api/events/barrel/BarrelAccessEvent.java b/src/com/dre/brewery/api/events/barrel/BarrelAccessEvent.java index b3c853d..2ac4c95 100644 --- a/src/com/dre/brewery/api/events/barrel/BarrelAccessEvent.java +++ b/src/com/dre/brewery/api/events/barrel/BarrelAccessEvent.java @@ -2,6 +2,7 @@ package com.dre.brewery.api.events.barrel; import com.dre.brewery.Barrel; import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; import org.bukkit.entity.Player; import org.bukkit.event.Cancellable; import org.bukkit.event.HandlerList; @@ -16,12 +17,18 @@ public class BarrelAccessEvent extends BarrelEvent implements Cancellable { private static final HandlerList handlers = new HandlerList(); private final Player player; private final Block clickedBlock; + private final BlockFace clickedBlockFace; private boolean isCancelled; public BarrelAccessEvent(Barrel barrel, Player player, Block clickedBlock) { + this(barrel, player, clickedBlock, BlockFace.UP); + } + + public BarrelAccessEvent(Barrel barrel, Player player, Block clickedBlock, BlockFace clickedBlockFace) { super(barrel); this.player = player; this.clickedBlock = clickedBlock; + this.clickedBlockFace = clickedBlockFace; } /** @@ -32,6 +39,13 @@ public class BarrelAccessEvent extends BarrelEvent implements Cancellable { return clickedBlock; } + /** + * Get the clicked Block Face when clicking on the Barrel Block + */ + public BlockFace getClickedBlockFace() { + return clickedBlockFace; + } + @Override public boolean isCancelled() { return isCancelled; diff --git a/src/com/dre/brewery/filedata/BConfig.java b/src/com/dre/brewery/filedata/BConfig.java index 022a6e4..2cb75e8 100644 --- a/src/com/dre/brewery/filedata/BConfig.java +++ b/src/com/dre/brewery/filedata/BConfig.java @@ -49,6 +49,7 @@ public class BConfig { public static boolean useLWC; //LWC public static boolean useLB; //LogBlock public static boolean useGP; //GriefPrevention + public static boolean useTowny; //Towny public static boolean hasVault; // Vault public static boolean useCitadel; // CivCraft/DevotedMC Citadel public static boolean useGMInventories; // GamemodeInventories @@ -59,6 +60,7 @@ public class BConfig { // Barrel public static boolean openEverywhere; public static boolean loadDataAsync; + public static boolean virtualChestPerms; // Cauldron public static boolean useOffhandForCauldron; @@ -212,10 +214,12 @@ public class BConfig { } } useLWC = config.getBoolean("useLWC", true) && plMan.isPluginEnabled("LWC"); + useTowny = config.getBoolean("useTowny", true) && plMan.isPluginEnabled("Towny"); useGP = config.getBoolean("useGriefPrevention", true) && plMan.isPluginEnabled("GriefPrevention"); useLB = config.getBoolean("useLogBlock", false) && plMan.isPluginEnabled("LogBlock"); useGMInventories = config.getBoolean("useGMInventories", false); useCitadel = config.getBoolean("useCitadel", false) && plMan.isPluginEnabled("Citadel"); + virtualChestPerms = config.getBoolean("useVirtualChestPerms", false); // The item util has been removed in Vault 1.7+ hasVault = plMan.isPluginEnabled("Vault") && Integer.parseInt(plMan.getPlugin("Vault").getDescription().getVersion().split("\\.")[1]) <= 6; diff --git a/src/com/dre/brewery/integration/IntegrationListener.java b/src/com/dre/brewery/integration/IntegrationListener.java index de7e320..83c2347 100644 --- a/src/com/dre/brewery/integration/IntegrationListener.java +++ b/src/com/dre/brewery/integration/IntegrationListener.java @@ -9,6 +9,7 @@ import com.dre.brewery.filedata.BConfig; import com.dre.brewery.integration.barrel.GriefPreventionBarrel; import com.dre.brewery.integration.barrel.LWCBarrel; import com.dre.brewery.integration.barrel.LogBlockBarrel; +import com.dre.brewery.integration.barrel.TownyBarrel; import com.dre.brewery.integration.item.MMOItemsPluginItem; import com.dre.brewery.recipe.BCauldronRecipe; import com.dre.brewery.recipe.RecipeItem; @@ -17,7 +18,9 @@ import net.mmogroup.mmolib.api.item.NBTItem; import org.bukkit.GameMode; import org.bukkit.Material; import org.bukkit.block.Block; +import org.bukkit.block.BlockState; import org.bukkit.entity.Player; +import org.bukkit.event.Event; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; @@ -106,6 +109,7 @@ public class IntegrationListener implements Listener { } else { P.p.msg(player, "&cError opening Barrel, please report to an Admin!"); } + return; } } } @@ -124,6 +128,7 @@ public class IntegrationListener implements Listener { if (!LWCBarrel.checkAccess(player, sign, plugin)) { P.p.msg(event.getPlayer(), P.p.languageReader.get("Error_NoBarrelAccess")); event.setCancelled(true); + return; } } catch (Throwable e) { event.setCancelled(true); @@ -137,11 +142,75 @@ public class IntegrationListener implements Listener { } else { P.p.msg(player, "&cError opening Barrel, please report to an Admin!"); } + return; } } } } } + + if (BConfig.useTowny) { + if (P.p.getServer().getPluginManager().isPluginEnabled("Towny")) { + try { + if (!TownyBarrel.checkAccess(event)) { + P.p.msg(event.getPlayer(), P.p.languageReader.get("Error_NoBarrelAccess")); + event.setCancelled(true); + return; + } + } catch (Throwable e) { + event.setCancelled(true); + P.p.errorLog("Failed to Check Towny for Barrel Open Permissions!"); + P.p.errorLog("Brewery was tested with Towny v0.96.3.0"); + P.p.errorLog("Disable the Towny support in the config and do /brew reload"); + e.printStackTrace(); + Player player = event.getPlayer(); + if (player.hasPermission("brewery.admin") || player.hasPermission("brewery.mod")) { + P.p.msg(player, "&cTowny check Error, Brewery was tested with up to v0.96.3.0 of Towny"); + P.p.msg(player, "&cSet &7useTowny: false &cin the config and /brew reload"); + } else { + P.p.msg(player, "&cError opening Barrel, please report to an Admin!"); + } + return; + } + } + } + + if (BConfig.virtualChestPerms) { + Player player = event.getPlayer(); + BlockState originalBlockState = event.getClickedBlock().getState(); + + event.getClickedBlock().setType(Material.CHEST, false); + PlayerInteractEvent simulatedEvent = new PlayerInteractEvent( + player, + Action.RIGHT_CLICK_BLOCK, + player.getInventory().getItemInMainHand(), + event.getClickedBlock(), + event.getClickedBlockFace(), + EquipmentSlot.HAND); + + try { + P.p.getServer().getPluginManager().callEvent(simulatedEvent); + } catch (Throwable e) { + P.p.errorLog("Failed to simulate a Chest for Barrel Open Permissions!"); + P.p.errorLog("Disable useVirtualChestPerms in the config and do /brew reload"); + e.printStackTrace(); + if (player.hasPermission("brewery.admin") || player.hasPermission("brewery.mod")) { + P.p.msg(player, "&cVirtual Chest Error"); + P.p.msg(player, "&cSet &7useVirtualChestPerms: false &cin the config and /brew reload"); + } else { + P.p.msg(player, "&cError opening Barrel, please report to an Admin!"); + } + } finally { + event.getClickedBlock().setType(Material.AIR, false); + originalBlockState.update(true); + } + + if (simulatedEvent.useInteractedBlock() == Event.Result.DENY) { + event.setCancelled(true); + P.p.msg(event.getPlayer(), P.p.languageReader.get("Error_NoBarrelAccess")); + //return; + } + } } @EventHandler(ignoreCancelled = true, priority = EventPriority.LOW) diff --git a/src/com/dre/brewery/integration/barrel/TownyBarrel.java b/src/com/dre/brewery/integration/barrel/TownyBarrel.java new file mode 100644 index 0000000..d5adb4e --- /dev/null +++ b/src/com/dre/brewery/integration/barrel/TownyBarrel.java @@ -0,0 +1,23 @@ +package com.dre.brewery.integration.barrel; + +import com.dre.brewery.P; +import com.dre.brewery.api.events.barrel.BarrelAccessEvent; + +import com.palmergames.bukkit.towny.TownySettings; +import com.palmergames.bukkit.towny.object.TownyPermission; +import com.palmergames.bukkit.towny.utils.PlayerCacheUtil; +import org.bukkit.Material; + +public class TownyBarrel { + public static boolean checkAccess(BarrelAccessEvent event) { + if (!TownySettings.isSwitchMaterial("BREWERY") && !TownySettings.isSwitchMaterial("BARREL")) { + if (P.use1_14) { + return true; + } else if (!TownySettings.isSwitchMaterial("CHEST")) { + return true; + } + } + Material mat = P.use1_14 ? Material.BARREL : Material.CHEST; + return PlayerCacheUtil.getCachePermission(event.getPlayer(), event.getSpigot().getLocation(), mat, TownyPermission.ActionType.SWITCH); + } +}