Jump to content

Recommended Posts

Posted (edited)

 

Can someone point me to where I went wrong?

I'm trying to add a new ability to the player, everything works fine,
but the data is not persisting!
Sorry for my english

 

The CosmoStorage class:

Spoiler

package com.pegasusgamer.saintseiyamod.capabilities.cosmo;

import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagInt;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.Capability.IStorage;

public class CosmoStorage implements IStorage<ICosmo>{

    @Override
    public NBTBase writeNBT(Capability<ICosmo> capability, ICosmo instance, EnumFacing side) {
        
        NBTTagCompound tag = new NBTTagCompound();           
        tag.setDouble("cosmo", instance.getCosmo());  
        tag.setDouble("cosmo_limit", instance.getLimit());
        tag.setDouble("cosmo_regen", instance.getRegen());
        
        System.out.println("Salvando no NBT(Cosmo): "+instance.getCosmo());
        
        return tag;
    }

    @Override
    public void readNBT(Capability<ICosmo> capability, ICosmo instance, EnumFacing side, NBTBase nbt) {
        
        NBTTagCompound tag = (NBTTagCompound) nbt;
        instance.setCosmo(tag.getDouble("cosmo"));
        instance.setLimit(tag.getDouble("cosmo_limit"));
        instance.setRegen(tag.getDouble("cosmo_regen"));
        
        System.out.println("Carregando do NBT(Cosmo): "+instance.getCosmo());
        
    }

}

 

The CosmoProvider class:

Spoiler

package com.pegasusgamer.saintseiyamod.capabilities.cosmo;

import net.minecraft.nbt.NBTBase;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;

public class CosmoProvider implements ICapabilitySerializable<NBTBase> {
    
    @CapabilityInject(ICosmo.class)
    public static Capability<ICosmo> COSMO_CAP = null;

    private final ICosmo instance = COSMO_CAP.getDefaultInstance();
    

    @Override
    public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
        return capability == COSMO_CAP;
    }

    @Override
    public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
        return capability == COSMO_CAP ? COSMO_CAP.<T> cast(this.instance) : null;
    }

    @Override
    public NBTBase serializeNBT() {
        return COSMO_CAP.getStorage().writeNBT(COSMO_CAP, this.instance, null);
    }

    @Override
    public void deserializeNBT(NBTBase nbt) {
        COSMO_CAP.getStorage().readNBT(COSMO_CAP, this.instance, null, nbt);
    }

}

 

The CapabilityHandler class:

Spoiler

package com.pegasusgamer.saintseiyamod.capabilities;

import com.pegasusgamer.saintseiyamod.capabilities.cosmo.CosmoProvider;
import com.pegasusgamer.saintseiyamod.util.Reference;

import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.event.AttachCapabilitiesEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

public class CapabilityHandler {
    
    public static final ResourceLocation COSMO_CAP = new ResourceLocation(Reference.MOD_ID, "cosmo");

    @SubscribeEvent
    public void attachCapability(AttachCapabilitiesEvent<Entity> event) {
        if (!(event.getObject() instanceof EntityPlayer)) return;
        
        event.addCapability(COSMO_CAP, new CosmoProvider());
    }
    
}

 

The CommonProxy class:

Spoiler

package com.pegasusgamer.saintseiyamod.proxy;

import org.lwjgl.input.Keyboard;

import com.pegasusgamer.saintseiyamod.Main;
import com.pegasusgamer.saintseiyamod.capabilities.CapabilityHandler;
import com.pegasusgamer.saintseiyamod.capabilities.cosmo.Cosmo;
import com.pegasusgamer.saintseiyamod.capabilities.cosmo.CosmoStorage;
import com.pegasusgamer.saintseiyamod.capabilities.cosmo.ICosmo;
import com.pegasusgamer.saintseiyamod.events.CosmoEventHandler;
import com.pegasusgamer.saintseiyamod.gui.overlays.CosmoGUI;
import com.pegasusgamer.saintseiyamod.util.Config;
import com.pegasusgamer.saintseiyamod.util.Reference;

import net.minecraft.client.model.ModelBase;
import net.minecraft.client.renderer.entity.Render;
import net.minecraft.client.settings.KeyBinding;
import net.minecraft.entity.Entity;
import net.minecraft.entity.monster.EntityMob;
import net.minecraft.item.Item;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.capabilities.CapabilityManager;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.fml.client.registry.ClientRegistry;

public class CommonProxy {
    
    public void preInit() {        
        //Registro das Capacidades
        CapabilityManager.INSTANCE.register(ICosmo.class, new CosmoStorage(), () -> new Cosmo());
        MinecraftForge.EVENT_BUS.register(new CapabilityHandler());
        
        //Registro dos Eventos    
        MinecraftForge.EVENT_BUS.register(new CosmoEventHandler());    
    }
    
    public void init() {}
    
    public void postInit() {}
    
    public void registerItemRenderer(Item item, int meta, String id) {}
    
}

 

Edited by FelipeMunhoz
Best view
Posted
21 minutes ago, diesieben07 said:

