added entity and potion listeners

This commit is contained in:
Lars Neuhaus 2025-04-07 17:52:48 +02:00
parent fdbb525870
commit 153a968776
5 changed files with 109 additions and 25 deletions

View File

@ -0,0 +1,27 @@
package eu.mhsl.craftattack.spawn.varo.appliances.metaGameplay.forbiddenItems;
import eu.mhsl.craftattack.core.appliance.ApplianceListener;
import net.kyori.adventure.text.Component;
import org.bukkit.event.EventHandler;
import org.bukkit.event.entity.EntitySpawnEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
class ForbiddenEntityListener extends ApplianceListener<ForbiddenItems> {
@EventHandler
public void onEntitySpawn(EntitySpawnEvent event) {
if(!this.getAppliance().forbiddenEntities.isForbidden(event.getEntity())) return;
event.setCancelled(true);
}
@EventHandler
public void onEntityInteraction(PlayerInteractEntityEvent event) {
if(!this.getAppliance().forbiddenEntities.isForbidden(event.getRightClicked())) return;
event.setCancelled(true);
event.getPlayer().sendActionBar(
Component.text()
.append(Component.text(event.getRightClicked().getName()))
.append(Component.text(" ist eine verbotene Entität!"))
);
}
}

View File

@ -0,0 +1,17 @@
package eu.mhsl.craftattack.spawn.varo.appliances.metaGameplay.forbiddenItems;
import java.util.Set;
import java.util.function.Function;
public class ForbiddenGroup<TType> {
private final Set<Function<TType, Boolean>> forbiddenT;
public ForbiddenGroup(Set<Function<TType, Boolean>> forbiddenT) {
this.forbiddenT = forbiddenT;
}
public boolean isForbidden(TType value) {
return this.forbiddenT.stream()
.anyMatch(test -> test.apply(value));
}
}

View File

@ -11,18 +11,17 @@ import org.jetbrains.annotations.NotNull;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.function.Function;
public class ForbiddenItems extends Appliance { public class ForbiddenItems extends Appliance {
Set<Function<Material, Boolean>> forbiddenMaterials = Set.of( public ForbiddenGroup<Material> forbiddenMaterials = new ForbiddenGroup<>(Set.of(
(material) -> material.name().startsWith("NETHERITE"), (material) -> material.name().startsWith("NETHERITE"),
(material) -> material.equals(Material.TOTEM_OF_UNDYING), (material) -> material.equals(Material.TOTEM_OF_UNDYING),
(material) -> material.equals(Material.END_CRYSTAL), (material) -> material.equals(Material.END_CRYSTAL),
(material) -> material.equals(Material.MACE), (material) -> material.equals(Material.MACE),
(material) -> material.equals(Material.ENCHANTED_GOLDEN_APPLE) (material) -> material.equals(Material.ENCHANTED_GOLDEN_APPLE)
); ));
Set<Function<Entity, Boolean>> forbiddenEntities = Set.of( public ForbiddenGroup<Entity> forbiddenEntities = new ForbiddenGroup<>(Set.of(
(entity) -> { (entity) -> {
if(!(entity instanceof Villager villager)) return false; if(!(entity instanceof Villager villager)) return false;
return List.of( return List.of(
@ -30,31 +29,18 @@ public class ForbiddenItems extends Appliance {
Villager.Profession.ARMORER Villager.Profession.ARMORER
).contains(villager.getProfession()); ).contains(villager.getProfession());
} }
); ));
Set<Function<PotionEffect, Boolean>> forbiddenPotions = Set.of( public ForbiddenGroup<PotionEffect> forbiddenPotions = new ForbiddenGroup<>(Set.of(
(potion) -> !potion.getType().equals(PotionEffectType.INSTANT_HEALTH) && potion.getAmplifier() >= 2 (potion) -> !potion.getType().equals(PotionEffectType.INSTANT_HEALTH) && potion.getAmplifier() >= 1 // Amplifier starts at 0
); ));
public boolean isForbidden(Material material) {
return this.forbiddenMaterials.stream()
.anyMatch(test -> test.apply(material));
}
public boolean isForbidden(Entity entity) {
return this.forbiddenEntities.stream()
.anyMatch(test -> test.apply(entity));
}
public boolean isForbidden(PotionEffect potionEffect) {
return this.forbiddenPotions.stream()
.anyMatch(test -> test.apply(potionEffect));
}
@Override @Override
protected @NotNull List<Listener> listeners() { protected @NotNull List<Listener> listeners() {
return List.of( return List.of(
new ForbiddenMaterialListener() new ForbiddenMaterialListener(),
new ForbiddenEntityListener(),
new ForbiddenPotionsListener()
); );
} }
} }

View File

@ -27,7 +27,7 @@ class ForbiddenMaterialListener extends ApplianceListener<ForbiddenItems> {
} }
private void testAndCancel(Cancellable event, Material material, Entity entity) { private void testAndCancel(Cancellable event, Material material, Entity entity) {
if(!this.getAppliance().isForbidden(material)) return; if(!this.getAppliance().forbiddenMaterials.isForbidden(material)) return;
event.setCancelled(true); event.setCancelled(true);
if(entity instanceof Player p) { if(entity instanceof Player p) {

View File

@ -0,0 +1,54 @@
package eu.mhsl.craftattack.spawn.varo.appliances.metaGameplay.forbiddenItems;
import eu.mhsl.craftattack.core.appliance.ApplianceListener;
import net.kyori.adventure.text.Component;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.EventHandler;
import org.bukkit.event.entity.LingeringPotionSplashEvent;
import org.bukkit.event.entity.PotionSplashEvent;
import org.bukkit.event.player.PlayerItemConsumeEvent;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionEffect;
import javax.annotation.Nullable;
import java.util.Collection;
class ForbiddenPotionsListener extends ApplianceListener<ForbiddenItems> {
@EventHandler
public void onPotionSplash(PotionSplashEvent event) {
Collection<PotionEffect> effects = event.getPotion().getEffects();
this.testAndCancel(event, effects, null);
}
@EventHandler
public void onPotionConsume(PlayerItemConsumeEvent event) {
if(!(event.getItem().getItemMeta() instanceof PotionMeta meta)) return;
if(meta.getBasePotionType() == null) return;
Collection<PotionEffect> effects = meta.getBasePotionType().getPotionEffects();
this.testAndCancel(event, effects, event.getPlayer());
}
@EventHandler
public void onLingeringPotionSplash(LingeringPotionSplashEvent event) {
if(event.getAreaEffectCloud().getBasePotionType() == null) return;
Collection<PotionEffect> effects = event.getAreaEffectCloud().getBasePotionType().getPotionEffects();
this.testAndCancel(event, effects, null);
}
private void testAndCancel(Cancellable event, Collection<PotionEffect> effects, @Nullable Player player) {
if(effects.stream().noneMatch(potionEffect -> this.getAppliance().forbiddenPotions.isForbidden(potionEffect))) return;
event.setCancelled(true);
if(player == null) return;
player.sendActionBar(
Component.text()
.append(Component.text("Das ist ein verbotener Trank!"))
);
}
}