added Anti Player Loot despawn
This commit is contained in:
1 parent
9dad34719b
commit
c119913071
2 files changed
+65
No files matched your search
+14
@@ -0,0 +1,14 @@
|
||||
package eu.mhsl.craftattack.spawn.craftattack.appliances.metaGameplay.antiPlayerLootDespawn;
|
||||
|
||||
import eu.mhsl.craftattack.spawn.core.appliance.Appliance;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class AntiPlayerLootDespawn extends Appliance {
|
||||
@Override
|
||||
protected @NotNull List<Listener> listeners() {
|
||||
return List.of(new AntiPlayerLootDespawnListener());
|
||||
}
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package eu.mhsl.craftattack.spawn.craftattack.appliances.metaGameplay.antiPlayerLootDespawn;
|
||||
|
||||
import eu.mhsl.craftattack.spawn.core.appliance.ApplianceListener;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.entity.Item;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.entity.ItemDespawnEvent;
|
||||
import org.bukkit.event.entity.ItemSpawnEvent;
|
||||
import org.bukkit.event.entity.PlayerDeathEvent;
|
||||
import org.bukkit.persistence.PersistentDataContainer;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
public class AntiPlayerLootDespawnListener extends ApplianceListener<AntiPlayerLootDespawn> {
|
||||
private static final int extraCycles = 5;
|
||||
private static final String namespace = AntiPlayerLootDespawnListener.class.getSimpleName().toLowerCase(Locale.ROOT);
|
||||
public static NamespacedKey cyclesKey = new NamespacedKey(namespace, "item_deaths_cycles".toLowerCase());
|
||||
private Location lastDeathLocation;
|
||||
private long lastDeathTick = -1;
|
||||
|
||||
@EventHandler
|
||||
public void onDeath(PlayerDeathEvent event) {
|
||||
this.lastDeathLocation = event.getEntity().getLocation();
|
||||
this.lastDeathTick = event.getPlayer().getWorld().getGameTime();
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onItemSpawn(ItemSpawnEvent event) {
|
||||
Item item = event.getEntity();
|
||||
|
||||
if(this.lastDeathLocation == null) return;
|
||||
if(!item.getWorld().equals(this.lastDeathLocation.getWorld())) return;
|
||||
if (item.getWorld().getGameTime() != this.lastDeathTick) return;
|
||||
if(item.getLocation().distanceSquared(this.lastDeathLocation) > 9) return;
|
||||
|
||||
item.getPersistentDataContainer().set(cyclesKey, PersistentDataType.INTEGER, extraCycles);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onItemDespawn(ItemDespawnEvent event) {
|
||||
PersistentDataContainer data = event.getEntity().getPersistentDataContainer();
|
||||
Integer cycles = data.get(cyclesKey, PersistentDataType.INTEGER);
|
||||
|
||||
if(cycles == null || cycles <= 0) return;
|
||||
|
||||
data.set(cyclesKey, PersistentDataType.INTEGER, cycles - 1);
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user