Skip to content
Draft
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
6 changes: 6 additions & 0 deletions api-internal/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
<artifactId>gson</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>24.0.0</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
Expand Down
2 changes: 1 addition & 1 deletion api-internal/src/main/java/at/pavlov/internal/Hook.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ default boolean active() {
}

void onEnable();
void onDisable();
default void onDisable() {}
Class<? extends Hook<?>> getTypeClass();

default @NotNull String enabledMessage() {
Expand Down
20 changes: 18 additions & 2 deletions api-internal/src/main/java/at/pavlov/internal/HookManager.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package at.pavlov.internal;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

public class HookManager {
private final Map<Class<? extends Hook<?>>, Hook<?>> hooks = new HashMap<>();
Expand All @@ -30,7 +32,7 @@ public boolean isRegistered(@NotNull Class<? extends Hook<?>> type) {
return false;
}

public <T extends Hook<?>> @NotNull T getHook(@NotNull Class<T> type) {
public <T extends Hook<?>> @Nullable T getHook(@NotNull Class<T> type) {
Hook<?> hook = this.hooks.get(type);
if (hook != null) {
return type.cast(hook);
Expand All @@ -42,7 +44,21 @@ public boolean isRegistered(@NotNull Class<? extends Hook<?>> type) {
}
}

throw new IllegalArgumentException("No registered hook of type " + type.getName() + "!");
return null;
}

public <C, T extends Hook<C>> void processIfPresent(@NotNull Class<T> type, Consumer<C> consumer) {
T hook = getHook(type);
if (hook == null || !hook.active()) {
return;
}

C hookContent = hook.hook();
if (hookContent == null) {
return;
}

consumer.accept(hookContent);
}

/**
Expand Down
10 changes: 10 additions & 0 deletions cannons-bukkit/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>glaremasters repo</id>
<url>https://repo.glaremasters.me/repository/towny/</url>
</repository>
</repositories>

<dependencies>
Expand Down Expand Up @@ -149,6 +153,12 @@
<version>2.11.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.palmergames.bukkit.towny</groupId>
<artifactId>towny</artifactId>
<version>0.101.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
Expand Down
30 changes: 27 additions & 3 deletions cannons-bukkit/src/main/java/at/pavlov/cannons/Aiming.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
import at.pavlov.cannons.event.CannonLinkAimingEvent;
import at.pavlov.cannons.event.CannonTargetEvent;
import at.pavlov.cannons.event.CannonUseEvent;
import at.pavlov.cannons.hooks.towny.TownyHook;
import at.pavlov.cannons.projectile.Projectile;
import at.pavlov.cannons.scheduler.FakeBlockHandler;
import at.pavlov.cannons.utils.CannonsUtil;
import at.pavlov.cannons.utils.SoundUtils;
import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.utils.CombatUtil;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.Location;
Expand All @@ -37,11 +40,11 @@
import org.bukkit.util.Vector;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
Expand Down Expand Up @@ -651,7 +654,7 @@ private void findSuitableTarget(Cannon cannon, Map<UUID, Target> targets) {
return;
}

ArrayList<Target> possibleTargets = new ArrayList<>();
LinkedList<Target> possibleTargets = new LinkedList<>();

for (Target t : targets.values()) {
TargetType type = t.targetType();
Expand All @@ -667,13 +670,34 @@ private void findSuitableTarget(Cannon cannon, Map<UUID, Target> targets) {
if (cannon.isWhitelisted(t.uniqueId())) continue;
//Player
if (type == TargetType.PLAYER) {

final boolean[] skip = new boolean[1];
Cannons.getPlugin().getHookManager().processIfPresent(TownyHook.class, townyApi -> {
Resident ownerResident = townyApi.getResident(cannon.getOwner());
if (ownerResident == null) {
return;
}

Resident residentTarget = townyApi.getResident(t.uniqueId());
if (residentTarget == null) {
return;
}

if (CombatUtil.isAlly(residentTarget, ownerResident)) {
skip[0] = true;
}
});

if (skip[0]) continue;

// get solution
handlePossibleTarget(cannon, t, possibleTargets);
continue;
}

Cannon tCannon = CannonManager.getCannon(t.uniqueId());
if (tCannon == null) continue;
if (cannon.isWhitelisted(tCannon.getOwner())) continue;
//Cannons & Other have same handling
//check if the owner is whitelisted
if (type == TargetType.CANNON || type == TargetType.OTHER) {
Expand All @@ -697,7 +721,7 @@ private void findSuitableTarget(Cannon cannon, Map<UUID, Target> targets) {
}
}

private void handlePossibleTarget(Cannon cannon, Target t, ArrayList<Target> possibleTargets) {
private void handlePossibleTarget(Cannon cannon, Target t, List<Target> possibleTargets) {
if (canFindTargetSolution(cannon, t)) {
possibleTargets.add(t);
}
Expand Down
10 changes: 6 additions & 4 deletions cannons-bukkit/src/main/java/at/pavlov/cannons/Cannons.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import at.pavlov.cannons.hooks.VaultHook;
import at.pavlov.cannons.hooks.movecraft.type.MaxCannonsProperty;
import at.pavlov.cannons.hooks.movecraftcombat.MovecraftCombatHook;
import at.pavlov.cannons.hooks.towny.TownyHook;
import at.pavlov.cannons.listener.*;
import at.pavlov.cannons.projectile.Projectile;
import at.pavlov.cannons.projectile.ProjectileManager;
Expand Down Expand Up @@ -206,6 +207,10 @@ public void onEnable()
PlaceholderAPIHook placeholderAPIHook = new PlaceholderAPIHook(this);
hookManager.registerHook(placeholderAPIHook);

logDebug("Loading TownyHook");
TownyHook townyHook = new TownyHook(this);
hookManager.registerHook(townyHook);

logDebug("Time to enable hooks: " + new DecimalFormat("0.00").format(System.currentTimeMillis() - startTime) + "ms");

startTime = System.nanoTime();
Expand Down Expand Up @@ -306,10 +311,7 @@ public void onEnable()
" You don't need to add Movecraft-Cannons anymore as Movecraft support is now embedded," +
" we suggest you stop using it as in the future it might stop work properly.");

if (hookManager.isRegistered(MovecraftCombatHook.class)) {

}
movecraftHook.onDisable();
movecraftHook.onDisable();
}, 1L);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import at.pavlov.cannons.builders.ParticleBuilder;
import at.pavlov.cannons.cannon.CannonManager;
import at.pavlov.cannons.container.ItemHolder;
import at.pavlov.cannons.hooks.towny.TownyAllowCannon;
import at.pavlov.cannons.utils.ArmorCalculationUtil;
import at.pavlov.cannons.utils.CannonsUtil;
import at.pavlov.cannons.utils.FileUtils;
Expand Down Expand Up @@ -39,6 +40,9 @@
//movecraft
private boolean movecraftEnabled;
private boolean movecraftCannonEnabled;

private boolean townyEnabled;
private TownyAllowCannon townyAllowedPlayers;
//endregion

//build limits
Expand Down Expand Up @@ -133,6 +137,9 @@ public void loadConfig() {
setMovecraftEnabled(config.getBoolean("hooks.movecraft.enabled", true));
setMovecraftCannonEnabled(config.getBoolean("hooks.movecraftCombat.enabled", true));

setTownyEnabled(config.getBoolean("hooks.towny.enabled", true));
setTownyAllowedPlayers(TownyAllowCannon.fromConfig(config, "hooks.towny"));

setRelayExplosionEvent(config.getBoolean("general.relayExplosionEvent", false));
setClaimEdgeLength(config.getInt("general.claimEdgeLength", 60));
ArmorCalculationUtil.setMagicValue(config.getDouble("general.armorEffectiveness", 0.04));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,28 @@

import at.pavlov.cannons.Enum.InteractAction;
import at.pavlov.cannons.cannon.Cannon;
import lombok.Getter;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;

import java.util.UUID;

public class CannonUseEvent extends Event
{
@Getter
public class CannonUseEvent extends Event implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private final Cannon cannon;
private final UUID player;
private final InteractAction action;
private boolean cancelled;

public CannonUseEvent(Cannon cannon, UUID player, InteractAction action)
{
public CannonUseEvent(Cannon cannon, UUID player, InteractAction action) {
this.cannon = cannon;
this.player = player;
this.action = action;
this.cancelled = false;
}

public Cannon getCannon() {
return cannon;
}

public UUID getPlayer() {
return player;
}

public InteractAction getAction() {
return action;
}

public boolean isCancelled() {
return cancelled;
}

public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ public void onEnable() {
plugin.logInfo(ChatColor.GREEN + enabledMessage());
}

@Override
public void onDisable() { }

@Override
public Class<? extends Hook<?>> getTypeClass() {
return VaultHook.class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,6 @@ public void onEnable() {
plugin.logInfo(ChatColor.GREEN + enabledMessage());
}

@Override
public void onDisable() {
HandlerList.unregisterAll(new CraftDetectListener());
HandlerList.unregisterAll(new TranslationListener());
HandlerList.unregisterAll(new RotationListener());
}

@Override
public Class<? extends Hook<?>> getTypeClass() {
return MovecraftHook.class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ public void onEnable() {
plugin.logInfo(ChatColor.GREEN + enabledMessage());
}

@Override
public void onDisable() {
HandlerList.unregisterAll(new ProjectileImpactListener());
}

@Override
public Class<? extends Hook<?>> getTypeClass() {
return MovecraftCombatHook.class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ public void onEnable() {
plugin.logInfo(ChatColor.GREEN + enabledMessage());
}

@Override
public void onDisable() {

}

@Override
public boolean active() {
return working;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package at.pavlov.cannons.hooks.towny;

import org.bukkit.configuration.file.FileConfiguration;

import java.util.Locale;

public enum TownyAllowCannon {
ALL,
TOWN,
ALLIES;

public static TownyAllowCannon fromConfig(FileConfiguration config, String key) {
String result = config.getString(key, "TOWN");

try {
return TownyAllowCannon.valueOf(result.toUpperCase(Locale.ROOT));
} catch (Exception ignored) {
return TOWN;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package at.pavlov.cannons.hooks.towny;

import at.pavlov.cannons.Cannons;
import at.pavlov.cannons.hooks.BukkitHook;
import at.pavlov.internal.Hook;
import com.palmergames.bukkit.towny.TownyAPI;
import lombok.SneakyThrows;
import org.bukkit.plugin.PluginManager;

public class TownyHook extends BukkitHook<TownyAPI> {

public TownyHook(Cannons plugin) {
super(plugin);
}

@SneakyThrows
@Override
public void onEnable() {

if (!plugin.getMyConfig().isTownyEnabled()) {
return;
}

PluginManager pluginManager = plugin.getServer().getPluginManager();
if (!pluginManager.isPluginEnabled("Towny")) {
plugin.logDebug("Towny not found or disabled");
return;
}

hook = TownyAPI.getInstance();
pluginManager.registerEvents(new TownyListeners(), plugin);
}

@Override
public Class<? extends Hook<?>> getTypeClass() {
return TownyHook.class;
}
}
Loading