From b03e452053a413d85614cdae0213c752cc50cb15 Mon Sep 17 00:00:00 2001 From: RedstoneFuture Date: Mon, 1 Jul 2024 06:28:37 +0200 Subject: [PATCH 1/4] Full reworking of can-build check by using of test-event --- .../armorstandeditor/PlayerEditorManager.java | 37 ++++++++++++++----- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/src/main/java/io/github/rypofalem/armorstandeditor/PlayerEditorManager.java b/src/main/java/io/github/rypofalem/armorstandeditor/PlayerEditorManager.java index 79778a9a..25cf16ee 100644 --- a/src/main/java/io/github/rypofalem/armorstandeditor/PlayerEditorManager.java +++ b/src/main/java/io/github/rypofalem/armorstandeditor/PlayerEditorManager.java @@ -20,18 +20,19 @@ package io.github.rypofalem.armorstandeditor; import com.google.common.collect.ImmutableList; - import io.github.rypofalem.armorstandeditor.api.ArmorStandRenameEvent; import io.github.rypofalem.armorstandeditor.api.ItemFrameGlowEvent; import io.github.rypofalem.armorstandeditor.menu.ASEHolder; import io.github.rypofalem.armorstandeditor.protections.*; - import org.bukkit.*; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.entity.*; -import org.bukkit.event.*; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; import org.bukkit.event.block.Action; +import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryCloseEvent; @@ -333,13 +334,29 @@ private ArrayList getFrameTargets(Player player) { return itemFrames; } - - boolean canEdit(Player player, Entity entity) { - //Get the Entity being checked for editing - Block block = entity.getLocation().getBlock(); - - // Check if all protections allow this edit, if one fails, don't allow edit - return protections.stream().allMatch(protection -> protection.checkPermission(block, player)); + static boolean canEdit(Player player, Entity entity) { + return canEdit(player, entity.getLocation()); + } + + static boolean canEdit(Player player, Location location) { + // Get the Entity being checked for editing + Block block = location.getBlock(); + + // TODO Remove old protection system, including "protections/" folder and documentation update: + +/* // Check if all protections allow this edit, if one fails, don't allow edit + return protections.stream().allMatch(protection -> protection.checkPermission(block, player));*/ + + // Creating test place-event for the target location (works also for Fine Adjustment): + BlockPlaceEvent placeEvent = new BlockPlaceEvent(block, block.getState(), location.getBlock(), + player.getActiveItem(), player, false, player.getActiveItemHand()); + + // Checking build permission by test-event: + Bukkit.getServer().getPluginManager().callEvent(placeEvent); + boolean canBuild = !placeEvent.isCancelled(); + placeEvent.setCancelled(true); + + return canBuild; } void applyLeftTool(Player player, ArmorStand as) { From 00f53097e0f245f8c60e9a51c3c16e3ce4468a6f Mon Sep 17 00:00:00 2001 From: RedstoneFuture Date: Mon, 1 Jul 2024 06:30:35 +0200 Subject: [PATCH 2/4] Adding build-check for (reverse) move location --- .../github/rypofalem/armorstandeditor/PlayerEditor.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/java/io/github/rypofalem/armorstandeditor/PlayerEditor.java b/src/main/java/io/github/rypofalem/armorstandeditor/PlayerEditor.java index 4e6d464e..13e30f0b 100644 --- a/src/main/java/io/github/rypofalem/armorstandeditor/PlayerEditor.java +++ b/src/main/java/io/github/rypofalem/armorstandeditor/PlayerEditor.java @@ -282,6 +282,10 @@ private void move(ArmorStand armorStand) { loc.add(0, 0, movChange); break; } + + // Checking the building permission for the target location: + if (!PlayerEditorManager.canEdit(getPlayer(), loc)) return; + Scheduler.teleport(armorStand, loc); } @@ -299,6 +303,10 @@ private void reverseMove(ArmorStand armorStand) { loc.subtract(0, 0, movChange); break; } + + // Checking the building permission for the target location: + if (!PlayerEditorManager.canEdit(getPlayer(), loc)) return; + Scheduler.teleport(armorStand, loc); } From 647b74bf9e0c3a4a4676e665b86cee3cbcbcfd36 Mon Sep 17 00:00:00 2001 From: RedstoneFuture Date: Mon, 1 Jul 2024 07:45:31 +0200 Subject: [PATCH 3/4] Reworking notes --- .../rypofalem/armorstandeditor/PlayerEditorManager.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/github/rypofalem/armorstandeditor/PlayerEditorManager.java b/src/main/java/io/github/rypofalem/armorstandeditor/PlayerEditorManager.java index 25cf16ee..d2971b65 100644 --- a/src/main/java/io/github/rypofalem/armorstandeditor/PlayerEditorManager.java +++ b/src/main/java/io/github/rypofalem/armorstandeditor/PlayerEditorManager.java @@ -339,10 +339,10 @@ static boolean canEdit(Player player, Entity entity) { } static boolean canEdit(Player player, Location location) { - // Get the Entity being checked for editing Block block = location.getBlock(); - // TODO Remove old protection system, including "protections/" folder and documentation update: + // TODO Deconstruct unused restriction-system checks in "protections/" folder. + // Only 'asedit.ignoreProtection.' check is necessary now. /* // Check if all protections allow this edit, if one fails, don't allow edit return protections.stream().allMatch(protection -> protection.checkPermission(block, player));*/ From 78dbd51fed555304e232bb9c8afa58b4e1d56d48 Mon Sep 17 00:00:00 2001 From: RedstoneFuture Date: Tue, 2 Jul 2024 04:10:30 +0200 Subject: [PATCH 4/4] Reworking notes, Removing ignored event-cancel --- .../armorstandeditor/PlayerEditorManager.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/io/github/rypofalem/armorstandeditor/PlayerEditorManager.java b/src/main/java/io/github/rypofalem/armorstandeditor/PlayerEditorManager.java index d2971b65..80704381 100644 --- a/src/main/java/io/github/rypofalem/armorstandeditor/PlayerEditorManager.java +++ b/src/main/java/io/github/rypofalem/armorstandeditor/PlayerEditorManager.java @@ -347,16 +347,16 @@ static boolean canEdit(Player player, Location location) { /* // Check if all protections allow this edit, if one fails, don't allow edit return protections.stream().allMatch(protection -> protection.checkPermission(block, player));*/ - // Creating test place-event for the target location (works also for Fine Adjustment): + // Creating test place-event for the target location. (Works also for Fine Adjustment.) + // Used 'BlockPlaceEvent', because e.g. 'EntityPlaceEvent' is not handled the same for every restriction system. BlockPlaceEvent placeEvent = new BlockPlaceEvent(block, block.getState(), location.getBlock(), player.getActiveItem(), player, false, player.getActiveItemHand()); - // Checking build permission by test-event: + // Checking build permission by test-event. (The block is generally not placed via 'callEvent()' method.) Bukkit.getServer().getPluginManager().callEvent(placeEvent); - boolean canBuild = !placeEvent.isCancelled(); - placeEvent.setCancelled(true); + // An event-cancel afterward would have no effect. - return canBuild; + return !placeEvent.isCancelled(); } void applyLeftTool(Player player, ArmorStand as) {