Não vejo você mudando os dados em nenhum lugar.

Sorry

 

package com.pegasusgamer.saintseiyamod.events;

import com.pegasusgamer.saintseiyamod.capabilities.cosmo.CosmoProvider;
import com.pegasusgamer.saintseiyamod.capabilities.cosmo.ICosmo;

import net.minecraft.client.Minecraft;
import net.minecraft.entity.item.EntityXPOrb;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.event.entity.player.PlayerPickupXpEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent.Phase;
import net.minecraftforge.fml.common.gameevent.TickEvent.PlayerTickEvent;

public class CosmoEventHandler {
	
	@SubscribeEvent
	public void onPlayeTick (PlayerTickEvent event) {

        if(event.phase == Phase.END) {
        	
        	EntityPlayer player = event.player;
        	
        	//if (!player.world.isRemote) return;
        	
        	ICosmo cosmo = player.getCapability(CosmoProvider.COSMO_CAP, null);       			    		
        	
    		//Regeneração padrão do cosmo se o mesmo estiver abaixo do limite
    		if(cosmo.getCosmo() < cosmo.getLimit())
    			cosmo.increaseCosmo(cosmo.getRegen()/20);
    		
    		//Define o cosmo atual para o cosmo maximo se o mesmo passar do limite
    		if(cosmo.getCosmo() > cosmo.getLimit())
    			cosmo.setCosmo(cosmo.getLimit());
    		
        }
        
	}

	/**
     * Copy data from dead player to the new player
     */
    @SubscribeEvent
    public void onPlayerClone(PlayerEvent.Clone event) {
    	
        EntityPlayer player = event.getEntityPlayer();
        
        ICosmo cosmo = player.getCapability(CosmoProvider.COSMO_CAP, null);
        ICosmo oldCosmo = event.getOriginal().getCapability(CosmoProvider.COSMO_CAP, null);
        
        //Define o cosmo atual para metade do cosmo anterior a morte. 
		cosmo.setCosmo(oldCosmo.getCosmo()/2);
		//Define o limite de cosmo para o anterior subtraido da quantidade de xp dropada
        cosmo.setLimit(oldCosmo.getLimit()-Math.min((event.getOriginal().experienceLevel*7), 100));
        //Define a regeneração de cosmo para a anterior a morte.
        cosmo.setRegen(oldCosmo.getRegen());
    }
    
    @SubscribeEvent
    public void onPlayerPickupXp(PlayerPickupXpEvent event) {

    	EntityPlayer player = event.getEntityPlayer();
    	ICosmo cosmo = player.getCapability(CosmoProvider.COSMO_CAP, null);
    	
    	//Aumenta o limite de cosmo ao coletar xp na mesma proporção
    	cosmo.increaseLimit(event.getOrb().getXpValue());

    }
    
}

 

Posted
2 minutes ago, diesieben07 said:

Você só deve modificar os dados no servidor.

O que exatamente faz você pensar que os dados não estão persistindo (quais sintomas você está observando)?

 

There is a gui that displays the current cosmo

 

package com.pegasusgamer.saintseiyamod.gui.overlays;

import com.pegasusgamer.saintseiyamod.capabilities.cosmo.CosmoProvider;
import com.pegasusgamer.saintseiyamod.capabilities.cosmo.ICosmo;
import com.pegasusgamer.saintseiyamod.util.Reference;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.eventhandler.EventPriority;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class CosmoGUI {
	
	public static class GUIRenderEventClass {
		
		@SubscribeEvent(priority = EventPriority.NORMAL)
		public void onRenderGui(RenderGameOverlayEvent event) {
			if (!event.isCancelable() && event.getType() == RenderGameOverlayEvent.ElementType.EXPERIENCE) {			
				
				Minecraft mc = Minecraft.getMinecraft();
				mc.renderEngine.bindTexture(new ResourceLocation(Reference.MOD_ID+":textures/gui/cosmo_bar_overlay.png"));
				
				EntityPlayer player = mc.player;
				ICosmo cosmo = player.getCapability(CosmoProvider.COSMO_CAP, null);		
				
				int posX = event.getResolution().getScaledWidth() / 2 + 10;
				int posY = event.getResolution().getScaledHeight() - 45;
				
				//Retorna caso o player esteja no modo criativo
				if(player.isCreative())
					return;
			
				//Desenha a barra de cosmo na tela
				mc.ingameGUI.drawTexturedModalRect(posX, posY, 0, 0, 81, 5);				
				int barWidht = (int)((cosmo.getCosmo()/cosmo.getLimit())*81);				
				mc.ingameGUI.drawTexturedModalRect(posX, posY, 0, 5, barWidht, 5);
				
				//Desenha o texto com quantidade de cosmo atual e maxima
				int stringSize = ((cosmo.getCosmo()+" / "+cosmo.getLimit()).toString().length()*4);
				mc.fontRenderer.drawStringWithShadow((int)cosmo.getCosmo()+" / "+(int)cosmo.getLimit(), (posX+41)-(int)(stringSize/2), posY-8, -13421569);
				
			}
		}
	}
	
}

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.