Skip to content

Commit

Permalink
add player as shooter predicate for each module
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Jun 24, 2024
1 parent 142ecde commit e2ddbc1
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 107 deletions.
7 changes: 7 additions & 0 deletions src/main/java/me/xginko/snowballfight/SnowballFight.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.xginko.snowballfight;

import com.tcoded.folialib.FoliaLib;
import com.tcoded.folialib.impl.ServerImplementation;
import me.xginko.snowballfight.commands.snowballs.SnowballsCommand;
import me.xginko.snowballfight.modules.SnowballModule;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
Expand Down Expand Up @@ -100,6 +101,12 @@ public static BukkitAudiences getAudiences() {
public static FoliaLib getFoliaLib() {
return foliaLib;
}
public static ServerImplementation getScheduler() {
return foliaLib.getImpl();
}
public static boolean isServerFolia() {
return foliaLib.isFolia();
}
public static SnowballConfig config() {
return config;
}
Expand Down
27 changes: 13 additions & 14 deletions src/main/java/me/xginko/snowballfight/modules/DamageOnHit.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,37 @@
package me.xginko.snowballfight.modules;

import com.tcoded.folialib.FoliaLib;
import com.tcoded.folialib.impl.ServerImplementation;
import me.xginko.snowballfight.SnowballConfig;
import me.xginko.snowballfight.SnowballFight;
import me.xginko.snowballfight.utils.EntityUtil;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.ProjectileHitEvent;

import java.util.Collections;
import java.util.HashSet;
import java.util.EnumSet;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

public class DamageOnHit implements SnowballModule, Listener {

private final ServerImplementation scheduler;
private final HashSet<EntityType> configuredTypes;
private final Set<EntityType> configuredTypes;
private final double damage;
private final boolean isFolia, onlyForSpecificEntities, asBlacklist;
private final boolean onlyForSpecificEntities, asBlacklist, onlyPlayers;

protected DamageOnHit() {
shouldEnable();
FoliaLib foliaLib = SnowballFight.getFoliaLib();
this.isFolia = foliaLib.isFolia();
this.scheduler = isFolia ? foliaLib.getImpl() : null;
SnowballConfig config = SnowballFight.config();
config.master().addComment("settings.damage", "\nEnable snowballs dealing damage when they hit an entity.");
this.damage = config.getDouble("settings.damage.damage", 3.0,
"Configure the damage that entities take from getting hit by a snowball.");
this.onlyPlayers = config.getBoolean("settings.damage.only-thrown-by-player", true,
"If enabled will only work if the snowball was thrown by a player.");
this.onlyForSpecificEntities = config.getBoolean("settings.damage.only-for-specific-entities", false,
"When enabled, only configured entities will take extra damage when hit by a snowball.");
this.asBlacklist = config.getBoolean("settings.damage.use-list-as-blacklist", false,
Expand All @@ -45,13 +43,13 @@ protected DamageOnHit() {
try {
return EntityType.valueOf(configuredType);
} catch (IllegalArgumentException e) {
SnowballFight.logger().warn("(Damage) Configured entity type '"+configuredType+"' not recognized. " +
"Please use correct values from: https://jd.papermc.io/paper/1.20/org/bukkit/entity/EntityType.html");
SnowballFight.logger().warn("(Damage) Configured entity type '{}' not recognized. " +
"Please use correct values from: https://jd.papermc.io/paper/1.20/org/bukkit/entity/EntityType.html", configuredType);
return null;
}
})
.filter(Objects::nonNull)
.collect(Collectors.toCollection(HashSet::new));
.collect(Collectors.toCollection(() -> EnumSet.noneOf(EntityType.class)));
}

@Override
Expand All @@ -74,12 +72,13 @@ public void disable() {
private void onSnowballHit(ProjectileHitEvent event) {
if (!event.getEntityType().equals(EntityType.SNOWBALL)) return;
if (!EntityUtil.isLivingEntity(event.getHitEntity())) return;
if (onlyPlayers && !(event.getEntity().getShooter() instanceof Player)) return;

final LivingEntity living = (LivingEntity) event.getHitEntity();
if (onlyForSpecificEntities && (asBlacklist == configuredTypes.contains(living.getType()))) return;

if (isFolia) {
scheduler.runAtEntity(living, dmg -> living.damage(damage, event.getEntity()));
if (SnowballFight.isServerFolia()) {
SnowballFight.getScheduler().runAtEntity(living, dmg -> living.damage(damage, event.getEntity()));
} else {
living.damage(damage, event.getEntity());
}
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/me/xginko/snowballfight/modules/ExplodeOnHit.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package me.xginko.snowballfight.modules;

import com.tcoded.folialib.FoliaLib;
import com.tcoded.folialib.impl.ServerImplementation;
import me.xginko.snowballfight.SnowballConfig;
import me.xginko.snowballfight.SnowballFight;
import me.xginko.snowballfight.events.PostSnowballExplodeEvent;
Expand All @@ -10,6 +8,7 @@
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Snowball;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
Expand All @@ -25,18 +24,17 @@

public class ExplodeOnHit implements SnowballModule, Listener {

private final ServerImplementation scheduler;
private final Set<EntityType> configuredTypes;
private final float explosionPower;
private final boolean explosionSetFire, explosionBreakBlocks, onlyForEntities, onlyForSpecificEntities, asBlacklist, isFolia;
private final boolean explosionSetFire, explosionBreakBlocks, onlyForEntities, onlyForSpecificEntities, asBlacklist,
onlyPlayers;

protected ExplodeOnHit() {
shouldEnable();
FoliaLib foliaLib = SnowballFight.getFoliaLib();
this.isFolia = foliaLib.isFolia();
this.scheduler = isFolia ? foliaLib.getImpl() : null;
SnowballConfig config = SnowballFight.config();
config.master().addComment("settings.explosions","\nMake snowballs explode when hitting something.");
this.onlyPlayers = config.getBoolean("settings.explosions.only-thrown-by-player", true,
"If enabled will only work if the snowball was thrown by a player.");
this.explosionPower = config.getFloat("settings.explosions.power", 2.0F,
"TNT has a power of 4.0.");
this.explosionSetFire = config.getBoolean("settings.explosions.set-fire", false,
Expand Down Expand Up @@ -94,6 +92,8 @@ private void onSnowballHit(ProjectileHitEvent event) {
if (onlyForSpecificEntities && (asBlacklist == configuredTypes.contains(hitEntity.getType()))) return;
}

if (onlyPlayers && !(event.getEntity().getShooter() instanceof Player)) return;

PreSnowballExplodeEvent preSnowballExplodeEvent = new PreSnowballExplodeEvent(
(Snowball) event.getEntity(),
hitEntity,
Expand All @@ -109,8 +109,8 @@ private void onSnowballHit(ProjectileHitEvent event) {
final Location explodeLoc = preSnowballExplodeEvent.getExplodeLocation();
final Snowball snowball = preSnowballExplodeEvent.getSnowball();

if (isFolia) {
scheduler.runAtLocation(explodeLoc, snobol -> {
if (SnowballFight.isServerFolia()) {
SnowballFight.getScheduler().runAtLocation(explodeLoc, snobol -> {
new PostSnowballExplodeEvent(
preSnowballExplodeEvent.getSnowball(),
preSnowballExplodeEvent.getHitEntity(),
Expand Down
51 changes: 33 additions & 18 deletions src/main/java/me/xginko/snowballfight/modules/FireworkOnHit.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import com.destroystokyo.paper.event.entity.EntityKnockbackByEntityEvent;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.tcoded.folialib.FoliaLib;
import com.tcoded.folialib.impl.ServerImplementation;
import me.xginko.snowballfight.SnowballCache;
import me.xginko.snowballfight.SnowballConfig;
import me.xginko.snowballfight.SnowballFight;
Expand All @@ -16,6 +14,7 @@
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Firework;
import org.bukkit.entity.Player;
import org.bukkit.entity.Snowball;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
Expand All @@ -25,29 +24,34 @@
import org.bukkit.event.entity.ProjectileHitEvent;
import org.bukkit.inventory.meta.FireworkMeta;

import java.util.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

public class FireworkOnHit implements SnowballModule, Listener {

private final ServerImplementation scheduler;
private final SnowballCache snowballCache;
private final Cache<UUID, Boolean> snowballFireworks;
private final List<FireworkEffect.Type> effectTypes;
private final HashSet<EntityType> configuredTypes;
private final boolean isFolia, dealDamage, dealKnockback, flicker, trail, onlyForEntities, onlyForSpecificEntities, asBlacklist;
private final Set<EntityType> configuredTypes;
private final boolean dealDamage, dealKnockback, flicker, trail, onlyForEntities, onlyForSpecificEntities,
asBlacklist, onlyPlayers;

protected FireworkOnHit() {
shouldEnable();
FoliaLib foliaLib = SnowballFight.getFoliaLib();
this.isFolia = foliaLib.isFolia();
this.scheduler = isFolia ? foliaLib.getImpl() : null;
this.snowballCache = SnowballFight.getCache();
this.snowballFireworks = Caffeine.newBuilder().expireAfterWrite(2, TimeUnit.SECONDS).build();
SnowballConfig config = SnowballFight.config();
config.master().addComment("settings.fireworks",
"\nDetonate a firework when a snowball hits something for a cool effect.");
this.onlyPlayers = config.getBoolean("settings.fireworks.only-thrown-by-player", true,
"If enabled will only work if the snowball was thrown by a player.");
this.dealDamage = config.getBoolean("settings.fireworks.deal-damage", false,
"Should firework effects deal damage like regular fireworks?");
this.dealKnockback = config.getBoolean("settings.fireworks.deal-knockback", false,
Expand All @@ -64,8 +68,8 @@ protected FireworkOnHit() {
try {
return FireworkEffect.Type.valueOf(effect);
} catch (IllegalArgumentException e) {
SnowballFight.logger().warn("FireworkEffect Type '"+effect+"' not recognized. " +
"Please use valid enums from: https://jd.papermc.io/paper/1.20/org/bukkit/FireworkEffect.Type.html");
SnowballFight.logger().warn("(Fireworks) FireworkEffect Type '{}' not recognized. Please use valid enums from:" +
" https://jd.papermc.io/paper/1.20/org/bukkit/FireworkEffect.Type.html", effect);
return null;
}
})
Expand All @@ -89,13 +93,13 @@ protected FireworkOnHit() {
try {
return EntityType.valueOf(configuredType);
} catch (IllegalArgumentException e) {
SnowballFight.logger().warn("(Fireworks) Configured entity type '"+configuredType+"' not recognized. " +
"Please use correct values from: https://jd.papermc.io/paper/1.20/org/bukkit/entity/EntityType.html");
SnowballFight.logger().warn("(Fireworks) Configured entity type '{}' not recognized. " +
"Please use correct values from: https://jd.papermc.io/paper/1.20/org/bukkit/entity/EntityType.html", configuredType);
return null;
}
})
.filter(Objects::nonNull)
.collect(Collectors.toCollection(HashSet::new));
.collect(Collectors.toCollection(() -> EnumSet.noneOf(EntityType.class)));
}

@Override
Expand All @@ -117,14 +121,18 @@ public void disable() {
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
private void onSnowballHit(ProjectileHitEvent event) {
if (!event.getEntityType().equals(EntityType.SNOWBALL)) return;

final Entity hitEntity = event.getHitEntity();
if (onlyForEntities) {
if (hitEntity == null) return;
if (onlyForSpecificEntities && (asBlacklist == configuredTypes.contains(hitEntity.getType()))) return;
}

if (onlyPlayers && !(event.getEntity().getShooter() instanceof Player)) return;

if (hitEntity != null) {
if (isFolia) scheduler.runAtEntity(hitEntity, firework -> detonateFirework(hitEntity.getLocation(), (Snowball) event.getEntity()));
if (SnowballFight.isServerFolia()) SnowballFight.getScheduler()
.runAtEntity(hitEntity, firework -> detonateFirework(hitEntity.getLocation(), (Snowball) event.getEntity()));
else detonateFirework(hitEntity.getLocation(), (Snowball) event.getEntity());
return;
}
Expand All @@ -133,9 +141,16 @@ private void onSnowballHit(ProjectileHitEvent event) {

if (hitBlock != null) {
final BlockFace hitFace = event.getHitBlockFace();
final Location fireworkLoc = hitFace != null ? hitBlock.getRelative(hitFace).getLocation().toCenterLocation() : hitBlock.getLocation().toCenterLocation();
if (isFolia) scheduler.runAtLocation(fireworkLoc, firework -> detonateFirework(fireworkLoc, (Snowball) event.getEntity()));
else detonateFirework(hitBlock.getLocation(), (Snowball) event.getEntity());
final Location fireworkLoc;

if (hitFace != null) fireworkLoc = hitBlock.getRelative(hitFace).getLocation().toCenterLocation();
else fireworkLoc = hitBlock.getLocation().toCenterLocation();

if (SnowballFight.isServerFolia()) {
SnowballFight.getScheduler().runAtLocation(fireworkLoc, firework -> detonateFirework(fireworkLoc, (Snowball) event.getEntity()));
} else {
detonateFirework(hitBlock.getLocation(), (Snowball) event.getEntity());
}
}
}

Expand Down
21 changes: 10 additions & 11 deletions src/main/java/me/xginko/snowballfight/modules/KnockbackOnHit.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package me.xginko.snowballfight.modules;

import com.tcoded.folialib.FoliaLib;
import com.tcoded.folialib.impl.ServerImplementation;
import me.xginko.snowballfight.SnowballConfig;
import me.xginko.snowballfight.SnowballFight;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
Expand All @@ -22,19 +21,17 @@

public class KnockbackOnHit implements SnowballModule, Listener {

private final ServerImplementation scheduler;
private final Set<EntityType> configuredTypes;
private final Vector vectorModifier;
private final double multiplier;
private final boolean isFolia, modifyVector, onlyForSpecificEntities, asBlacklist;
private final boolean modifyVector, onlyForSpecificEntities, asBlacklist, onlyPlayers;

protected KnockbackOnHit() {
shouldEnable();
FoliaLib foliaLib = SnowballFight.getFoliaLib();
this.isFolia = foliaLib.isFolia();
this.scheduler = isFolia ? foliaLib.getImpl() : null;
SnowballConfig config = SnowballFight.config();
config.master().addComment("settings.knockback", "Modify knockback values on snowball hit.");
this.onlyPlayers = config.getBoolean("settings.knockback.only-thrown-by-player", true,
"If enabled will only work if the snowball was thrown by a player.");
this.multiplier = config.getDouble("settings.knockback.multiplier", 1.2,
"The multiplier for the knockback of the snowball.");
this.modifyVector = config.getBoolean("settings.knockback.vector-modifier.enable", true);
Expand Down Expand Up @@ -82,13 +79,15 @@ public void disable() {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onSnowballHit(ProjectileHitEvent event) {
if (!event.getEntityType().equals(EntityType.SNOWBALL)) return;
final Projectile snowball = event.getEntity();
if (onlyForSpecificEntities && (asBlacklist == configuredTypes.contains(snowball.getType()))) return;
final Entity hitEntity = event.getHitEntity();
if (hitEntity == null) return;
if (onlyForSpecificEntities && (asBlacklist == configuredTypes.contains(hitEntity.getType()))) return;

final Projectile snowball = event.getEntity();
if (onlyPlayers && !(snowball.getShooter() instanceof Player)) return;

if (isFolia) {
scheduler.runAtEntity(hitEntity, knockback -> {
if (SnowballFight.isServerFolia()) {
SnowballFight.getScheduler().runAtEntity(hitEntity, knockback -> {
if (modifyVector) hitEntity.setVelocity(snowball.getVelocity().multiply(multiplier).add(vectorModifier));
else hitEntity.setVelocity(snowball.getVelocity().multiply(multiplier));
});
Expand Down
Loading

0 comments on commit e2ddbc1

Please sign in to comment.