finalized JoinProtection
This commit is contained in:
parent
d94bbb7417
commit
859733e3dd
@ -2,6 +2,7 @@ package eu.mhsl.craftattack.spawn.varo.appliances.metaGameplay.joinProtection;
|
|||||||
|
|
||||||
import eu.mhsl.craftattack.core.Main;
|
import eu.mhsl.craftattack.core.Main;
|
||||||
import eu.mhsl.craftattack.core.appliance.Appliance;
|
import eu.mhsl.craftattack.core.appliance.Appliance;
|
||||||
|
import net.kyori.adventure.text.Component;
|
||||||
import net.kyori.adventure.util.Ticks;
|
import net.kyori.adventure.util.Ticks;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
@ -10,30 +11,41 @@ import org.bukkit.event.Listener;
|
|||||||
import org.bukkit.potion.PotionEffect;
|
import org.bukkit.potion.PotionEffect;
|
||||||
import org.bukkit.potion.PotionEffectType;
|
import org.bukkit.potion.PotionEffectType;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.List;
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
public class JoinProtection extends Appliance {
|
public class JoinProtection extends Appliance {
|
||||||
private static final int resistanceDuration = 30;
|
private static final int resistanceDuration = 10;
|
||||||
|
|
||||||
private final List<UUID> protectedPlayers = new ArrayList<>();
|
static final class Options {
|
||||||
|
public boolean wasOnGround = false;
|
||||||
|
}
|
||||||
|
private final Map<UUID, Options> protectedPlayers = new HashMap<>();
|
||||||
|
|
||||||
public void addProtection(Player player) {
|
public void addProtection(Player player) {
|
||||||
protectedPlayers.add(player.getUniqueId());
|
this.protectedPlayers.put(player.getUniqueId(), new Options());
|
||||||
PotionEffect effect = new PotionEffect(PotionEffectType.RESISTANCE, Ticks.TICKS_PER_SECOND*resistanceDuration, 1);
|
PotionEffect resistance = new PotionEffect(PotionEffectType.RESISTANCE, Ticks.TICKS_PER_SECOND * resistanceDuration, 1);
|
||||||
player.addPotionEffect(effect, true);
|
PotionEffect blindness = new PotionEffect(PotionEffectType.DARKNESS, Ticks.TICKS_PER_SECOND * 3, 1);
|
||||||
|
player.addPotionEffects(List.of(resistance, blindness));
|
||||||
|
|
||||||
Bukkit.getScheduler().runTaskLater(
|
Bukkit.getScheduler().runTaskLater(
|
||||||
Main.instance(),
|
Main.instance(),
|
||||||
() -> protectedPlayers.remove(player.getUniqueId()),
|
() -> this.protectedPlayers.remove(player.getUniqueId()),
|
||||||
Ticks.TICKS_PER_SECOND*resistanceDuration
|
Ticks.TICKS_PER_SECOND * resistanceDuration
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable Options getOptions(Player player) {
|
||||||
|
return this.protectedPlayers.get(player.getUniqueId());
|
||||||
|
}
|
||||||
|
|
||||||
public void cancelEvent(Player player, Cancellable event) {
|
public void cancelEvent(Player player, Cancellable event) {
|
||||||
if(!protectedPlayers.contains(player.getUniqueId())) return;
|
if(!this.protectedPlayers.containsKey(player.getUniqueId())) return;
|
||||||
event.setCancelled(true);
|
event.setCancelled(true);
|
||||||
|
player.sendActionBar(
|
||||||
|
Component.text("Du befindest dich in der %s Sekündigen")
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package eu.mhsl.craftattack.spawn.varo.appliances.metaGameplay.joinProtection;
|
package eu.mhsl.craftattack.spawn.varo.appliances.metaGameplay.joinProtection;
|
||||||
|
|
||||||
import eu.mhsl.craftattack.core.appliance.ApplianceListener;
|
import eu.mhsl.craftattack.core.appliance.ApplianceListener;
|
||||||
|
import org.bukkit.entity.LivingEntity;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.block.BlockBreakEvent;
|
import org.bukkit.event.block.BlockBreakEvent;
|
||||||
@ -16,8 +17,9 @@ public class JoinProtectionListener extends ApplianceListener<JoinProtection> {
|
|||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void onDamage(EntityDamageEvent event) {
|
public void onDamage(EntityDamageEvent event) {
|
||||||
if(!(event.getEntity() instanceof Player)) return;
|
if(!(event.getEntity() instanceof Player player)) return;
|
||||||
this.getAppliance().cancelEvent((Player) event.getEntity(), event);
|
if(event.getCause().equals(EntityDamageEvent.DamageCause.FALL)) return;
|
||||||
|
this.getAppliance().cancelEvent(player, event);
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
@ -25,10 +27,16 @@ public class JoinProtectionListener extends ApplianceListener<JoinProtection> {
|
|||||||
this.getAppliance().cancelEvent(event.getPlayer(), event);
|
this.getAppliance().cancelEvent(event.getPlayer(), event);
|
||||||
}
|
}
|
||||||
|
|
||||||
// flying is not enabled on the server
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void onMove(PlayerMoveEvent event) {
|
public void onMove(PlayerMoveEvent event) {
|
||||||
if(!event.hasChangedPosition()) return;
|
if(!event.hasChangedPosition()) return;
|
||||||
|
JoinProtection.Options option = this.getAppliance().getOptions(event.getPlayer());
|
||||||
|
if(option == null) return;
|
||||||
|
if(!option.wasOnGround) {
|
||||||
|
option.wasOnGround = ((LivingEntity) event.getPlayer()).isOnGround();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
this.getAppliance().cancelEvent(event.getPlayer(), event);
|
this.getAppliance().cancelEvent(event.getPlayer(), event);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,4 +54,9 @@ public class JoinProtectionListener extends ApplianceListener<JoinProtection> {
|
|||||||
public void onBlockBreak(BlockBreakEvent event) {
|
public void onBlockBreak(BlockBreakEvent event) {
|
||||||
this.getAppliance().cancelEvent(event.getPlayer(), event);
|
this.getAppliance().cancelEvent(event.getPlayer(), event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void onFlightChange(PlayerToggleFlightEvent event) {
|
||||||
|
this.getAppliance().cancelEvent(event.getPlayer(), event);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user