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

Add Quarters protection module #273

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
18 changes: 15 additions & 3 deletions dough-protection/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@
<id>william278-repo</id>
<url>https://repo.william278.net/snapshots/</url>
</repository>
<repository>
<id>towny-repo</id>
<url>https://repo.glaremasters.me/repository/towny/</url>
</repository>
</repositories>

<dependencies>
Expand Down Expand Up @@ -157,9 +161,9 @@

<!-- Towny -->
<dependency>
<groupId>com.github.LlmDl</groupId>
<artifactId>Towny</artifactId>
<version>1b86d017c5</version>
<groupId>com.palmergames.bukkit.towny</groupId>
<artifactId>towny</artifactId>
<version>0.100.3.0</version>
<scope>provided</scope>
</dependency>

Expand Down Expand Up @@ -282,6 +286,14 @@
<version>1.0.580</version>
<scope>provided</scope>
</dependency>

<!-- Quarters -->
<dependency>
<groupId>com.github.jwkerr</groupId>
<artifactId>Quarters</artifactId>
<version>e9ed8a133a</version>
<scope>provided</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import io.github.bakedlibs.dough.protection.modules.LocketteProtectionModule;
import io.github.bakedlibs.dough.protection.modules.PlotSquaredProtectionModule;
import io.github.bakedlibs.dough.protection.modules.PreciousStonesProtectionModule;
import io.github.bakedlibs.dough.protection.modules.QuartersProtectionModule;
import io.github.bakedlibs.dough.protection.modules.RedProtectProtectionModule;
import io.github.bakedlibs.dough.protection.modules.ShopChestProtectionModule;
import io.github.bakedlibs.dough.protection.modules.TownyProtectionModule;
Expand Down Expand Up @@ -80,7 +81,6 @@ private void loadModuleImplementations(Plugin plugin) {

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

// If Quarters is installed, the QuartersProtectionModule must be enabled instead of the Towny module.
if (pm.isPluginEnabled("Quarters")) {
registerModule(pm, "Quarters", quarters -> new QuartersProtectionModule(quarters));
} else {
registerModule(pm, "Towny", towny -> new TownyProtectionModule(towny));
}

/*
* The following Plugins work by utilising one of the above listed
* Plugins in the background.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package io.github.bakedlibs.dough.protection.modules;

import au.lupine.quarters.api.manager.QuarterManager;
import au.lupine.quarters.object.entity.Quarter;
import com.palmergames.bukkit.towny.TownyAPI;
import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.TownBlock;
import com.palmergames.bukkit.towny.object.TownyPermission.ActionType;
import com.palmergames.bukkit.towny.utils.PlayerCacheUtil;
import io.github.bakedlibs.dough.protection.Interaction;
import io.github.bakedlibs.dough.protection.ProtectionModule;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

import javax.annotation.Nonnull;


/**
* Protection handling module for Quarters, a Towny add-on.
* If Quarters is installed on a server, this module must be registered
* instead of the Towny module.
*
* @author galacticwarrior9
*/
public class QuartersProtectionModule implements ProtectionModule {

private final Plugin plugin;

public QuartersProtectionModule(@Nonnull Plugin plugin) {
this.plugin = plugin;
}

@Override
public Plugin getPlugin() {
return plugin;
}

@Override
public void load() {
// We don't need to load any APIs, everything is static
}

@Override
public boolean hasPermission(OfflinePlayer p, Location l, Interaction action) {
if (!(p instanceof Player)) {
return false;
}
return isInteractionAllowed((Player) p, convert(action), l);
}

private boolean isInteractionAllowed(Player player, ActionType type, Location l) {
boolean allowedInUnderlyingPlot = PlayerCacheUtil.getCachePermission(player, l, l.getBlock().getType(), type);
if (allowedInUnderlyingPlot) {
return true;
}

Quarter quarter = QuarterManager.getInstance().getQuarter(l);
if (quarter == null) {
return false;
}

Resident resident = TownyAPI.getInstance().getResident(player.getUniqueId());
if (resident == null) {
return true;
}

return quarter.testPermission(convertToQuartersAction(type), resident);
}

/**
* Returns the corresponding Towny {@link ActionType} from the dough {@link Interaction}
*
* @param action The dough {@link Interaction}
* @return The corresponding Towny {@link ActionType}
*/
private ActionType convert(Interaction action) {
switch (action) {
case INTERACT_BLOCK:
return ActionType.SWITCH;
case INTERACT_ENTITY:
case ATTACK_PLAYER:
case ATTACK_ENTITY:
return ActionType.ITEM_USE;
case BREAK_BLOCK:
return ActionType.DESTROY;
case PLACE_BLOCK:
default:
return ActionType.BUILD;
}
}

/**
* Returns the corresponding Quarters {@link au.lupine.quarters.object.state.ActionType} from the Towny {@link ActionType}
*
* @param action The Towny {@link ActionType}
* @return The corresponding Quarters {@link au.lupine.quarters.object.state.ActionType}
*/
private au.lupine.quarters.object.state.ActionType convertToQuartersAction(ActionType action) {
switch (action) {
case DESTROY:
return au.lupine.quarters.object.state.ActionType.DESTROY;
case ITEM_USE:
return au.lupine.quarters.object.state.ActionType.ITEM_USE;
case SWITCH:
return au.lupine.quarters.object.state.ActionType.SWITCH;
default:
return au.lupine.quarters.object.state.ActionType.BUILD;
}
}
}