From 53c0f1aff8b26fe39e18ea57b47498a3cd4c5dc9 Mon Sep 17 00:00:00 2001 From: Sollace Date: Thu, 1 Sep 2022 22:54:34 +0200 Subject: [PATCH] Add corruption stat and fix level and xp not actually being saved --- .../unicopia/ability/magic/Levelled.java | 2 ++ .../entity/player/PlayerLevelStore.java | 31 +++++++++++-------- .../unicopia/entity/player/Pony.java | 26 +++++++++++++++- 3 files changed, 45 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/Levelled.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/Levelled.java index 84ea1495..2af23627 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/Levelled.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/Levelled.java @@ -25,6 +25,8 @@ public interface Levelled { LevelStore getLevel(); + LevelStore getCorruption(); + interface LevelStore { int getMax(); diff --git a/src/main/java/com/minelittlepony/unicopia/entity/player/PlayerLevelStore.java b/src/main/java/com/minelittlepony/unicopia/entity/player/PlayerLevelStore.java index 2be9ee25..9738813d 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/player/PlayerLevelStore.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/player/PlayerLevelStore.java @@ -2,30 +2,35 @@ package com.minelittlepony.unicopia.entity.player; import com.minelittlepony.unicopia.ability.magic.Levelled; -import net.minecraft.entity.data.DataTracker; import net.minecraft.entity.data.TrackedData; -import net.minecraft.entity.data.TrackedDataHandlerRegistry; -import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.sound.SoundCategory; -import net.minecraft.sound.SoundEvents; +import net.minecraft.sound.*; import net.minecraft.util.math.MathHelper; class PlayerLevelStore implements Levelled.LevelStore { - private static final TrackedData LEVEL = DataTracker.registerData(PlayerEntity.class, TrackedDataHandlerRegistry.INTEGER); - private final Pony pony; - PlayerLevelStore(Pony pony) { + private final TrackedData dataEntry; + + private final boolean upgradeMana; + + private final SoundEvent levelUpSound; + + PlayerLevelStore(Pony pony, TrackedData dataEntry, boolean upgradeMana, SoundEvent levelUpSound) { this.pony = pony; - pony.getEntity().getDataTracker().startTracking(LEVEL, 0); + this.dataEntry = dataEntry; + this.upgradeMana = upgradeMana; + this.levelUpSound = levelUpSound; + pony.getEntity().getDataTracker().startTracking(dataEntry, 0); } @Override public void add(int levels) { if (levels > 0) { - pony.getMagicalReserves().getMana().add(pony.getMagicalReserves().getMana().getMax() / 2); - pony.getReferenceWorld().playSound(null, pony.getOrigin(), SoundEvents.ENTITY_PLAYER_LEVELUP, SoundCategory.PLAYERS, 1, 2); + if (upgradeMana) { + pony.getMagicalReserves().getMana().add(pony.getMagicalReserves().getMana().getMax() / 2); + } + pony.getReferenceWorld().playSound(null, pony.getOrigin(), levelUpSound, SoundCategory.PLAYERS, 1, 2); } Levelled.LevelStore.super.add(levels); } @@ -37,11 +42,11 @@ class PlayerLevelStore implements Levelled.LevelStore { @Override public int get() { - return pony.getEntity().getDataTracker().get(LEVEL); + return pony.getEntity().getDataTracker().get(dataEntry); } @Override public void set(int level) { - pony.getEntity().getDataTracker().set(LEVEL, MathHelper.clamp(level, 0, getMax())); + pony.getEntity().getDataTracker().set(dataEntry, MathHelper.clamp(level, 0, getMax())); } } diff --git a/src/main/java/com/minelittlepony/unicopia/entity/player/Pony.java b/src/main/java/com/minelittlepony/unicopia/entity/player/Pony.java index c2892485..07a426dd 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/player/Pony.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/player/Pony.java @@ -60,6 +60,7 @@ import net.minecraft.nbt.NbtCompound; import net.minecraft.network.packet.s2c.play.EntityPassengersSetS2CPacket; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.world.ServerWorld; +import net.minecraft.sound.SoundEvents; import net.minecraft.text.Text; import net.minecraft.util.Hand; import net.minecraft.util.math.BlockPos; @@ -74,6 +75,8 @@ public class Pony extends Living implements Transmittable, Copieab static final TrackedData EXERTION = DataTracker.registerData(PlayerEntity.class, TrackedDataHandlerRegistry.FLOAT); static final TrackedData MANA = DataTracker.registerData(PlayerEntity.class, TrackedDataHandlerRegistry.FLOAT); static final TrackedData XP = DataTracker.registerData(PlayerEntity.class, TrackedDataHandlerRegistry.FLOAT); + static final TrackedData LEVEL = DataTracker.registerData(PlayerEntity.class, TrackedDataHandlerRegistry.INTEGER); + static final TrackedData CORRUPTION = DataTracker.registerData(PlayerEntity.class, TrackedDataHandlerRegistry.INTEGER); private static final TrackedData EFFECT = DataTracker.registerData(PlayerEntity.class, TrackedDataHandlerRegistry.NBT_COMPOUND); @@ -88,6 +91,7 @@ public class Pony extends Living implements Transmittable, Copieab private final ManaContainer mana; private final PlayerLevelStore levels; + private final PlayerLevelStore corruption; private final List tickers; @@ -116,7 +120,8 @@ public class Pony extends Living implements Transmittable, Copieab public Pony(PlayerEntity player) { super(player, EFFECT); this.mana = new ManaContainer(this); - this.levels = new PlayerLevelStore(this); + this.levels = new PlayerLevelStore(this, LEVEL, true, SoundEvents.ENTITY_PLAYER_LEVELUP); + this.corruption = new PlayerLevelStore(this, CORRUPTION, false, SoundEvents.PARTICLE_SOUL_ESCAPE); this.tickers = Lists.newArrayList(gravity, mana, attributes, charms); player.getDataTracker().startTracking(RACE, Race.DEFAULT_ID); @@ -202,6 +207,11 @@ public class Pony extends Living implements Transmittable, Copieab return levels; } + @Override + public LevelStore getCorruption() { + return corruption; + } + @Override public boolean isInvisible() { return invisible && SpellPredicate.IS_DISGUISE.isOn(this); @@ -528,6 +538,9 @@ public class Pony extends Living implements Transmittable, Copieab compound.put("gravity", gravity.toNBT()); compound.put("charms", charms.toNBT()); compound.put("discoveries", discoveries.toNBT()); + compound.putInt("levels", levels.get()); + compound.putInt("corruption", corruption.get()); + compound.putFloat("magicXp", mana.getXp().get()); getSpellSlot().get(true).ifPresent(effect ->{ compound.put("effect", Spell.writeNbt(effect)); @@ -550,6 +563,9 @@ public class Pony extends Living implements Transmittable, Copieab gravity.fromNBT(compound.getCompound("gravity")); charms.fromNBT(compound.getCompound("charms")); discoveries.fromNBT(compound.getCompound("discoveries")); + levels.set(compound.getInt("levels")); + corruption.set(compound.getInt("corruption")); + mana.getXp().set(compound.getFloat("magicXp")); magicExhaustion = compound.getFloat("magicExhaustion"); @@ -577,12 +593,20 @@ public class Pony extends Living implements Transmittable, Copieab getDiscoveries().copyFrom(oldPlayer.getDiscoveries()); getCharms().equipSpell(Hand.MAIN_HAND, oldPlayer.getCharms().getEquippedSpell(Hand.MAIN_HAND)); getCharms().equipSpell(Hand.OFF_HAND, oldPlayer.getCharms().getEquippedSpell(Hand.OFF_HAND)); + corruption.set(oldPlayer.getCorruption().get()); + levels.set(oldPlayer.getLevel().get()); + mana.getXp().set(oldPlayer.getMagicalReserves().getXp().get()); advancementProgress.putAll(oldPlayer.getAdvancementProgress()); setDirty(); } @Override public void onSpellSet(@Nullable Spell spell) { + if (spell != null) { + if (spell.getAffinity() == Affinity.BAD && entity.getWorld().random.nextInt(120) == 0) { + getCorruption().add(1); + } + } setDirty(); }