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-139 Prevent flying during combat feature #139

Merged
merged 2 commits into from
Dec 24, 2023
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
3 changes: 3 additions & 0 deletions eternalcombat-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ dependencies {
// bstats
api(libs.bStatsBukkit)

// caffeine
api(libs.caffeine)

// worldguard
compileOnly(libs.worldGuardBukkit)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ public static class Settings extends OkaeriConfig {
@Comment("# Block the use of elytra?")
public boolean shouldPreventElytraUsage = true;

@Comment("# Block flying? (flying players will fall to the ground)")
public boolean shouldPreventFlying = true;

@Comment("# Disable the use of elytra on damage?")
public boolean shouldElytraDisableOnDamage = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.bukkit.event.entity.EntityToggleGlideEvent;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerToggleFlightEvent;

import java.util.List;
import java.util.UUID;
Expand Down Expand Up @@ -63,7 +64,7 @@ void onPlace(BlockPlaceEvent event) {
}

@EventHandler
public void onToggleGlide(EntityToggleGlideEvent event) {
void onToggleGlide(EntityToggleGlideEvent event) {
if (!this.config.settings.shouldPreventElytraUsage) {
return;
}
Expand All @@ -83,7 +84,27 @@ public void onToggleGlide(EntityToggleGlideEvent event) {
}

@EventHandler
public void onDamage(EntityDamageEvent event) {
void onFly(PlayerToggleFlightEvent event) {
if (!this.config.settings.shouldPreventFlying) {
return;
}

Player player = event.getPlayer();
UUID uniqueId = player.getUniqueId();

if (!this.fightManager.isInCombat(uniqueId)) {
return;
}

if (event.isFlying()) {
player.setAllowFlight(false);

event.setCancelled(true);
}
}

@EventHandler
void onDamage(EntityDamageEvent event) {
if (!this.config.settings.shouldElytraDisableOnDamage) {
return;
}
Expand Down Expand Up @@ -117,7 +138,7 @@ void onOpenInventory(InventoryOpenEvent event) {
}

@EventHandler
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
Player player = event.getPlayer();
UUID playerUniqueId = player.getUniqueId();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
return;
}

if (this.config.settings.shouldPreventFlying) {
if (attackedPlayerByPerson.isFlying()) {
attackedPlayerByPerson.setFlying(false);
attackedPlayerByPerson.setAllowFlight(false);
}

if (attacker.isFlying()) {
attacker.setFlying(false);
attacker.setAllowFlight(false);
}
}

this.fightManager.tag(attackedUniqueId, combatTime, CauseOfTag.PLAYER);
this.fightManager.tag(attackerUniqueId, combatTime, CauseOfTag.PLAYER);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.eternalcode.combat.fight.pearl;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;

import java.time.Duration;
import java.time.Instant;
Expand All @@ -14,7 +14,7 @@ public class FightPearlManager {

public FightPearlManager(FightPearlSettings pearlSettings) {
this.pearlSettings = pearlSettings;
this.pearlDelays = CacheBuilder.newBuilder()
this.pearlDelays = Caffeine.newBuilder()
.expireAfterWrite(pearlSettings.pearlThrowDelay)
.build();
}
Expand Down
3 changes: 2 additions & 1 deletion eternalcombat-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ tasks.shadowJar {
"com.eternalcode.gitcheck",
"org.json.simple",
"org.apache.commons",
"javassist"
"javassist",
"com.github.benmanes.caffeine",
).forEach { pack ->
relocate(pack, "$prefix.$pack")
}
Expand Down
4 changes: 4 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pandaUtilities = "0.5.2-alpha"
gitCheck = "1.0.0"
apacheCommons = "2.15.0"

caffeine = "3.0.4"

bStatsBukkit = "3.0.2"
worldGuardBukkit = "7.0.9"

Expand Down Expand Up @@ -43,6 +45,8 @@ bStatsBukkit = { group = "org.bstats", name = "bstats-bukkit", version.ref = "bS

worldGuardBukkit = { group = "com.sk89q.worldguard", name = "worldguard-bukkit", version.ref = "worldGuardBukkit" }

caffeine = { group = "com.github.ben-manes.caffeine", name = "caffeine", version.ref = "caffeine" }

jUnitJupiterApi = { group = "org.junit.jupiter", name = "junit-jupiter-api", version.ref = "junitJupiterApi" }
jUnitJupiterParams = { group = "org.junit.jupiter", name = "junit-jupiter-params", version.ref = "junitJupiterParams" }
jUnitJupiterEngine = { group = "org.junit.jupiter", name = "junit-jupiter-engine", version.ref = "junitJupiterEngine" }