Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reworking protection system (test-event & move-location check) #545

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -333,13 +334,29 @@ private ArrayList<ItemFrame> 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) {
Block block = location.getBlock();

// TODO Deconstruct unused restriction-system checks in "protections/" folder.
// Only 'asedit.ignoreProtection.<plugin>' 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));*/

// 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. (The block is generally not placed via 'callEvent()' method.)
Bukkit.getServer().getPluginManager().callEvent(placeEvent);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might need to do something here for Folia users, since I dont think calling events directly like that works in their API... Something to check and investigate I think?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Since you also support Folia, I'll have to find out about this first. I am not familiar with it.

// An event-cancel afterward would have no effect.

return !placeEvent.isCancelled();
}

void applyLeftTool(Player player, ArmorStand as) {
Expand Down
Loading