added RecoveryCompass

This commit is contained in:
2025-10-10 23:39:29 +02:00
parent 7c254707c1
commit 5910847172
5 changed files with 78 additions and 100 deletions

View File

@@ -0,0 +1,32 @@
package eu.mhsl.craftattack.spawn.craftattack.appliances.gameplay.recoveryCompass;
import eu.mhsl.craftattack.spawn.core.appliance.ApplianceListener;
import org.bukkit.Material;
import org.bukkit.event.EventHandler;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.inventory.ItemStack;
import java.util.Objects;
class KeepRecoveryCompassOnDeathListener extends ApplianceListener<RecoveryCompass> {
@EventHandler
public void onDeath(PlayerDeathEvent event) {
ItemStack source = event.getDrops().stream()
.filter(Objects::nonNull)
.filter(it -> it.getType() == Material.RECOVERY_COMPASS)
.findFirst()
.orElse(null);
if (source == null) return;
if (source.getAmount() > 1) {
source.setAmount(source.getAmount() - 1);
} else {
event.getDrops().remove(source);
}
ItemStack kept = source.clone();
kept.setAmount(1);
event.getItemsToKeep().add(kept);
}
}

View File

@@ -0,0 +1,24 @@
package eu.mhsl.craftattack.spawn.craftattack.appliances.gameplay.recoveryCompass;
import eu.mhsl.craftattack.spawn.core.appliance.ApplianceListener;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
class PlayerFirstJoinCompassGift extends ApplianceListener<RecoveryCompass> {
private final NamespacedKey alreadyGiftedKey = new NamespacedKey(this.getClass().getSimpleName().toLowerCase(), "alreadyGifted".toLowerCase());
@EventHandler
public void onJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
PersistentDataContainer container = player.getPersistentDataContainer();
if(container.has(alreadyGiftedKey)) return;
player.getInventory().addItem(ItemStack.of(Material.RECOVERY_COMPASS));
container.set(alreadyGiftedKey, PersistentDataType.BOOLEAN, true);
}
}

View File

@@ -0,0 +1,22 @@
package eu.mhsl.craftattack.spawn.craftattack.appliances.gameplay.recoveryCompass;
import eu.mhsl.craftattack.spawn.core.appliance.Appliance;
import org.bukkit.event.Listener;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public class RecoveryCompass extends Appliance {
public RecoveryCompass() {
System.out.println("ICH LEBE");
}
@Override
protected @NotNull List<Listener> listeners() {
return List.of(
new PlayerFirstJoinCompassGift(),
new KeepRecoveryCompassOnDeathListener()
);
}
}