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