From 577a8e54ec5397efed0363df48c54302cea32ef7 Mon Sep 17 00:00:00 2001 From: Xiao-MoMi <70987828+Xiao-MoMi@users.noreply.github.com> Date: Sun, 22 May 2022 13:04:02 +0800 Subject: [PATCH] =?UTF-8?q?res=20wg=20=E5=85=BC=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Integrations/ResidenceIntegrations.java | 38 +++++++++ .../Integrations/WorldGuardIntegrations.java | 39 +++++++++ .../customcrops/listener/RightClickBlock.java | 34 +++++--- .../listener/RightClickCustomBlock.java | 80 +++++++++++++++---- src/main/resources/config.yml | 6 ++ 5 files changed, 174 insertions(+), 23 deletions(-) create mode 100644 src/main/java/net/momirealms/customcrops/Integrations/ResidenceIntegrations.java create mode 100644 src/main/java/net/momirealms/customcrops/Integrations/WorldGuardIntegrations.java diff --git a/src/main/java/net/momirealms/customcrops/Integrations/ResidenceIntegrations.java b/src/main/java/net/momirealms/customcrops/Integrations/ResidenceIntegrations.java new file mode 100644 index 0000000..fcc6376 --- /dev/null +++ b/src/main/java/net/momirealms/customcrops/Integrations/ResidenceIntegrations.java @@ -0,0 +1,38 @@ +package net.momirealms.customcrops.Integrations; + +import com.bekvon.bukkit.residence.protection.ClaimedResidence; +import com.bekvon.bukkit.residence.protection.FlagPermissions; +import com.bekvon.bukkit.residence.protection.ResidencePermissions; +import dev.lone.itemsadder.api.Events.CustomBlockInteractEvent; +import org.bukkit.Location; + +public class ResidenceIntegrations { + public static boolean checkResBuild(Location location, CustomBlockInteractEvent event){ + FlagPermissions.addFlag("build"); + ClaimedResidence res = com.bekvon.bukkit.residence.Residence.getInstance().getResidenceManager().getByLoc(location); + if(res!=null){ + ResidencePermissions perms = res.getPermissions(); + String playerName = event.getPlayer().getName(); + boolean hasPermission = perms.playerHas(playerName, "build", true); + if(!hasPermission){ + event.setCancelled(true); + return true; + } + } + return false; + } + public static boolean checkResHarvest(Location location, CustomBlockInteractEvent event){ + FlagPermissions.addFlag("harvest"); + ClaimedResidence res = com.bekvon.bukkit.residence.Residence.getInstance().getResidenceManager().getByLoc(location); + if(res!=null){ + ResidencePermissions perms = res.getPermissions(); + String playerName = event.getPlayer().getName(); + boolean hasPermission = perms.playerHas(playerName, "harvest", true); + if(!hasPermission){ + event.setCancelled(true); + return true; + } + } + return false; + } +} diff --git a/src/main/java/net/momirealms/customcrops/Integrations/WorldGuardIntegrations.java b/src/main/java/net/momirealms/customcrops/Integrations/WorldGuardIntegrations.java new file mode 100644 index 0000000..0236cf7 --- /dev/null +++ b/src/main/java/net/momirealms/customcrops/Integrations/WorldGuardIntegrations.java @@ -0,0 +1,39 @@ +package net.momirealms.customcrops.Integrations; + +import com.sk89q.worldedit.bukkit.BukkitAdapter; +import com.sk89q.worldguard.LocalPlayer; +import com.sk89q.worldguard.WorldGuard; +import com.sk89q.worldguard.bukkit.WorldGuardPlugin; +import com.sk89q.worldguard.protection.flags.Flags; +import com.sk89q.worldguard.protection.regions.RegionContainer; +import com.sk89q.worldguard.protection.regions.RegionQuery; +import dev.lone.itemsadder.api.Events.CustomBlockInteractEvent; +import org.bukkit.Location; +import org.bukkit.entity.Player; + +public class WorldGuardIntegrations { + public static boolean checkWGBuild(Player player, Location loc, CustomBlockInteractEvent event){ + + LocalPlayer localPlayer = WorldGuardPlugin.inst().wrapPlayer(player); + RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer(); + RegionQuery query = container.createQuery(); + + if (!query.testState(BukkitAdapter.adapt(loc), localPlayer, Flags.BUILD)) { + event.setCancelled(true); + return true; + } + return false; + } + public static boolean checkWGHarvest(Player player, Location loc, CustomBlockInteractEvent event){ + + LocalPlayer localPlayer = WorldGuardPlugin.inst().wrapPlayer(player); + RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer(); + RegionQuery query = container.createQuery(); + + if (!query.testState(BukkitAdapter.adapt(loc), localPlayer, Flags.BLOCK_BREAK)) { + event.setCancelled(true); + return true; + } + return false; + } +} diff --git a/src/main/java/net/momirealms/customcrops/listener/RightClickBlock.java b/src/main/java/net/momirealms/customcrops/listener/RightClickBlock.java index b9052b4..bc55585 100644 --- a/src/main/java/net/momirealms/customcrops/listener/RightClickBlock.java +++ b/src/main/java/net/momirealms/customcrops/listener/RightClickBlock.java @@ -1,5 +1,9 @@ package net.momirealms.customcrops.listener; +import com.bekvon.bukkit.residence.Residence; +import com.bekvon.bukkit.residence.protection.ClaimedResidence; +import com.bekvon.bukkit.residence.protection.FlagPermissions; +import com.bekvon.bukkit.residence.protection.ResidencePermissions; import dev.lone.itemsadder.api.CustomStack; import net.momirealms.customcrops.CustomCrops; import net.momirealms.customcrops.DataManager.MaxSprinklersPerChunk; @@ -24,16 +28,16 @@ import java.util.List; public class RightClickBlock implements Listener { - FileConfiguration config = CustomCrops.instance.getConfig(); - @EventHandler public void rightClickBlock(PlayerInteractEvent event){ - if(event.getAction() != Action.RIGHT_CLICK_BLOCK || !event.hasItem()){ + FileConfiguration config = CustomCrops.instance.getConfig(); + if(!event.hasItem()){ return; } + if(event.getAction() != Action.RIGHT_CLICK_BLOCK && event.getAction() != Action.RIGHT_CLICK_AIR) return; Player player = event.getPlayer(); ItemStack itemStack = event.getItem(); - + //水壶加水 if(itemStack.getType() == Material.WOODEN_SWORD){ List lineOfSight = player.getLineOfSight(null, 3); boolean hasWater = false; @@ -47,19 +51,30 @@ public class RightClickBlock implements Listener { return; } } + if(event.getAction() != Action.RIGHT_CLICK_BLOCK) return; if(event.getBlockFace() != BlockFace.UP) return; + if(CustomStack.byItemStack(event.getItem()) == null) return; - if(CustomStack.byItemStack(event.getItem()) == null){ - return; + Location location = event.getClickedBlock().getLocation(); + if(config.getBoolean("config.integration.residence")){ + FlagPermissions.addFlag("build"); + ClaimedResidence res = Residence.getInstance().getResidenceManager().getByLoc(location); + if(res!=null){ + ResidencePermissions perms = res.getPermissions(); + String playerName = event.getPlayer().getName(); + boolean hasPermission = perms.playerHas(playerName, "build", true); + if(!hasPermission){ + event.setCancelled(true); + return; + } + } } - + //是否过高过低 if(event.getClickedBlock().getY() > config.getInt("config.height.max") || event.getClickedBlock().getY() < config.getInt("config.height.min")){ MessageManager.playerMessage(config.getString("messages.prefix") + config.getString("messages.not-a-good-place"),player); return; } - Location location = event.getClickedBlock().getLocation(); - if(CustomStack.byItemStack(event.getItem()).getNamespacedID().equalsIgnoreCase(config.getString("config.sprinkler-1-item"))){ if(MaxSprinklersPerChunk.maxSprinklersPerChunk(location)){ MessageManager.playerMessage(config.getString("messages.prefix")+config.getString("messages.reach-limit-sprinkler").replace("{Max}", config.getString("config.max-sprinklers")),player); @@ -83,6 +98,7 @@ public class RightClickBlock implements Listener { } } private void addWater(ItemStack itemStack, Player player){ + FileConfiguration config = CustomCrops.instance.getConfig(); if(CustomStack.byItemStack(itemStack)!= null){ CustomStack customStack = CustomStack.byItemStack(itemStack); if(customStack.getNamespacedID().equalsIgnoreCase(config.getString("config.watering-can-1")) || diff --git a/src/main/java/net/momirealms/customcrops/listener/RightClickCustomBlock.java b/src/main/java/net/momirealms/customcrops/listener/RightClickCustomBlock.java index f9f4200..a1673e9 100644 --- a/src/main/java/net/momirealms/customcrops/listener/RightClickCustomBlock.java +++ b/src/main/java/net/momirealms/customcrops/listener/RightClickCustomBlock.java @@ -6,6 +6,8 @@ import dev.lone.itemsadder.api.Events.CustomBlockInteractEvent; import net.momirealms.customcrops.CustomCrops; import net.momirealms.customcrops.DataManager.CropManager; import net.momirealms.customcrops.DataManager.MaxCropsPerChunk; +import net.momirealms.customcrops.Integrations.ResidenceIntegrations; +import net.momirealms.customcrops.Integrations.WorldGuardIntegrations; import net.momirealms.customcrops.MessageManager; import org.bukkit.*; import org.bukkit.block.Block; @@ -21,8 +23,6 @@ import java.util.Objects; public class RightClickCustomBlock implements Listener { - FileConfiguration config = CustomCrops.instance.getConfig(); - @EventHandler public void rightClickCustomCrop(CustomBlockInteractEvent event){ if (event.getAction() != Action.RIGHT_CLICK_BLOCK) { @@ -31,7 +31,19 @@ public class RightClickCustomBlock implements Listener { Block clickedBlock = event.getBlockClicked(); Location clickedBlockLocation = clickedBlock.getLocation(); CustomBlock clickedCustomBlock = CustomBlock.byAlreadyPlaced(clickedBlock); + FileConfiguration config = CustomCrops.instance.getConfig(); + Player player = event.getPlayer(); if (event.getItem() == null) { + if(config.getBoolean("config.integration.residence")){ + if(ResidenceIntegrations.checkResHarvest(clickedBlockLocation, event)){ + return; + } + } + if(config.getBoolean("config.integration.worldguard")){ + if(WorldGuardIntegrations.checkWGHarvest(player,clickedBlockLocation, event)){ + return; + } + } if (clickedCustomBlock.getNamespacedID().contains("stage")){ if(clickedCustomBlock.getNamespacedID().equalsIgnoreCase(config.getString("config.dead-crop"))) return; String namespace = clickedCustomBlock.getNamespacedID().split(":")[0]; @@ -47,16 +59,31 @@ public class RightClickCustomBlock implements Listener { } } } - return; } - Player player = event.getPlayer(); + //res兼容 + if(config.getBoolean("config.integration.residence")){ + if(ResidenceIntegrations.checkResBuild(clickedBlockLocation, event)){ + return; + } + } + + //wg兼容 + if(config.getBoolean("config.integration.worldguard")){ + if(WorldGuardIntegrations.checkWGBuild(player,clickedBlockLocation, event)){ + return; + } + } + World world = player.getWorld(); ItemStack mainHandItem = player.getInventory().getItemInMainHand(); - + //右键的作物下方是否为自定义方块 if (CustomBlock.byAlreadyPlaced(world.getBlockAt(clickedBlockLocation.clone().subtract(0,1,0))) != null && clickedBlock.getType() == Material.TRIPWIRE) { + //检测右键的方块下方是否为干燥的种植盆方块 if(CustomBlock.byAlreadyPlaced(world.getBlockAt(clickedBlockLocation.clone().subtract(0,1,0))).getNamespacedID().equalsIgnoreCase(config.getString("config.pot"))) { + //如果手中的是水桶,那么转干为湿 if (mainHandItem.getType() == Material.WATER_BUCKET) { + //扣除水桶 if(player.getGameMode() != GameMode.CREATIVE){ mainHandItem.setAmount(mainHandItem.getAmount() - 1); player.getInventory().addItem(new ItemStack(Material.BUCKET)); @@ -64,23 +91,29 @@ public class RightClickCustomBlock implements Listener { CustomBlock.remove(clickedBlockLocation.clone().subtract(0, 1, 0)); CustomBlock.place(config.getString("config.watered-pot"), clickedBlockLocation.clone().subtract(0, 1, 0)); }else if(mainHandItem.getType() == Material.WOODEN_SWORD){ - waterPot(mainHandItem,player,clickedBlockLocation.clone().subtract(0,1,0)); + waterPot(mainHandItem,player,clickedBlockLocation.clone().subtract(0,1,0),config); } } + //检测右键的方块下方是否为湿润的种植盆方块 else if(CustomBlock.byAlreadyPlaced(world.getBlockAt(clickedBlockLocation.clone().subtract(0,1,0))).getNamespacedID().equalsIgnoreCase(config.getString("config.watered-pot"))){ + //如果是骨粉 if (mainHandItem.getType() == Material.BONE_MEAL){ + //植物是否具有stage属性 if (clickedCustomBlock.getNamespacedID().contains("stage")){ String[] cropNameList = clickedCustomBlock.getNamespacedID().split("_"); int nextStage = Integer.parseInt(cropNameList[2]) + 1; + //植物是否存在下一个stage if (CustomBlock.getInstance(cropNameList[0] + "_" +cropNameList[1] +"_" + nextStage) != null){ if(player.getGameMode() != GameMode.CREATIVE){ mainHandItem.setAmount(mainHandItem.getAmount() - 1); } + //骨粉的成功率 if (Math.random() < config.getDouble("config.bone-meal-chance")){ CustomBlock.remove(clickedBlockLocation); CustomBlock.place(cropNameList[0] + "_" +cropNameList[1] +"_" + nextStage,clickedBlockLocation); Particle particleSuccess = Particle.valueOf(config.getString("config.particle.success")); world.spawnParticle(particleSuccess, clickedBlockLocation.clone().add(0.5, 0.1,0.5), 1 ,0,0,0,0); + //使用骨粉是否消耗水分 if(config.getBoolean("config.bone-meal-consume-water")){ CustomBlock.remove(clickedBlockLocation.clone().subtract(0,1,0)); CustomBlock.place(config.getString("config.pot"), clickedBlockLocation.clone().subtract(0,1,0)); @@ -94,8 +127,11 @@ public class RightClickCustomBlock implements Listener { } } } else if (CustomBlock.byAlreadyPlaced(world.getBlockAt(clickedBlockLocation)) != null && event.getBlockFace() == BlockFace.UP){ + //检测右键的方块是否为干燥的种植盆方块 if (CustomBlock.byAlreadyPlaced(world.getBlockAt(clickedBlockLocation)).getNamespacedID().equalsIgnoreCase(config.getString("config.pot"))){ + //如果手中的是水桶,那么转干为湿 if (mainHandItem.getType() == Material.WATER_BUCKET){ + //扣除水桶 if(player.getGameMode() != GameMode.CREATIVE){ mainHandItem.setAmount(mainHandItem.getAmount() - 1); player.getInventory().addItem(new ItemStack(Material.BUCKET)); @@ -103,25 +139,30 @@ public class RightClickCustomBlock implements Listener { CustomBlock.remove(clickedBlockLocation); CustomBlock.place(config.getString("config.watered-pot"),clickedBlockLocation); } else if (mainHandItem.getType() == Material.WOODEN_SWORD){ - waterPot(mainHandItem, player,clickedBlockLocation); + waterPot(mainHandItem, player,clickedBlockLocation, config); } else { - tryPlantSeed(clickedBlockLocation, mainHandItem, player); + tryPlantSeed(clickedBlockLocation, mainHandItem, player, config); } + //检测右键的方块是否为湿润的种植盆方块 }else if(CustomBlock.byAlreadyPlaced(world.getBlockAt(clickedBlockLocation)).getNamespacedID().equalsIgnoreCase(config.getString("config.watered-pot"))){ - tryPlantSeed(clickedBlockLocation, mainHandItem, player); + tryPlantSeed(clickedBlockLocation, mainHandItem, player, config); } } } - private void tryPlantSeed(Location clickedBlockLocation, ItemStack mainHandItem, Player player) { + //尝试种植植物 + private void tryPlantSeed(Location clickedBlockLocation, ItemStack mainHandItem, Player player, FileConfiguration config) { + if(CustomStack.byItemStack(mainHandItem) == null) return; if (CustomStack.byItemStack(mainHandItem).getNamespacedID().toLowerCase().endsWith("_seeds")){ String namespaced_id = CustomStack.byItemStack(mainHandItem).getNamespacedID().toLowerCase(); String[] crop = CustomStack.byItemStack(mainHandItem).getNamespacedID().toLowerCase().replace("_seeds","").split(":"); + //是否超高超低 if (clickedBlockLocation.getY() < config.getInt("config.height.min") || clickedBlockLocation.getY() > config.getInt("config.height.max")){ MessageManager.playerMessage(config.getString("messages.prefix") + config.getString("messages.not-a-good-place"),player); return; } + //是否启用了季节 Label_out: if(config.getBoolean("enable-season")){ if(config.getBoolean("config.enable-greenhouse")){ @@ -146,26 +187,31 @@ public class RightClickCustomBlock implements Listener { return; } } + //该种子是否存在于配置文件中 if(!config.contains("crops."+crop[1])){ MessageManager.playerMessage(config.getString("messages.prefix")+config.getString("messages.no-such-seed"),player); return; } + //是否到达区块上限 if(MaxCropsPerChunk.maxCropsPerChunk(clickedBlockLocation)){ MessageManager.playerMessage(config.getString("messages.prefix")+config.getString("messages.reach-limit-crop").replace("{Max}", config.getString("config.max-crops")),player); return; } + //添加到缓存中 if(config.getBoolean("enable-season")){ CropManager.putInstance(clickedBlockLocation.clone().add(0,1,0), config.getString("crops."+crop[1]+".season")); }else{ CropManager.putInstance(clickedBlockLocation.clone().add(0,1,0), "all"); } + //减少种子数量 if(player.getGameMode() != GameMode.CREATIVE){ mainHandItem.setAmount(mainHandItem.getAmount() -1); } + //放置自定义农作物 CustomBlock.place(namespaced_id.replace("_seeds","_stage_1"),clickedBlockLocation.clone().add(0,1,0)); } } - private void waterPot(ItemStack itemStack, Player player, Location location){ + private void waterPot(ItemStack itemStack, Player player, Location location, FileConfiguration config){ if(CustomStack.byItemStack(itemStack) == null) return; @@ -189,6 +235,12 @@ public class RightClickCustomBlock implements Listener { x = 4; z = 4; } else return; + /* + -45 < yaw < 45 z+ + -135 < yaw < -45 x+ + 45 < yaw < 135 x- + else z- + */ player.playSound(player,Sound.BLOCK_WATER_AMBIENT,1,1); float yaw = player.getLocation().getYaw(); if (yaw <= 45 && yaw >= -135) { @@ -200,7 +252,7 @@ public class RightClickCustomBlock implements Listener { for (int i = 0; i <= x; i++) { for (int j = 0; j <= z; j++) { Location tempLoc = location.clone().add(i, 0, j); - canWaterPot(tempLoc); + canWaterPot(tempLoc,config); } } } else { @@ -212,13 +264,13 @@ public class RightClickCustomBlock implements Listener { for (int i = 0; i <= x; i++) { for (int j = 0; j <= z; j++) { Location tempLoc = location.clone().subtract(i, 0, j); - canWaterPot(tempLoc); + canWaterPot(tempLoc,config); } } } }); } - private void canWaterPot(Location location){ + private void canWaterPot(Location location, FileConfiguration config){ if(CustomBlock.byAlreadyPlaced(location.getWorld().getBlockAt(location)) != null){ if(CustomBlock.byAlreadyPlaced(location.getWorld().getBlockAt(location)).getNamespacedID().equalsIgnoreCase(config.getString("config.pot"))){ Bukkit.getScheduler().callSyncMethod(CustomCrops.instance,()->{ diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 689f635..8f9802f 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -65,6 +65,12 @@ config: - world # 只在被加载的区块中生长和枯萎,对性能友好 only-grow-in-loaded-chunks: true + #与第三方插件兼容 + integration: + #收获权限为harvest 浇水种植权限为build + residence: false + #收获flag为BLOCK_BREAK 浇水种植flag为BUILD + worldguard: false messages: prefix: '[CustomCrops] '