Skip to content

Commit f861c7e

Browse files
Add Quarters protection module
1 parent baf2d79 commit f861c7e

File tree

3 files changed

+130
-22
lines changed

3 files changed

+130
-22
lines changed

dough-protection/pom.xml

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@
6363
<id>william278-repo</id>
6464
<url>https://repo.william278.net/snapshots/</url>
6565
</repository>
66+
<repository>
67+
<id>towny-repo</id>
68+
<url>https://repo.glaremasters.me/repository/towny/</url>
69+
</repository>
6670
</repositories>
6771

6872
<dependencies>
@@ -157,9 +161,9 @@
157161

158162
<!-- Towny -->
159163
<dependency>
160-
<groupId>com.github.LlmDl</groupId>
161-
<artifactId>Towny</artifactId>
162-
<version>1b86d017c5</version>
164+
<groupId>com.palmergames.bukkit.towny</groupId>
165+
<artifactId>towny</artifactId>
166+
<version>0.100.3.0</version>
163167
<scope>provided</scope>
164168
</dependency>
165169

@@ -282,6 +286,14 @@
282286
<version>1.0.580</version>
283287
<scope>provided</scope>
284288
</dependency>
289+
290+
<!-- Quarters -->
291+
<dependency>
292+
<groupId>com.github.jwkerr</groupId>
293+
<artifactId>Quarters</artifactId>
294+
<version>e9ed8a133a</version>
295+
<scope>provided</scope>
296+
</dependency>
285297
</dependencies>
286298

287299
</project>

dough-protection/src/main/java/io/github/bakedlibs/dough/protection/ProtectionManager.java

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import javax.annotation.Nonnull;
1010
import javax.annotation.ParametersAreNonnullByDefault;
1111

12+
import io.github.bakedlibs.dough.protection.modules.*;
1213
import org.bukkit.Location;
1314
import org.bukkit.OfflinePlayer;
1415
import org.bukkit.block.Block;
@@ -18,24 +19,6 @@
1819
import io.github.bakedlibs.dough.common.DoughLogger;
1920
import io.github.bakedlibs.dough.protection.loggers.CoreProtectLogger;
2021
import io.github.bakedlibs.dough.protection.loggers.LogBlockLogger;
21-
import io.github.bakedlibs.dough.protection.modules.BentoBoxProtectionModule;
22-
import io.github.bakedlibs.dough.protection.modules.BlockLockerProtectionModule;
23-
import io.github.bakedlibs.dough.protection.modules.BoltProtectionModule;
24-
import io.github.bakedlibs.dough.protection.modules.ChestProtectProtectionModule;
25-
import io.github.bakedlibs.dough.protection.modules.FactionsUUIDProtectionModule;
26-
import io.github.bakedlibs.dough.protection.modules.FunnyGuildsProtectionModule;
27-
import io.github.bakedlibs.dough.protection.modules.GriefPreventionProtectionModule;
28-
import io.github.bakedlibs.dough.protection.modules.HuskTownsProtectionModule;
29-
import io.github.bakedlibs.dough.protection.modules.HuskClaimsProtectionModule;
30-
import io.github.bakedlibs.dough.protection.modules.LWCProtectionModule;
31-
import io.github.bakedlibs.dough.protection.modules.LandsProtectionModule;
32-
import io.github.bakedlibs.dough.protection.modules.LocketteProtectionModule;
33-
import io.github.bakedlibs.dough.protection.modules.PlotSquaredProtectionModule;
34-
import io.github.bakedlibs.dough.protection.modules.PreciousStonesProtectionModule;
35-
import io.github.bakedlibs.dough.protection.modules.RedProtectProtectionModule;
36-
import io.github.bakedlibs.dough.protection.modules.ShopChestProtectionModule;
37-
import io.github.bakedlibs.dough.protection.modules.TownyProtectionModule;
38-
import io.github.bakedlibs.dough.protection.modules.WorldGuardProtectionModule;
3922

