added forbiddenItems appliance with material detection
This commit is contained in:
parent
fcc2abdc49
commit
fdbb525870
@ -0,0 +1,60 @@
|
|||||||
|
package eu.mhsl.craftattack.spawn.varo.appliances.metaGameplay.forbiddenItems;
|
||||||
|
|
||||||
|
import eu.mhsl.craftattack.core.appliance.Appliance;
|
||||||
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.entity.Entity;
|
||||||
|
import org.bukkit.entity.Villager;
|
||||||
|
import org.bukkit.event.Listener;
|
||||||
|
import org.bukkit.potion.PotionEffect;
|
||||||
|
import org.bukkit.potion.PotionEffectType;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
public class ForbiddenItems extends Appliance {
|
||||||
|
Set<Function<Material, Boolean>> forbiddenMaterials = Set.of(
|
||||||
|
(material) -> material.name().startsWith("NETHERITE"),
|
||||||
|
(material) -> material.equals(Material.TOTEM_OF_UNDYING),
|
||||||
|
(material) -> material.equals(Material.END_CRYSTAL),
|
||||||
|
(material) -> material.equals(Material.MACE),
|
||||||
|
(material) -> material.equals(Material.ENCHANTED_GOLDEN_APPLE)
|
||||||
|
);
|
||||||
|
|
||||||
|
Set<Function<Entity, Boolean>> forbiddenEntities = Set.of(
|
||||||
|
(entity) -> {
|
||||||
|
if(!(entity instanceof Villager villager)) return false;
|
||||||
|
return List.of(
|
||||||
|
Villager.Profession.LIBRARIAN,
|
||||||
|
Villager.Profession.ARMORER
|
||||||
|
).contains(villager.getProfession());
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Set<Function<PotionEffect, Boolean>> forbiddenPotions = Set.of(
|
||||||
|
(potion) -> !potion.getType().equals(PotionEffectType.INSTANT_HEALTH) && potion.getAmplifier() >= 2
|
||||||
|
);
|
||||||
|
|
||||||
|
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
|
||||||
|
protected @NotNull List<Listener> listeners() {
|
||||||
|
return List.of(
|
||||||
|
new ForbiddenMaterialListener()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
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.Material;
|
||||||
|
import org.bukkit.entity.Entity;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.event.Cancellable;
|
||||||
|
import org.bukkit.event.EventHandler;
|
||||||
|
import org.bukkit.event.entity.EntityPickupItemEvent;
|
||||||
|
import org.bukkit.event.inventory.ClickType;
|
||||||
|
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
class ForbiddenMaterialListener extends ApplianceListener<ForbiddenItems> {
|
||||||
|
@EventHandler
|
||||||
|
public void onPickup(EntityPickupItemEvent event) {
|
||||||
|
this.testAndCancel(event, event.getItem().getItemStack().getType(), event.getEntity());
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void onInventoryInteraction(InventoryClickEvent event) {
|
||||||
|
if(event.getCurrentItem() == null) return;
|
||||||
|
if(List.of(ClickType.DROP, ClickType.CONTROL_DROP).contains(event.getClick())) return;
|
||||||
|
this.testAndCancel(event, event.getCurrentItem().getType(), event.getWhoClicked());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void testAndCancel(Cancellable event, Material material, Entity entity) {
|
||||||
|
if(!this.getAppliance().isForbidden(material)) return;
|
||||||
|
event.setCancelled(true);
|
||||||
|
|
||||||
|
if(entity instanceof Player p) {
|
||||||
|
Component itemName = material.getItemTranslationKey() != null
|
||||||
|
? Component.translatable(material.getItemTranslationKey())
|
||||||
|
: Component.text(material.name());
|
||||||
|
|
||||||
|
p.sendActionBar(
|
||||||
|
Component.text()
|
||||||
|
.append(itemName)
|
||||||
|
.append(Component.text(" ist ein verbotener Gegenstand!"))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user