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+ }
0 commit comments