Skip to content

Commit 115b828

Browse files
Full rework of the project, Improving restriction checks, Adding Folia support
1 parent 1e7af1e commit 115b828

File tree

11 files changed

+440
-295
lines changed

11 files changed

+440
-295
lines changed

pom.xml

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,19 @@
5353
</distributionManagement>
5454

5555
<repositories>
56+
<!-- SpigotMC (server software) -->
5657
<repository>
5758
<id>spigot-repo</id>
5859
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
5960
</repository>
61+
62+
<!-- Folia (server software) -->
63+
<repository>
64+
<id>papermc</id>
65+
<url>https://repo.papermc.io/repository/maven-public/</url>
66+
</repository>
67+
68+
<!-- WorldGuard (restriction system) -->
6069
<repository>
6170
<id>sk89q-repo</id>
6271
<url>https://maven.enginehub.org/repo/</url>
@@ -65,6 +74,7 @@
6574

6675
<dependencyManagement>
6776
<dependencies>
77+
<!-- PlotSquared (restriction system) -->
6878
<dependency>
6979
<groupId>com.intellectualsites.bom</groupId>
7080
<artifactId>bom-newest</artifactId>
@@ -76,24 +86,39 @@
7686
</dependencyManagement>
7787

7888
<dependencies>
79-
<dependency>
89+
<!-- SpigotMC (server software) -->
90+
<!-- <dependency>
8091
<groupId>org.spigotmc</groupId>
8192
<artifactId>spigot-api</artifactId>
8293
<version>1.21.4-R0.1-SNAPSHOT</version>
8394
<scope>provided</scope>
95+
</dependency>-->
96+
97+
<!-- Folia (server software) -->
98+
<dependency>
99+
<groupId>dev.folia</groupId>
100+
<artifactId>folia-api</artifactId>
101+
<version>1.21.4-R0.1-SNAPSHOT</version>
102+
<scope>provided</scope>
103+
<optional>true</optional>
84104
</dependency>
105+
106+
<!-- PlotSquared (restriction system) -->
85107
<dependency>
86108
<groupId>com.intellectualsites.plotsquared</groupId>
87109
<artifactId>plotsquared-core</artifactId>
88110
<scope>provided</scope>
89111
</dependency>
112+
113+
<!-- WorldGuard (restriction system) -->
90114
<dependency>
91115
<groupId>com.sk89q.worldguard</groupId>
92116
<artifactId>worldguard-bukkit</artifactId>
93117
<version>7.0.12</version>
94118
<scope>provided</scope>
95119
</dependency>
96120

121+
<!-- PlotSquared (restriction system) -->
97122
<dependency>
98123
<groupId>com.intellectualsites.plotsquared</groupId>
99124
<artifactId>plotsquared-bukkit</artifactId>
Lines changed: 30 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,53 @@
11
package de.redstoneworld.redrestrictionhelper;
22

3+
import de.redstoneworld.redrestrictionhelper.analyze.Analyzer;
4+
import de.redstoneworld.redrestrictionhelper.analyze.Result;
35
import de.redstoneworld.redrestrictionhelper.enums.ActionTypes;
46
import de.redstoneworld.redrestrictionhelper.enums.CheckMethods;
5-
import de.redstoneworld.redrestrictionhelper.enums.ResultReasons;
6-
import de.redstoneworld.redrestrictionhelper.internal.Analyzer;
77
import org.bukkit.Location;
88
import org.bukkit.entity.Player;
99
import org.bukkit.plugin.Plugin;
1010

