Skip to content

Commit

Permalink
change ClaimAPI system
Browse files Browse the repository at this point in the history
  • Loading branch information
UsainSrht committed Jan 15, 2024
1 parent 5444d3b commit f26d06a
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 32 deletions.
12 changes: 8 additions & 4 deletions src/main/java/me/usainsrht/uhomes/UHomes.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import me.angeschossen.lands.api.LandsIntegration;
import me.lucko.commodore.Commodore;
import me.lucko.commodore.CommodoreProvider;
import me.ryanhamshire.GriefPrevention.GriefPrevention;
import me.usainsrht.uhomes.claim_interfaces.ClaimAPI;
import me.usainsrht.uhomes.claim_interfaces.GriefPrevention;
import me.usainsrht.uhomes.claim_interfaces.Lands;
import me.usainsrht.uhomes.command.CommandHandler;
import me.usainsrht.uhomes.command.HomeCommand;
import me.usainsrht.uhomes.command.SetHomeCommand;
Expand Down Expand Up @@ -39,9 +41,11 @@ public void onEnable() {

this.homeManager = new HomeManager(this);
this.teleportManager = new TeleportManager(this);
Object claimAPI = null;
if (getServer().getPluginManager().isPluginEnabled("Lands")) claimAPI = LandsIntegration.of(this);
else if (getServer().getPluginManager().isPluginEnabled("GriefPrevention")) claimAPI = GriefPrevention.instance;
ClaimAPI claimAPI = null;
if (getServer().getPluginManager().isPluginEnabled("Lands"))
claimAPI = new Lands(LandsIntegration.of(this));
else if (getServer().getPluginManager().isPluginEnabled("GriefPrevention"))
claimAPI = new GriefPrevention(me.ryanhamshire.GriefPrevention.GriefPrevention.instance);
this.claimManager = new ClaimManager(this, claimAPI);

loadConfig();
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/me/usainsrht/uhomes/claim_interfaces/ClaimAPI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package me.usainsrht.uhomes.claim_interfaces;

import org.bukkit.Location;
import org.bukkit.entity.Player;

public interface ClaimAPI {

boolean canEnter(Player player, Location location);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package me.usainsrht.uhomes.claim_interfaces;

import me.ryanhamshire.GriefPrevention.Claim;
import me.ryanhamshire.GriefPrevention.ClaimPermission;
import org.bukkit.Location;
import org.bukkit.entity.Player;

public class GriefPrevention implements ClaimAPI {

private me.ryanhamshire.GriefPrevention.GriefPrevention instance;

public GriefPrevention(me.ryanhamshire.GriefPrevention.GriefPrevention instance) {
this.instance = instance;
}

@Override
public boolean canEnter(Player player, Location location) {
Claim claim = instance.dataStore.getClaimAt(location, true, null);
if (claim == null) return true;
return claim.hasExplicitPermission(player, ClaimPermission.Manage);
}
}
22 changes: 22 additions & 0 deletions src/main/java/me/usainsrht/uhomes/claim_interfaces/Lands.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package me.usainsrht.uhomes.claim_interfaces;

import me.angeschossen.lands.api.LandsIntegration;
import me.angeschossen.lands.api.land.Area;
import org.bukkit.Location;
import org.bukkit.entity.Player;

public class Lands implements ClaimAPI {

private LandsIntegration landsIntegration;

public Lands(LandsIntegration landsIntegration) {
this.landsIntegration = landsIntegration;
}

@Override
public boolean canEnter(Player player, Location location) {
Area area = landsIntegration.getArea(location);
if (area == null) return true;
return (area.isTrusted(player.getUniqueId()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public static void setHome(Player player, Location location, @Nullable String na
return;
}
if (MainConfig.isSethomeClaimCheck()) {
if (!UHomes.getInstance().getClaimManager().canEnter(player, location)) {
if (!UHomes.getInstance().getClaimManager().getClaimAPI().canEnter(player, location)) {
MessageUtil.send(player, MainConfig.getMessage("not_allowed_to_sethome"));
SoundUtil.play(player, MainConfig.getSound("not_allowed_to_sethome"));
return;
Expand Down
33 changes: 8 additions & 25 deletions src/main/java/me/usainsrht/uhomes/manager/ClaimManager.java
Original file line number Diff line number Diff line change
@@ -1,45 +1,28 @@
package me.usainsrht.uhomes.manager;

import me.angeschossen.lands.api.LandsIntegration;
import me.angeschossen.lands.api.land.Area;
import me.ryanhamshire.GriefPrevention.Claim;
import me.ryanhamshire.GriefPrevention.ClaimPermission;
import me.ryanhamshire.GriefPrevention.GriefPrevention;
import me.usainsrht.uhomes.UHomes;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import me.usainsrht.uhomes.claim_interfaces.ClaimAPI;

public class ClaimManager {

private UHomes plugin;
private Object claimAPI;
private ClaimAPI claimAPI;

public ClaimManager(UHomes plugin, Object claimAPI) {
public ClaimManager(UHomes plugin, ClaimAPI claimAPI) {
this.plugin = plugin;
this.claimAPI = claimAPI;
}

public void setClaimAPI(ClaimAPI claimAPI) {
this.claimAPI = claimAPI;
}

public UHomes getPlugin() {
return plugin;
}

public Object getClaimAPI() {
public ClaimAPI getClaimAPI() {
return claimAPI;
}

public boolean canEnter(Player player, Location location) {
if (claimAPI == null) return true;
//todo stops code silently when class is undefined
else if (claimAPI instanceof LandsIntegration api) {
Area area = api.getArea(location);
if (area == null) return true;
return (area.isTrusted(player.getUniqueId()));
} else if (claimAPI instanceof GriefPrevention api) {
Claim claim = api.dataStore.getClaimAt(location, true, null);
if (claim == null) return true;
return claim.hasExplicitPermission(player, ClaimPermission.Manage);
}
return true;
}

}
4 changes: 2 additions & 2 deletions src/main/java/me/usainsrht/uhomes/manager/HomeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public void teleport(Entity entity, Home home) {
return;
}
if (MainConfig.isTeleportClaimCheck() && entity instanceof Player player) {
if (!plugin.getClaimManager().canEnter(player, home.getLocation())) {
if (!plugin.getClaimManager().getClaimAPI().canEnter(player, home.getLocation())) {
MessageUtil.send(entity, MainConfig.getMessage("not_allowed_to_teleport"));
SoundUtil.play(entity, MainConfig.getSound("not_allowed_to_teleport"));
return;
Expand Down Expand Up @@ -264,7 +264,7 @@ public void relocate(Player player, Home home) {
//todo confirmation
Location location = player.getLocation().clone();
if (MainConfig.isSethomeClaimCheck()) {
if (!UHomes.getInstance().getClaimManager().canEnter(player, location)) {
if (!UHomes.getInstance().getClaimManager().getClaimAPI().canEnter(player, location)) {
MessageUtil.send(player, MainConfig.getMessage("not_allowed_to_sethome"));
SoundUtil.play(player, MainConfig.getSound("not_allowed_to_sethome"));
return;
Expand Down

0 comments on commit f26d06a

Please sign in to comment.