Skip to content
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
12 changes: 9 additions & 3 deletions src/main/java/dev/naspo/tether/Tether.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

import dev.naspo.tether.commands.Commands;
import dev.naspo.tether.commands.TabCompleter;
import dev.naspo.tether.listeners.*;
import dev.naspo.tether.listeners.EntityDeathListener;
import dev.naspo.tether.listeners.EntityDismountListener;
import dev.naspo.tether.listeners.EntityUnleashListener;
import dev.naspo.tether.listeners.PlayerInteractAtEntityListener;
import dev.naspo.tether.listeners.PlayerInteractListener;
import dev.naspo.tether.listeners.PlayerLeashEntityListener;
import dev.naspo.tether.services.IntegrationManager;
import dev.naspo.tether.services.LeashMobService;
import dev.naspo.tether.services.LeashPlayerService;
Expand All @@ -23,6 +28,7 @@ public void onLoad() {

@Override
public void onEnable() {

this.saveDefaultConfig();
this.getConfig().options().copyDefaults(true);
this.saveConfig();
Expand All @@ -46,8 +52,8 @@ private void instantiateClasses() {

private void registerEvents() {
this.getServer().getPluginManager().registerEvents(new PlayerInteractAtEntityListener(this, leashMobService, leashPlayerService), this);
this.getServer().getPluginManager().registerEvents(new PlayerInteractListener(leashMobService), this);
this.getServer().getPluginManager().registerEvents(new PlayerLeashEntityListener(leashMobService), this);
this.getServer().getPluginManager().registerEvents(new PlayerInteractListener(leashMobService, integrationManager), this);
this.getServer().getPluginManager().registerEvents(new PlayerLeashEntityListener(), this);
this.getServer().getPluginManager().registerEvents(new EntityDeathListener(), this);
this.getServer().getPluginManager().registerEvents(new EntityDismountListener(this, leashPlayerService), this);
this.getServer().getPluginManager().registerEvents(new EntityUnleashListener(), this);
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/dev/naspo/tether/integrations/Integration.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package dev.naspo.tether.integrations;

import dev.naspo.tether.Tether;
import org.bukkit.entity.LivingEntity;
import org.bukkit.Location;
import org.bukkit.entity.Player;

public abstract class Integration {
protected final Tether tetherPlugin;
protected final String pluginName;
protected boolean enabled = false;

public Integration(Tether tetherPlugin, String pluginName) {
public Integration(final Tether tetherPlugin, final String pluginName) {
this.tetherPlugin = tetherPlugin;
this.pluginName = pluginName;
}
Expand Down Expand Up @@ -43,11 +43,11 @@ public boolean isEnabled() {
}

/**
* Checks if the clicked LivingEntity can be leashed based on this integration.
* Checks if a mob can be leashed at the given location based on this integration.
*
* @param clicked The clicked LivingEntity. (Includes Player).
* @param player The player trying to leash.
* @return true if the player is permitted to leash the clicked LivingEntity.
* @param location The location the mob is trying to be leashed at.
* @param player The player trying to leash.
* @return true if the player is permitted to leash a mob at the given location.
*/
public abstract boolean canLeash(LivingEntity clicked, Player player);
public abstract boolean canLeash(Location location, Player player);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class WorldGuardIntegration extends Integration {
private FlagRegistry flagRegistry;
private StateFlag leashFlag;

public WorldGuardIntegration(Tether tetherPlugin) {
public WorldGuardIntegration(final Tether tetherPlugin) {
super(tetherPlugin, "WorldGuard");
}

Expand All @@ -35,23 +35,23 @@ protected boolean onEnable() {
}

@Override
public boolean canLeash(LivingEntity clicked, Player player) {
public boolean canLeash(final Location location, final Player player) {
// WorldGuard uses their own custom Player, Location, and World objects, so I am converting them here.
LocalPlayer wgLocalPlayer = WorldGuardPlugin.inst().wrapPlayer(player);
com.sk89q.worldedit.util.Location wgClickedLocation = BukkitAdapter.adapt(clicked.getLocation());
com.sk89q.worldedit.world.World wgClickedWorld = BukkitAdapter.adapt(clicked.getWorld());
final LocalPlayer wgLocalPlayer = WorldGuardPlugin.inst().wrapPlayer(player);
final com.sk89q.worldedit.util.Location wgLocation = BukkitAdapter.adapt(location);
final com.sk89q.worldedit.world.World wgWorld = BukkitAdapter.adapt(location.getWorld());

// If they have WorldGuard region bypass permission, return true.
boolean canBypass = worldGuardAPI.getPlatform().getSessionManager().hasBypass(wgLocalPlayer, wgClickedWorld);
final boolean canBypass = worldGuardAPI.getPlatform().getSessionManager().hasBypass(wgLocalPlayer, wgWorld);
if (canBypass) {
return true;
}

// Region data can be accessed via the RegionContainer object.
RegionContainer regionContainer = worldGuardAPI.getPlatform().getRegionContainer();
final RegionContainer regionContainer = worldGuardAPI.getPlatform().getRegionContainer();

// Query the state of our custom leash StateFlag at the clicked entity's location for a player.
return regionContainer.createQuery().testState(wgClickedLocation, wgLocalPlayer, leashFlag);
// Query the state of our custom leash StateFlag at the location for a player.
return regionContainer.createQuery().testState(wgLocation, wgLocalPlayer, leashFlag);
}

/**
Expand All @@ -63,13 +63,13 @@ private boolean registerLeashFlag() {
final String LEASH_FLAG_STRING = "leash";

try {
StateFlag stateFlag = new StateFlag(LEASH_FLAG_STRING, true);
final StateFlag stateFlag = new StateFlag(LEASH_FLAG_STRING, true);
flagRegistry.register(stateFlag);
this.leashFlag = stateFlag;
return true;
} catch (FlagConflictException e) {
} catch (final FlagConflictException e) {
// If there is a flag conflict, log that as an error to the console.
Flag<?> flag = flagRegistry.get(LEASH_FLAG_STRING);
final Flag<?> flag = flagRegistry.get(LEASH_FLAG_STRING);
if (flag != null) {
tetherPlugin.getLogger().warning("Couldn't register the 'leash' WorldGuard flag! It looks like another " +
"plugin registered it. WorldGuard integration with Tether will not work.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
import com.griefdefender.api.claim.Claim;
import com.griefdefender.api.claim.TrustTypes;
import dev.naspo.tether.Tether;
import org.bukkit.entity.LivingEntity;
import org.bukkit.Location;
import org.bukkit.entity.Player;

public class GriefDefenderIntegration extends ToggleableIntegration {
private Core griefDefenderAPI;

public GriefDefenderIntegration(Tether tetherPlugin) {
public GriefDefenderIntegration(final Tether tetherPlugin) {
super(tetherPlugin, "GriefDefender", "griefdefender");
}

Expand All @@ -22,8 +22,8 @@ protected boolean onEnable() {
}

@Override
public boolean canLeash(LivingEntity clicked, Player player) {
Claim claim = griefDefenderAPI.getClaimAt(clicked.getLocation());
public boolean canLeash(final Location location, final Player player) {
final Claim claim = griefDefenderAPI.getClaimAt(location);

if (claim != null) {
if (claim.isWilderness()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,38 @@
import me.ryanhamshire.GriefPrevention.ClaimPermission;
import me.ryanhamshire.GriefPrevention.DataStore;
import me.ryanhamshire.GriefPrevention.GriefPrevention;
import org.bukkit.Location;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;

public class GriefPreventionIntegration extends ToggleableIntegration {
private DataStore dataStore;

public GriefPreventionIntegration(Tether tetherPlugin) {
public GriefPreventionIntegration(final Tether tetherPlugin) {
super(tetherPlugin, "GriefPrevention", "griefprevention");
}

@Override
protected boolean onEnable() {
dataStore = GriefPrevention.instance.dataStore;
return true;
}

@Override
public boolean canLeash(LivingEntity clicked, Player player) {
Claim claim = dataStore.getClaimAt(clicked.getLocation(), true, null);
public boolean canLeash(final Location location, final Player player) {
final Claim claim = getDataStore().getClaimAt(location, true, null);
// If there is a claim here, return true if the player has explicit permission or is ignoring claims.
if (claim != null) {
return claim.hasExplicitPermission(player.getUniqueId(), ClaimPermission.Access) ||
dataStore.getPlayerData(player.getUniqueId()).ignoreClaims;
getDataStore().getPlayerData(player.getUniqueId()).ignoreClaims;
}
// Otherwise always return true if there is no claim.
return true;
}

public DataStore getDataStore() {
if (dataStore == null) {
dataStore = GriefPrevention.instance.dataStore;
}
return dataStore;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import dev.naspo.tether.Tether;
import me.angeschossen.lands.api.land.Area;
import me.angeschossen.lands.api.land.Land;
import org.bukkit.Location;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;

public class LandsIntegration extends ToggleableIntegration {
private me.angeschossen.lands.api.LandsIntegration landsAPI;

public LandsIntegration(Tether tetherPlugin) {
public LandsIntegration(final Tether tetherPlugin) {
super(tetherPlugin, "Lands", "lands");
}

Expand All @@ -20,10 +21,10 @@ protected boolean onEnable() {
}

@Override
public boolean canLeash(LivingEntity clicked, Player player) {
Area area = landsAPI.getArea(clicked.getLocation());
public boolean canLeash(final Location location, final Player player) {
final Area area = landsAPI.getArea(location);
if (area != null) {
Land land = area.getLand();
final Land land = area.getLand();
return land.getTrustedPlayers().contains(player.getUniqueId());
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
import com.bekvon.bukkit.residence.protection.ClaimedResidence;
import com.bekvon.bukkit.residence.protection.ResidencePermissions;
import dev.naspo.tether.Tether;
import org.bukkit.Location;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;

public class ResidenceIntegration extends ToggleableIntegration {
private Residence residenceAPI;

public ResidenceIntegration(Tether tetherPlugin) {
public ResidenceIntegration(final Tether tetherPlugin) {
super(tetherPlugin, "Residence", "residence");
}

Expand All @@ -21,13 +22,14 @@ protected boolean onEnable() {
}

@Override
public boolean canLeash(LivingEntity clicked, Player player) {
ClaimedResidence residence = residenceAPI.getResidenceManager().getByLoc(clicked.getLocation());
public boolean canLeash(final Location location, final Player player) {
final ClaimedResidence residence = residenceAPI.getResidenceManager().getByLoc(location);

if (residence != null) {
ResidencePermissions perms = residence.getPermissions();
final ResidencePermissions perms = residence.getPermissions();
return perms.playerHas(player, "leash", true);
}
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import com.palmergames.bukkit.towny.TownyAPI;
import com.palmergames.bukkit.towny.object.Town;
import dev.naspo.tether.Tether;
import org.bukkit.Location;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;

public class TownyIntegration extends ToggleableIntegration {
private TownyAPI townyAPI;

public TownyIntegration(Tether tetherPlugin) {
public TownyIntegration(final Tether tetherPlugin) {
super(tetherPlugin, "Towny", "towny");
}

Expand All @@ -20,11 +21,11 @@ protected boolean onEnable() {
}

@Override
public boolean canLeash(LivingEntity clicked, Player player) {
Town town;
public boolean canLeash(final Location location, final Player player) {
final Town town;
try {
town = townyAPI.getTownBlock(clicked.getLocation()).getTown();
} catch (Exception e) {
town = townyAPI.getTownBlock(location).getTown();
} catch (final Exception e) {
return true;
}
return town.getTrustedResidents().contains(player.getUniqueId());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package dev.naspo.tether.listeners;

import dev.naspo.tether.exceptions.leashexception.LeashErrorType;
import dev.naspo.tether.exceptions.leashexception.LeashException;
import dev.naspo.tether.services.IntegrationManager;
import dev.naspo.tether.services.LeashMobService;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
Expand All @@ -9,19 +14,26 @@

public class PlayerInteractListener implements Listener {
private final LeashMobService leashMobService;
private final IntegrationManager integrationManager;

public PlayerInteractListener(LeashMobService leashMobService) {
public PlayerInteractListener(final LeashMobService leashMobService, final IntegrationManager integrationManager) {
this.leashMobService = leashMobService;
this.integrationManager = integrationManager;
}

@EventHandler
private void onPlayerInteract(PlayerInteractEvent event) {
private void onPlayerInteract(final PlayerInteractEvent event) {
// Ensuring it's a right-click on a fence with the main hand.
// Used for fence post functionality for mobs that are not leashable by default as fence post
// functionality won't work for them.
if (event.getAction() != Action.RIGHT_CLICK_BLOCK) return;
if (event.getHand() != EquipmentSlot.HAND) return;
if (event.getClickedBlock() == null) return;
if (!event.getClickedBlock().getType().name().toLowerCase().endsWith("fence")) return;
leashMobService.handleFenceLeashing(event.getPlayer(), event.getClickedBlock().getLocation());
final Location location = event.getClickedBlock().getLocation();
final Player player = event.getPlayer();
// Claim checks.
if (!integrationManager.canLeash(location, player)) return;
leashMobService.handleFenceLeashing(player, location);
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
package dev.naspo.tether.listeners;

import dev.naspo.tether.services.LeashMobService;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerLeashEntityEvent;

public class PlayerLeashEntityListener implements Listener {
private final LeashMobService leashMobService;

public PlayerLeashEntityListener(LeashMobService leashMobService) {
this.leashMobService = leashMobService;
}

// Tether takes complete control of leashing LivingEntities, therefore this event should always be cancelled
// if it's to do with a LivingEntity.
@EventHandler
private void onPlayerLeashEntity(PlayerLeashEntityEvent event) {
private void onPlayerLeashEntity(final PlayerLeashEntityEvent event) {
if (event.getEntity() instanceof LivingEntity) {
event.setCancelled(true);
}
Expand Down
Loading