11-
import java.util.List;
12-
1311
public class RestrictionCheck {
1412

15-
private static CheckMethods defaultCheckMethod = CheckMethods.EVENT_CALLING;
13+
private static final CheckMethods DEFAULT_CHECK_METHOD = CheckMethods.EVENT_CALLING;
14+
15+
private final Plugin bukkitPlugin;
1616

1717
private final ActionTypes actionType;
1818
private final Location location;
1919
private final Player targetPlayer;
2020
private final CheckMethods checkMethod;
21-
22-
private long timeOfCheck;
23-
private boolean result;
24-
private List<ResultReasons> resultReason;
25-
26-
27-
public RestrictionCheck(ActionTypes actionType, Location location, Player targetPlayer) {
21+
22+
private Result result;
23+
24+
25+
public RestrictionCheck(Plugin bukkitPlugin, ActionTypes actionType, Location location, Player targetPlayer) {
26+
this.bukkitPlugin = bukkitPlugin;
2827
this.actionType = actionType;
2928
this.location = location;
3029
this.targetPlayer = targetPlayer;
31-
this.checkMethod = defaultCheckMethod;
30+
this.checkMethod = DEFAULT_CHECK_METHOD;
31+
32+
runCheck();
3233
}
3334

34-
public RestrictionCheck(ActionTypes actionType, Location location, Player targetPlayer, CheckMethods checkMethod) {
35+
public RestrictionCheck(Plugin bukkitPlugin, ActionTypes actionType, Location location, Player targetPlayer, CheckMethods checkMethod) {
36+
this.bukkitPlugin = bukkitPlugin;
3537
this.actionType = actionType;
3638
this.location = location;
3739
this.targetPlayer = targetPlayer;
3840
this.checkMethod = checkMethod;
39-
}
40-
41-
public static void initial(Plugin bukkitPlugin) {
42-
Analyzer.setBukkitPlugin(bukkitPlugin);
4341

44-
if (!Analyzer.hasRestrictionPlugins()) {
45-
bukkitPlugin.getServer().getLogger().warning("No restriction plugin found.");
46-
}
47-
}
48-
49-
public static CheckMethods getDefaultCheckMethod() {
50-
return defaultCheckMethod;
42+
runCheck();
5143
}
52-
53-
public static void setDefaultCheckMethod(CheckMethods checkMethod) {
54-
RestrictionCheck.defaultCheckMethod = checkMethod;
44+
45+
private void runCheck() {
46+
47+
Analyzer analyzer = new Analyzer(bukkitPlugin);
48+
result = analyzer.executeAnalyse(this);
5549
}
56-
50+
5751
public ActionTypes getActionType() {
5852
return actionType;
5953
}
@@ -66,30 +60,16 @@ public Player getTargetPlayer() {
6660
return targetPlayer;
6761
}
6862

63+
public static CheckMethods getDefaultCheckMethod() {
64+
return DEFAULT_CHECK_METHOD;
65+
}
66+
6967
public CheckMethods getCheckMethod() {
7068
return checkMethod;
7169
}
72-
73-
public void setResult(boolean result, long timeOfCheck) {
74-
this.result = result;
75-
this.timeOfCheck = timeOfCheck;
76-
}
77-
78-
public void setResult(boolean result, long timeOfCheck, List<ResultReasons> resultReason) {
79-
this.result = result;
80-
this.timeOfCheck = timeOfCheck;
81-
this.resultReason = resultReason;
82-
}
83-
84-
public long getTimeOfCheck() {
85-
return timeOfCheck;
86-
}
8770

88-
public boolean getResult() {
71+
public Result getResult() {
8972
return result;
9073
}
91-
92-
public List<ResultReasons> getResultReason() {
93-
return resultReason;
94-
}
95-
}
74+
75+
}
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package de.redstoneworld.redrestrictionhelper.analyze;
2+
3+
import de.redstoneworld.redrestrictionhelper.RestrictionCheck;
4+
import de.redstoneworld.redrestrictionhelper.enums.CheckMethods;
5+
import de.redstoneworld.redrestrictionhelper.enums.RestrictionPlugins;
6+
import de.redstoneworld.redrestrictionhelper.enums.ResultReasons;
7+
import de.redstoneworld.redrestrictionhelper.analyze.restrictionplugins.PlotSquared_V7;
8+
import de.redstoneworld.redrestrictionhelper.analyze.restrictionplugins.WorldGuard_V7;
9+
import org.bukkit.Bukkit;
10+
import org.bukkit.Material;
11+
import org.bukkit.block.Block;
12+
import org.bukkit.block.BlockFace;
13+
import org.bukkit.entity.Player;
14+
import org.bukkit.event.Event;
15+
import org.bukkit.event.block.Action;
16+
import org.bukkit.event.block.BlockBreakEvent;
17+
import org.bukkit.event.block.BlockPlaceEvent;
18+
import org.bukkit.event.player.PlayerInteractEvent;
19+
import org.bukkit.inventory.ItemStack;
20+
import org.bukkit.plugin.Plugin;
21+
22+
import java.util.ArrayList;
23+
import java.util.List;
24+
import java.util.concurrent.atomic.AtomicBoolean;
25+
26+
public class Analyzer {
27+
28+
private final Plugin bukkitPlugin;
29+
private Result result;
30+
31+
32+
public Analyzer(Plugin bukkitPlugin) {
33+
this.bukkitPlugin = bukkitPlugin;
34+
}
35+
36+
public Result executeAnalyse(RestrictionCheck check) {
37+
38+
if (check.getCheckMethod() == CheckMethods.EVENT_CALLING) {
39+
analyseByTestEvent(check);
40+
41+
} else if (check.getCheckMethod() == CheckMethods.RESTRICTION_PLUGIN_API) {
42+
43+
if (!hasRestrictionPlugins()) {
44+
bukkitPlugin.getServer().getLogger().warning("No restriction plugin found.");
45+
return new Result(true);
46+
}
47+
48+
analyseByRestrictionPlugins(check);
49+
}
50+
51+
return result;
52+
}
53+
54+
private void analyseByTestEvent(RestrictionCheck check) {
55+
56+
Block block = check.getLocation().getBlock();
57+
Player player = check.getTargetPlayer();
58+
boolean passed = false;
59+
List<ResultReasons> reasons = new ArrayList<>();
60+
61+
62+
// Checking build permission by test-events. (The block is generally not placed via 'callEvent()' method.)
63+
64+
switch (check.getActionType()) {
65+
case INTERACT -> {
66+
PlayerInteractEvent testInteractEvent = new PlayerInteractEvent(player, Action.RIGHT_CLICK_BLOCK, new ItemStack(Material.AIR),
67+
block, BlockFace.UP);
68+
new TestEventExecuter(bukkitPlugin, check.getLocation(), testInteractEvent, false);
69+
70+
// special event-result here:
71+
passed = (testInteractEvent.useInteractedBlock() == Event.Result.ALLOW);
72+
if (passed) reasons.add(ResultReasons.RRH_EVENT_INTERACT);
73+
}
74+
case PLACE_AND_BREAK -> {
75+
// Used 'BlockPlaceEvent', because e.g. 'EntityPlaceEvent' is not handled the same for every restriction system.
76+
BlockPlaceEvent testPlaceEvent = new BlockPlaceEvent(block, block.getState(), block, new ItemStack(Material.AIR),
77+
player, false);
78+
new TestEventExecuter(bukkitPlugin, check.getLocation(), testPlaceEvent, false);
79+
80+
// Used 'BlockBreakEvent', because e.g. 'EntityBreak-Door-Event' is not handled the same for every restriction system.
81+
BlockBreakEvent testBreakEvent = new BlockBreakEvent(block, player);
82+
new TestEventExecuter(bukkitPlugin, check.getLocation(), testBreakEvent, false);
83+
84+
passed = ((!testPlaceEvent.isCancelled()) && (!testBreakEvent.isCancelled()));
85+
if (passed) reasons.add(ResultReasons.RRH_EVENT_PLACE);
86+
if (passed) reasons.add(ResultReasons.RRH_EVENT_BREAK);
87+
}
88+
case PLACE -> {
89+
// Used 'BlockPlaceEvent', because e.g. 'EntityPlaceEvent' is not handled the same for every restriction system.
90+
BlockPlaceEvent testPlaceEvent = new BlockPlaceEvent(block, block.getState(), block, new ItemStack(Material.AIR),
91+
player, false);
92+
new TestEventExecuter(bukkitPlugin, check.getLocation(), testPlaceEvent, false);
93+
94+
passed = !testPlaceEvent.isCancelled();
95+
if (passed) reasons.add(ResultReasons.RRH_EVENT_PLACE);
96+
}
97+
case BREAK -> {
98+
// Used 'BlockBreakEvent', because e.g. 'EntityBreak-Door-Event' is not handled the same for every restriction system.
99+
BlockBreakEvent testBreakEvent = new BlockBreakEvent(block, player);
100+
new TestEventExecuter(bukkitPlugin, check.getLocation(), testBreakEvent, false);
101+
102+
passed = !testBreakEvent.isCancelled();
103+
if (passed) reasons.add(ResultReasons.RRH_EVENT_BREAK);
104+
}
105+
}
106+
107+
result = new Result(passed, reasons);
108+
}
109+
110+
private void analyseByRestrictionPlugins(RestrictionCheck check) {
111+
112+
boolean passed = true;
113+
List<ResultReasons> reasons = new ArrayList<>();
114+
115+
if (bukkitPlugin.getServer().getPluginManager().isPluginEnabled(RestrictionPlugins.WORLD_GUARD.getName())) {
116+
WorldGuard_V7 wg = new WorldGuard_V7(bukkitPlugin);
117+
Result wgResult = wg.runCheck(check);
118+
119+
if (!wgResult.isAllowed()) passed = false;
120+
if (!wgResult.getResultReason().isEmpty()) reasons.addAll(wgResult.getResultReason());
121+
}
122+
123+
if (bukkitPlugin.getServer().getPluginManager().isPluginEnabled(RestrictionPlugins.PLOT_SQUARED.getName())) {
124+
PlotSquared_V7 ps = new PlotSquared_V7(bukkitPlugin);
125+
Result psResult = ps.runCheck(check);
126+
127+
if (!psResult.isAllowed()) passed = false;
128+
if (!psResult.getResultReason().isEmpty()) reasons.addAll(psResult.getResultReason());
129+
}
130+
131+
result = new Result(passed, reasons);
132+
}
133+
134+
public boolean hasRestrictionPlugins() {
135+
136+
AtomicBoolean foundPlugin = new AtomicBoolean(false);
137+
138+
RestrictionPlugins.RESTRICTION_PLUGIN_NAMES.forEach(plugin -> {
139+
140+
if (plugin.equalsIgnoreCase(RestrictionPlugins.RED_RESTRICTION_HELPER.getName())) return;
141+
142+
if (bukkitPlugin.getServer().getPluginManager().isPluginEnabled(plugin)) {
143+
foundPlugin.set(true);
144+
}
145+
});
146+
147+
return foundPlugin.get();
148+
}
149+
150+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package de.redstoneworld.redrestrictionhelper.analyze;
2+
3+
import de.redstoneworld.redrestrictionhelper.RestrictionCheck;
4+
import org.bukkit.plugin.Plugin;
5+
6+
public class RestrictionPluginCheck {
7+
8+
private final Plugin plugin;
9+
10+
11+
public RestrictionPluginCheck(Plugin plugin) {
12+
this.plugin = plugin;
13+
}
14+
15+
private Result runCheck(RestrictionCheck check) {
16+
return null;
17+
}
18+
19+
}

0 commit comments

Comments
 (0)