added AntiAutoTotem
This commit is contained in:
@@ -0,0 +1,62 @@
|
|||||||
|
package eu.mhsl.craftattack.spawn.craftattack.appliances.security.antiAutoTotem;
|
||||||
|
|
||||||
|
import eu.mhsl.craftattack.spawn.common.appliances.tooling.acInform.AcInform;
|
||||||
|
import eu.mhsl.craftattack.spawn.core.Main;
|
||||||
|
import eu.mhsl.craftattack.spawn.core.appliance.Appliance;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.event.Listener;
|
||||||
|
import org.bukkit.inventory.PlayerInventory;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
public class AntiAutoTotem extends Appliance {
|
||||||
|
|
||||||
|
public void checkTotemUse(Player player) {
|
||||||
|
PlayerInventory playerInv = player.getInventory();
|
||||||
|
|
||||||
|
Supplier<List<Material>> getHeldItems = () -> List.of(
|
||||||
|
playerInv.getItemInMainHand().getType(),
|
||||||
|
playerInv.getItemInOffHand().getType()
|
||||||
|
);
|
||||||
|
Function<Player, Boolean> isCurrentlyHoldingTotem = (p) -> getHeldItems.get().contains(Material.TOTEM_OF_UNDYING);
|
||||||
|
|
||||||
|
if(!isCurrentlyHoldingTotem.apply(player)) return;
|
||||||
|
if(getHeldItems.get().stream().allMatch(material -> material.equals(Material.TOTEM_OF_UNDYING))) return;
|
||||||
|
|
||||||
|
AtomicInteger tickCounter = new AtomicInteger();
|
||||||
|
Bukkit.getScheduler().runTaskTimer(
|
||||||
|
Main.instance(),
|
||||||
|
(task) -> {
|
||||||
|
if(tickCounter.incrementAndGet() > 10) {
|
||||||
|
task.cancel();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(isCurrentlyHoldingTotem.apply(player)) {
|
||||||
|
task.cancel();
|
||||||
|
Main.instance().getAppliance(AcInform.class).notifyAdmins(
|
||||||
|
"internal",
|
||||||
|
player.getName(),
|
||||||
|
"antiAutoTotem",
|
||||||
|
(float) tickCounter.get()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected @NotNull List<Listener> listeners() {
|
||||||
|
return List.of(
|
||||||
|
new OnTotemUseListener()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,15 @@
|
|||||||
|
package eu.mhsl.craftattack.spawn.craftattack.appliances.security.antiAutoTotem;
|
||||||
|
|
||||||
|
import eu.mhsl.craftattack.spawn.core.appliance.ApplianceListener;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.event.EventHandler;
|
||||||
|
import org.bukkit.event.entity.EntityResurrectEvent;
|
||||||
|
|
||||||
|
class OnTotemUseListener extends ApplianceListener<AntiAutoTotem> {
|
||||||
|
@EventHandler
|
||||||
|
public void onTotem(EntityResurrectEvent event) {
|
||||||
|
if(event.isCancelled()) return;
|
||||||
|
if(!(event.getEntity() instanceof Player player)) return;
|
||||||
|
this.getAppliance().checkTotemUse(player);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user