Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

I'm trying to make an event, that when a vanilla mob is close to a certain block, that mob will be give a count down timer (just like zombies have for conversion) and when that timer reaches zero the mob will be struck by lighting, but the issue is that when one mob's timer reaches zero, all the the timers reach zero too, and that's just because I use an int variable to keep track of the timer.

 

So, I was thinking I could create a new nbtdata thing to keep track of the timer for each mob instead of an int, but I don't know how to do that.

 

if anyone could help me that would be great.

Create a capability to store the timer.

 

Forge itself has several capability examples, look at the usages of

CapabilityManager.register

in your IDE. There's also a test mod here.

 

I have some examples here: API, implementation

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

  • Author

it just crashes and says "Attempted to load a proxy type mymod.proxies.ClientProxy into mymod.Main.TIMER, but the types don't match" the TIMER being my Capability, what does that mean?

  • Author

oh, yeah my apologies. here you go, and i'm aware that my code may not make sense i'm a little uncertain on how Capabilities and nbt writing and reading works

 

Main file:

/*	PROXY INFO */
    	@SidedProxy(clientSide = "mymod.proxies.ClientProxy", serverSide = "mymod.proxies.CommonProxy")
    	
    		public static CommonProxy proxy;

    	@CapabilityInject(mymod.events.Capability.CorTimer.class)
	public static final Capability<mymod.events.Capability.CorTimer> TIMER = null;

 

 

proxy code :

public class ClientProxy extends CommonProxy
{
   @Override
public  void preInit( FMLPreInitializationEvent event ) 
{
  super.preInit(event);
  ModEntities.RegisterEntity();
}

   @Override
public  void init( FMLInitializationEvent event ) 
{

	ModItems.registerRenders();
	ModBlocks.registerRenders();


}
   @Override
public  void postInit( FMLPostInitializationEvent event ) 
{
	super.postInit(event);
}
   
   
}

 

and the Capability code:

package mymod.events;

import java.util.Random;

import mymod.Main;
import mymod.inti.ModBlocks;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.passive.EntitySheep;
import net.minecraft.entity.passive.IAnimals;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTBase.NBTPrimitive;
import net.minecraft.nbt.NBTTagInt;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.common.capabilities.Capability.IStorage;
import net.minecraftforge.event.AttachCapabilitiesEvent;
import net.minecraftforge.event.entity.living.LivingEvent.LivingUpdateEvent;
import net.minecraftforge.event.entity.player.PlayerWakeUpEvent;
import net.minecraftforge.event.entity.player.SleepingLocationCheckEvent;
import net.minecraftforge.fml.common.eventhandler.Event.Result;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

public class Capability
{
@SubscribeEvent
    public void onEntityConstruct(AttachCapabilitiesEvent.Entity evt)
    {
        evt.addCapability(new ResourceLocation(Main.MODID, "IExtraSleeping"), new ICapabilitySerializable<NBTPrimitive>()
        {
        	CorTimer inst = Main.TIMER.getDefaultInstance();
            @Override
            public boolean hasCapability(net.minecraftforge.common.capabilities.Capability<?> capability, EnumFacing facing) {
                return capability == Main.TIMER;
            }

            @Override
            public <T> T getCapability(net.minecraftforge.common.capabilities.Capability<T> capability, EnumFacing facing) {
                return capability == Main.TIMER ? Main.TIMER.<T>cast(inst): null;
            }

            @Override
            public NBTPrimitive serializeNBT() {
            	return (NBTPrimitive)Main.TIMER.getStorage().writeNBT(Main.TIMER, inst, null);
            }

            @Override
            public void deserializeNBT(NBTPrimitive nbt) {
            	Main.TIMER.getStorage().readNBT(Main.TIMER, inst, null, nbt);
            }


        });
    }
public interface CorTimer {
    int isTimer();
    void setTimer(int value);
    }

public static class Storage implements IStorage<CorTimer> {

	@Override
	public void readNBT(net.minecraftforge.common.capabilities.Capability<CorTimer> arg0, CorTimer arg1,
			EnumFacing arg2, NBTBase arg3) {
		// TODO Auto-generated method stub
		arg1.setTimer(10);
	}

	@Override
	public NBTBase writeNBT(net.minecraftforge.common.capabilities.Capability<CorTimer> arg0, CorTimer arg1,
			EnumFacing arg2) {
		// TODO Auto-generated method stub
		return new NBTTagInt((arg1.isTimer()));
	}

}

public static class DefaultImpl implements CorTimer
    {
        private int isSleeping = 0;
        @Override public int isTimer() { return isSleeping; }
	@Override public void setTimer(int value){this.isSleeping = value;}
    }

@SubscribeEvent
public void onLivingUpdate(LivingUpdateEvent e)
{
	if(e.getEntity() instanceof IAnimals)
	{
		EntityLiving entity = (EntityLiving)e.getEntity();
		if(entity instanceof EntitySheep)
		{
			 final CorTimer sleep = entity.getCapability(Main.TIMER, null);
			 if (sleep != null)
                     sleep.setTimer(100);;
                     System.out.println(": " + sleep);
			/*BlockPos pos = entity.getPosition();
			Random rand = new Random();
			BlockPos pos1 = pos.add(rand.nextInt(2) +1, -1, rand.nextInt(2) +1);
			World world = entity.worldObj;
			 IBlockState iblockstate1 = world.getBlockState(pos1);
			 Block[] list = {ModBlocks.CorrtuptedGrass};
			 for(int i = 0; i < list.length; i++)
			 {
				 if(list[i] == iblockstate1.getBlock())
				{

				}
			 }
			System.out.println(pos + " new pos" + pos1);*/

		}

	}

}



}

 

Why is the capability attached to your mod class, not the entity?

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.