46 lines
1.9 KiB
Java
46 lines
1.9 KiB
Java
package com.thebrokenrail.sorcerycraft.mixin;
|
|
|
|
import com.thebrokenrail.sorcerycraft.gui.CastingTableScreenHandler;
|
|
import com.thebrokenrail.sorcerycraft.packet.UpdateKnownSpellsS2CPacket;
|
|
import com.thebrokenrail.sorcerycraft.spell.util.SpellHelper;
|
|
import com.thebrokenrail.sorcerycraft.spell.util.SpellPlayerEntity;
|
|
import com.thebrokenrail.sorcerycraft.spell.util.SpellServerPlayerEntity;
|
|
import net.fabricmc.api.EnvType;
|
|
import net.fabricmc.api.Environment;
|
|
import net.minecraft.nbt.CompoundTag;
|
|
import net.minecraft.server.network.ServerPlayerEntity;
|
|
import net.minecraft.util.Identifier;
|
|
import org.spongepowered.asm.mixin.Mixin;
|
|
import org.spongepowered.asm.mixin.injection.At;
|
|
import org.spongepowered.asm.mixin.injection.Inject;
|
|
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
|
|
|
import java.util.Map;
|
|
|
|
@SuppressWarnings("unused")
|
|
@Mixin(ServerPlayerEntity.class)
|
|
public abstract class MixinServerPlayerEntity extends MixinPlayerEntity implements SpellServerPlayerEntity {
|
|
@Inject(at = @At("HEAD"), method = "copyFrom")
|
|
public void copyFrom(ServerPlayerEntity oldPlayer, boolean alive, CallbackInfo info) {
|
|
SpellPlayerEntity oldSpellPlayer = (SpellPlayerEntity) oldPlayer;
|
|
SpellPlayerEntity newSpellPlayer = this;
|
|
|
|
newSpellPlayer.setDiscoveredSpells(oldSpellPlayer.getDiscoveredSpells());
|
|
}
|
|
|
|
@Override
|
|
public void setDiscoveredSpells(Map<Identifier, Integer> spells) {
|
|
super.setDiscoveredSpells(spells);
|
|
if (currentScreenHandler instanceof CastingTableScreenHandler) {
|
|
sync();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void sync() {
|
|
CompoundTag tag = new CompoundTag();
|
|
tag.put(SpellHelper.SPELL_TAG, SpellHelper.createSpellsTag(getDiscoveredSpells()));
|
|
//noinspection ConstantConditions
|
|
UpdateKnownSpellsS2CPacket.send((ServerPlayerEntity) (Object) this, tag);
|
|
}
|
|
} |