4023
/**
4124
* This Class provides a nifty API for plugins to query popular protection plugins.
@@ -80,7 +63,6 @@ private void loadModuleImplementations(Plugin plugin) {
8063

8164
// We sadly cannot use ModuleName::new as this would load the class into memory prematurely
8265
registerModule(pm, "WorldGuard", worldGuard -> new WorldGuardProtectionModule(worldGuard));
83-
registerModule(pm, "Towny", towny -> new TownyProtectionModule(towny));
8466
registerModule(pm, "GriefPrevention", griefPrevention -> new GriefPreventionProtectionModule(griefPrevention));
8567
registerModule(pm, "LWC", lwc -> new LWCProtectionModule(lwc));
8668
registerModule(pm, "PreciousStones", preciousStones -> new PreciousStonesProtectionModule(preciousStones));
@@ -98,6 +80,13 @@ private void loadModuleImplementations(Plugin plugin) {
9880
registerModule(pm, "HuskClaims", huskClaims -> new HuskClaimsProtectionModule(huskClaims));
9981
registerModule(pm, "Bolt", bolt -> new BoltProtectionModule(bolt));
10082

83+
// If Quarters is installed, the QuartersProtectionModule must be enabled instead of the Towny module.
84+
if (pm.isPluginEnabled("Quarters")) {
85+
registerModule(pm, "Quarters", quarters -> new QuartersProtectionModule(quarters));
86+
} else {
87+
registerModule(pm, "Towny", towny -> new TownyProtectionModule(towny));
88+
}
89+
10190
/*
10291
* The following Plugins work by utilising one of the above listed
10392
* Plugins in the background.
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package io.github.bakedlibs.dough.protection.modules;
2+
3+
import au.lupine.quarters.api.manager.QuarterManager;
4+
import au.lupine.quarters.object.entity.Quarter;
5+
import com.palmergames.bukkit.towny.TownyAPI;
6+
import com.palmergames.bukkit.towny.object.Resident;
7+
import com.palmergames.bukkit.towny.object.TownyPermission.ActionType;
8+
import com.palmergames.bukkit.towny.utils.PlayerCacheUtil;
9+
import io.github.bakedlibs.dough.protection.Interaction;
10+
import io.github.bakedlibs.dough.protection.ProtectionModule;
11+
import org.bukkit.Location;
12+
import org.bukkit.OfflinePlayer;
13+
import org.bukkit.entity.Player;
14+
import org.bukkit.plugin.Plugin;
15+
16+
import javax.annotation.Nonnull;
17+
18+
19+
/**
20+
* Protection handling module for Quarters, a Towny add-on.
21+
* If Quarters is installed on a server, this module must be registered
22+
* instead of the Towny module.
23+
*
24+
* @author galacticwarrior9
25+
*/
26+
public class QuartersProtectionModule implements ProtectionModule {
27+
28+
private final Plugin plugin;
29+
30+
public QuartersProtectionModule(@Nonnull Plugin plugin) {
31+
this.plugin = plugin;
32+
}
33+
34+
@Override
35+
public Plugin getPlugin() {
36+
return plugin;
37+
}
38+
39+
@Override
40+
public void load() {
41+
// We don't need to load any APIs, everything is static
42+
}
43+
44+
@Override
45+
public boolean hasPermission(OfflinePlayer p, Location l, Interaction action) {
46+
if (!(p instanceof Player)) {
47+
return false;
48+
}
49+
return isInteractionAllowed((Player) p, convert(action), l);
50+
}
51+
52+
private boolean isInteractionAllowed(Player player, ActionType type, Location l) {
53+
boolean allowedInUnderlyingPlot = PlayerCacheUtil.getCachePermission(player, l, l.getBlock().getType(), type);
54+
Quarter quarter = QuarterManager.getInstance().getQuarter(l);
55+
if (quarter == null) {
56+
return allowedInUnderlyingPlot;
57+
}
58+
59+
Resident resident = TownyAPI.getInstance().getResident(player.getUniqueId());
60+
if (resident == null) {
61+
return true;
62+
}
63+
64+
return quarter.testPermission(convertToQuartersAction(type), resident);
65+
}
66+
67+
/**
68+
* Returns the corresponding Towny {@link ActionType} from the dough {@link Interaction}
69+
*
70+
* @param action The dough {@link Interaction}
71+
* @return The corresponding Towny {@link ActionType}
72+
*/
73+
private ActionType convert(Interaction action) {
74+
switch (action) {
75+
case INTERACT_BLOCK:
76+
return ActionType.SWITCH;
77+
case INTERACT_ENTITY:
78+
case ATTACK_PLAYER:
79+
case ATTACK_ENTITY:
80+
return ActionType.ITEM_USE;
81+
case BREAK_BLOCK:
82+
return ActionType.DESTROY;
83+
case PLACE_BLOCK:
84+
default:
85+
return ActionType.BUILD;
86+
}
87+
}
88+
89+
/**
90+
* Returns the corresponding Quarters {@link au.lupine.quarters.object.state.ActionType} from the Towny {@link ActionType}
91+
*
92+
* @param action The Towny {@link ActionType}
93+
* @return The corresponding Quarters {@link au.lupine.quarters.object.state.ActionType}
94+
*/
95+
private au.lupine.quarters.object.state.ActionType convertToQuartersAction(ActionType action) {
96+
switch (action) {
97+
case DESTROY:
98+
return au.lupine.quarters.object.state.ActionType.DESTROY;
99+
case ITEM_USE:
100+
return au.lupine.quarters.object.state.ActionType.ITEM_USE;
101+
case SWITCH:
102+
return au.lupine.quarters.object.state.ActionType.SWITCH;
103+
default:
104+
return au.lupine.quarters.object.state.ActionType.BUILD;
105+
}
106+
}
107+
}

0 commit comments

Comments
 (0)