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

GH-167 start of normalizing developer api #195

Merged
merged 7 commits into from
Oct 2, 2024
Merged
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
2 changes: 2 additions & 0 deletions buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ object Versions {
const val JUNIT_JUPITER_PARAMS = "5.10.3"
const val JUNIT_JUPITER_ENGINE = "5.10.3"

const val JETBRAINS_ANNOTATIONS = "24.1.0"

const val ETERNALCODE_COMMONS = "1.1.3"
// TODO: Multification.

Expand Down
38 changes: 1 addition & 37 deletions eternalcombat-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,5 @@ plugins {
dependencies {
// Spigot api
compileOnlyApi("org.spigotmc:spigot-api:${Versions.SPIGOT_API}")

// kyori
api("net.kyori:adventure-platform-bukkit:${Versions.ADVENTURE_PLATFORM_BUKKIT}")
api("net.kyori:adventure-text-minimessage:${Versions.ADVENTURE_TEXT_MINIMESSAGE}")

// litecommands
api("dev.rollczi:litecommands-bukkit:${Versions.LITE_COMMANDS}")

// Okaeri configs
api("eu.okaeri:okaeri-configs-yaml-bukkit:${Versions.OKAERI_CONFIGS_YAML_BUKKIT}")
api("eu.okaeri:okaeri-configs-serdes-commons:${Versions.OKAERI_CONFIGS_SERDES_COMMONS}")
api("eu.okaeri:okaeri-configs-serdes-bukkit:${Versions.OKAERI_CONFIGS_SERDES_BUKKIT}")

// Panda utilities
api("org.panda-lang:panda-utilities:${Versions.PANDA_UTILITIES}")

// GitCheck
api("com.eternalcode:gitcheck:${Versions.GIT_CHECK}")

// commons
api("commons-io:commons-io:${Versions.APACHE_COMMONS}")

// bstats
api("org.bstats:bstats-bukkit:${Versions.B_STATS_BUKKIT}")

// caffeine
api("com.github.ben-manes.caffeine:caffeine:${Versions.CAFFEINE}")

api("com.eternalcode:eternalcode-commons-bukkit:${Versions.ETERNALCODE_COMMONS}")
api("com.eternalcode:eternalcode-commons-adventure:${Versions.ETERNALCODE_COMMONS}")

// worldguard
compileOnly("com.sk89q.worldguard:worldguard-bukkit:${Versions.WORLD_GUARD_BUKKIT}")

// PlaceholderAPI
compileOnlyApi("me.clip:placeholderapi:${Versions.PLACEHOLDER_API}")

api("org.jetbrains:annotations:${Versions.JETBRAINS_ANNOTATIONS}")
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
package com.eternalcode.combat;

import com.eternalcode.combat.config.implementation.PluginConfig;
import com.eternalcode.combat.drop.DropKeepInventoryManager;
import com.eternalcode.combat.drop.DropManager;
import com.eternalcode.combat.fight.drop.DropKeepInventoryService;
import com.eternalcode.combat.fight.FightManager;
import com.eternalcode.combat.fight.drop.DropService;
import com.eternalcode.combat.fight.effect.FightEffectService;
import com.eternalcode.combat.fight.pearl.FightPearlManager;
import com.eternalcode.combat.fight.tagout.FightTagOutService;
import com.eternalcode.combat.fight.pearl.FightPearlService;
import com.eternalcode.combat.region.RegionProvider;
import com.eternalcode.combat.fight.tagout.FightTagOutService;

public interface EternalCombatApi {

FightManager getFightManager();

RegionProvider getRegionProvider();

FightPearlManager getFightPearlManager();
FightPearlService getFightPearlService();

FightTagOutService getFightTagOutService();

FightEffectService getFightEffectService();

DropManager getDropManager();

DropKeepInventoryManager getDropKeepInventoryManager();
DropService getDropService();

PluginConfig getPluginConfig();
DropKeepInventoryService getDropKeepInventoryService();

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,76 +4,26 @@
import com.eternalcode.combat.fight.event.CauseOfUnTag;
import com.eternalcode.combat.fight.event.FightTagEvent;
import com.eternalcode.combat.fight.event.FightUntagEvent;
import com.eternalcode.combat.event.EventCaller;

import java.time.Duration;
import java.time.Instant;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

public class FightManager {

private final Map<UUID, FightTag> fights = new ConcurrentHashMap<>();
private final EventCaller eventCaller;

public FightManager(EventCaller eventCaller) {
this.eventCaller = eventCaller;
}

public boolean isInCombat(UUID player) {
if (!this.fights.containsKey(player)) {
return false;
}

FightTag fightTag = this.fights.get(player);
public interface FightManager {

return !fightTag.isExpired();
}
boolean isInCombat(UUID player);

public FightUntagEvent untag(UUID player, CauseOfUnTag causeOfUnTag) {
FightUntagEvent event = this.eventCaller.publishEvent(new FightUntagEvent(player, causeOfUnTag));
if (event.isCancelled()) {
return event;
}
FightTag getTag(UUID target);

this.fights.remove(player);
return event;
}

public FightTagEvent tag(UUID target, Duration delay, CauseOfTag causeOfTag) {
return this.tag(target, delay, causeOfTag, null);
}
Collection<FightTag> getFights();

@ApiStatus.Experimental
public FightTagEvent tag(UUID target, Duration delay, CauseOfTag causeOfTag, @Nullable UUID tagger) {
FightTagEvent event = this.eventCaller.publishEvent(new FightTagEvent(target, causeOfTag));

if (event.isCancelled()) {
return event;
}
Instant now = Instant.now();
Instant endOfCombatLog = now.plus(delay);

FightTag fightTag = new FightTag(target, endOfCombatLog, tagger);

this.fights.put(target, fightTag);
return event;
}
FightTagEvent tag(UUID target, Duration delay, CauseOfTag causeOfTag, @Nullable UUID tagger);

public Collection<FightTag> getFights() {
return Collections.unmodifiableCollection(this.fights.values());
}
FightTagEvent tag(UUID target, Duration delay, CauseOfTag causeOfTag);

public FightTag getTag(UUID target) {
return this.fights.get(target);
}
FightUntagEvent untag(UUID player, CauseOfUnTag causeOfUnTag);

public void untagAll() {
this.fights.clear();
}
void untagAll();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,17 @@
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

public class FightTag {
public interface FightTag {

private final UUID taggedPlayer;
private final Instant endOfCombatLog;
private final @Nullable UUID tagger;

FightTag(UUID personToAddCombat, Instant endOfCombatLog, @Nullable UUID tagger) {
this.taggedPlayer = personToAddCombat;
this.endOfCombatLog = endOfCombatLog;
this.tagger = tagger;
}

public UUID getTaggedPlayer() {
return this.taggedPlayer;
}

public Instant getEndOfCombatLog() {
return this.endOfCombatLog;
}

public boolean isExpired() {
return Instant.now().isAfter(this.endOfCombatLog);
}

public Duration getRemainingDuration() {
Duration between = Duration.between(Instant.now(), this.endOfCombatLog);
@Nullable
@ApiStatus.Experimental
UUID getTagger();

if (between.isNegative()) {
return Duration.ZERO;
}
Duration getRemainingDuration();

return between;
}
boolean isExpired();

@Nullable
@ApiStatus.Experimental
public UUID getTagger() {
return this.tagger;
}
Instant getEndOfCombatLog();

UUID getTaggedPlayer();
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.eternalcode.combat.drop;
package com.eternalcode.combat.fight.drop;

import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.eternalcode.combat.fight.drop;

import java.util.List;
import java.util.UUID;
import org.bukkit.inventory.ItemStack;

public interface DropKeepInventoryService {

List<ItemStack> nextItems(UUID uuid);

boolean hasItems(UUID uuid);

void addItems(UUID uuid, List<ItemStack> item);

void addItem(UUID uuid, ItemStack item);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.eternalcode.combat.drop;
package com.eternalcode.combat.fight.drop;

public interface DropModifier {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.eternalcode.combat.drop;
package com.eternalcode.combat.fight.drop;

import org.bukkit.inventory.ItemStack;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.eternalcode.combat.fight.drop;

public interface DropService {

DropResult modify(DropType dropType, Drop drop);

void registerModifier(DropModifier dropModifier);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.eternalcode.combat.drop;
package com.eternalcode.combat.fight.drop;

public enum DropType {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,75 +1,25 @@
package com.eternalcode.combat.fight.effect;

import java.util.List;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.List;
import java.util.ArrayList;

public class FightEffectService {

private final Map<UUID, List<PotionEffect>> activeEffects = new HashMap<>();
private static final int INFINITE_DURATION = -1;

public void storeActiveEffect(Player player, PotionEffect effect) {
List<PotionEffect> effects = this.activeEffects.computeIfAbsent(player.getUniqueId(), k -> new ArrayList<>());

effects.add(effect);
}

public void restoreActiveEffects(Player player) {
List<PotionEffect> currentEffects = this.getCurrentEffects(player);

for (PotionEffect effect : currentEffects) {
player.addPotionEffect(effect);
}

this.clearStoredEffects(player);
}

public void clearStoredEffects(Player player) {
this.activeEffects.remove(player.getUniqueId());
}

public List<PotionEffect> getCurrentEffects(Player player) {
return this.activeEffects.getOrDefault(player.getUniqueId(), new ArrayList<>());
}

public void applyCustomEffect(Player player, PotionEffectType type, Integer amplifier) {
PotionEffect activeEffect = player.getPotionEffect(type);

if (activeEffect == null) {
player.addPotionEffect(new PotionEffect(type, INFINITE_DURATION, amplifier));
return;
}

if (activeEffect.getAmplifier() > amplifier) {
return;
}
/**
* Manages custom potion effects on players during combat.
* Active effects before combat are stored and restored after combat ends.
*/
public interface FightEffectService {

if (activeEffect.getDuration() == -1) {
return;
}
void removeCustomEffect(Player player, PotionEffectType type, Integer amplifier);

this.storeActiveEffect(player, activeEffect);
player.addPotionEffect(new PotionEffect(type, INFINITE_DURATION, amplifier));
}
void applyCustomEffect(Player player, PotionEffectType type, Integer amplifier);

public void removeCustomEffect(Player player, PotionEffectType type, Integer amplifier) {
PotionEffect activeEffect = player.getPotionEffect(type);
List<PotionEffect> getCurrentEffects(Player player);

if (activeEffect == null) {
return;
}
void clearStoredEffects(Player player);

if (activeEffect.getAmplifier() != amplifier) {
return;
}
void restoreActiveEffects(Player player);

player.removePotionEffect(type);
}
void storeActiveEffect(Player player, PotionEffect effect);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.eternalcode.combat.fight.event;

public enum CancelTagReason {

TAGOUT

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
package com.eternalcode.combat.fight.event;

public enum CauseOfTag {

/**
* The player was tagged in combat as a result of an attack by another player.
*/
PLAYER,

/**
* The player was tagged in combat due to an interaction with a non-player entity
* (e.g., mobs or environmental damage).
*/
NON_PLAYER,

/**
* A command was executed to apply a combat tag to the player.
*/
COMMAND,

/**
* A custom cause, typically defined by external plugins or systems, applied the combat tag.
*/
CUSTOM
}
Loading
Loading