Items & Sets particles

This commit is contained in:
Roch Blonndiaux 2023-01-26 12:23:51 +01:00
parent 500a43fb1a
commit fc2222f0c8
4 changed files with 49 additions and 35 deletions

View File

@ -429,6 +429,8 @@ public class PlayerData {
@ApiStatus.Internal
public void resetOverridingItemParticles() {
if (overridingItemParticles != null)
overridingItemParticles.cancel();
overridingItemParticles = null;
}

View File

@ -16,6 +16,7 @@ import net.Indyuce.mmoitems.api.player.inventory.EquippedItem;
import net.Indyuce.mmoitems.comp.inventory.model.PlayerInventoryImage;
import net.Indyuce.mmoitems.comp.inventory.model.PlayerMMOInventory;
import net.Indyuce.mmoitems.comp.inventory.model.SlotEquippedItem;
import net.Indyuce.mmoitems.particle.api.ParticleRunnable;
import net.Indyuce.mmoitems.stat.data.*;
import net.Indyuce.mmoitems.stat.type.AttackWeaponStat;
import net.milkbowl.vault.permission.Permission;
@ -95,10 +96,6 @@ public class PlayerInventoryHandler implements Runnable {
this.processNewItemSets(newImage);
}
// Calculate player stats
// this.data.getStats().updateStats();
//this.updateStats(newImage);
// Update stats from external plugins
MMOItems.plugin.getRPG().refreshStats(this.data);
@ -158,10 +155,11 @@ public class PlayerInventoryHandler implements Runnable {
this.image.setAbilities().forEach(uuid -> this.data.getMMOPlayerData().getPassiveSkillMap().removeModifier(uuid));
// Particles
bonuses.getParticles()
.forEach(particleData -> this.data.getItemParticles()
this.image.particles()
.getOrDefault(-99, new ArrayList<>())
.forEach(uuid -> this.data.getItemParticles()
.stream()
.filter(particleRunnable -> particleRunnable.getParticleData().equals(particleData))
.filter(particleRunnable -> particleRunnable.getUniqueId().equals(uuid))
.findFirst()
.ifPresent(particleRunnable -> {
particleRunnable.cancel();
@ -215,7 +213,6 @@ public class PlayerInventoryHandler implements Runnable {
packet.runUpdate();
});
// Permissions
if (MMOItems.plugin.hasPermissions()) {
final Permission perms = MMOItems.plugin.getVault().getPermissions();
@ -232,8 +229,14 @@ public class PlayerInventoryHandler implements Runnable {
}
// Particles
for (ParticleData particle : bonuses.getParticles())
this.data.getItemParticles().add(particle.start(this.data));
List<UUID> particleRunnables = new ArrayList<>();
for (ParticleData particle : bonuses.getParticles()) {
final ParticleRunnable pRunnable = particle.start(this.data);
this.data.getItemParticles().add(pRunnable);
particleRunnables.add(pRunnable.getUniqueId());
}
// -99 is a special value that means the particle is not tied to an item
newImage.particles().put(-99, particleRunnables);
// Potion effects
for (PotionEffect effect : bonuses.getPotionEffects()) {
@ -268,14 +271,16 @@ public class PlayerInventoryHandler implements Runnable {
if (particleData.getType().hasPriority())
this.data.resetOverridingItemParticles();
else {
this.data.getItemParticles()
.stream()
.filter(particleRunnable -> particleRunnable.getParticleData().equals(particleData))
.findFirst()
.ifPresent(particleRunnable -> {
particleRunnable.cancel();
this.data.getItemParticles().remove(particleRunnable);
});
this.image.particles()
.getOrDefault(slot, new ArrayList<>())
.forEach(uuid -> this.data.getItemParticles()
.stream()
.filter(particleRunnable -> particleRunnable.getUniqueId().equals(uuid))
.findFirst()
.ifPresent(particleRunnable -> {
particleRunnable.cancel();
this.data.getItemParticles().remove(particleRunnable);
}));
}
}
@ -362,10 +367,16 @@ public class PlayerInventoryHandler implements Runnable {
if (newItem.hasData(ItemStats.ITEM_PARTICLES)) {
ParticleData particleData = (ParticleData) newItem.getData(ItemStats.ITEM_PARTICLES);
if (particleData.getType().hasPriority()) {
if (this.data.getOverridingItemParticles() == null)
this.data.setOverridingItemParticles(particleData.start(this.data));
} else
this.data.getItemParticles().add(particleData.start(this.data));
if (this.data.getOverridingItemParticles() == null) {
ParticleRunnable particleRunnable = particleData.start(this.data);
newImage.particles().computeIfAbsent(item.getSlotNumber(), k -> new ArrayList<>()).add(particleRunnable.getUniqueId());
this.data.setOverridingItemParticles(particleRunnable);
}
} else {
ParticleRunnable particleRunnable = particleData.start(this.data);
newImage.particles().computeIfAbsent(item.getSlotNumber(), k -> new ArrayList<>()).add(particleRunnable.getUniqueId());
this.data.setOverridingItemParticles(particleRunnable);
}
}
// Permissions

View File

@ -3,10 +3,9 @@ package net.Indyuce.mmoitems.comp.inventory.model;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.item.mmoitem.VolatileMMOItem;
import net.Indyuce.mmoitems.api.player.PlayerData;
import net.Indyuce.mmoitems.stat.type.ItemStat;
import net.Indyuce.mmoitems.util.Pair;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import scala.Int;
import java.util.*;
import java.util.stream.Collectors;
@ -26,7 +25,7 @@ public class PlayerInventoryImage {
private final Map<String, Integer> itemSets;
private final Map<Integer, List<UUID>> itemAbilities;
private final List<UUID> setAbilities;
private final Map<Integer, List<Pair<ItemStat, Double>>> stats;
private final Map<Integer, List<UUID>> particles;
private final long timestamp;
public PlayerInventoryImage(@NotNull PlayerData data) {
@ -38,7 +37,7 @@ public class PlayerInventoryImage {
this.cache = new HashMap<>();
this.itemAbilities = new HashMap<>();
this.setAbilities = new ArrayList<>();
this.stats = new HashMap<>();
this.particles = new HashMap<>();
}
public @NotNull PlayerData data() {
@ -65,8 +64,8 @@ public class PlayerInventoryImage {
return setAbilities;
}
public @NotNull Map<Integer, List<Pair<ItemStat, Double>>> stats() {
return stats;
public @NotNull Map<Integer, List<UUID>> particles() {
return particles;
}
public long timestamp() {
@ -115,13 +114,6 @@ public class PlayerInventoryImage {
// Hashcode
image.equipped.add(i);
image.hashCodes.put(i.getSlotNumber(), isEmpty(i) ? -1 : i.hashCode());
// Stats
if (!isEmpty(i))
image.stats.put(i.getSlotNumber(), new VolatileMMOItem(i.getNBT()).getStats()
.stream()
.map(itemStat -> new Pair<>(itemStat, i.getNBT().getStat(itemStat.getId())))
.collect(Collectors.toList()));
});
return image;
}

View File

@ -3,15 +3,20 @@ package net.Indyuce.mmoitems.particle.api;
import net.Indyuce.mmoitems.api.player.PlayerData;
import net.Indyuce.mmoitems.stat.data.ParticleData;
import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.NotNull;
import java.util.UUID;
public abstract class ParticleRunnable extends BukkitRunnable {
private final UUID uniqueId;
protected final ParticleData particle;
protected final PlayerData player;
public ParticleRunnable(ParticleData particle, PlayerData player) {
this.particle = particle;
this.player = player;
this.uniqueId = UUID.randomUUID();
}
@Override
@ -25,4 +30,8 @@ public abstract class ParticleRunnable extends BukkitRunnable {
public ParticleData getParticleData() {
return particle;
}
public @NotNull UUID getUniqueId() {
return uniqueId;
}
}