Jump to content

[1.9.4]adding new nbtdata to vanilla mobs


SuperLuke

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);*/

		}

	}

}



}

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • What MC version? What's the IP? Are any mods needed to be able to join?
    • Thank you for your answer ! Unfortunatly i have the same problem when i use setPos() public static int movingfunction(CommandContext<CommandSourceStack> context){ CommandSourceStack source = context.getSource(); if (!(source.getEntity() instanceof ServerPlayer)) { return 0; } ServerPlayer player = (ServerPlayer ) source.getEntity(); double moveSpeed = 0.5; for (int i =0; i<10000;i++) { LOGGER.info("running for the {} time", i); double x = player.getX() + player.getViewVector(1.0f).x * moveSpeed; double y = player.getY(); double z = player.getZ() + player.getViewVector(1.0f).z * moveSpeed ; Vec3 movementVec = new Vec3(x, y, z); LOGGER.info("x ={} y ={} z ={}", x, y, z); player.setPos( movementVec); } return 1; } With the logs i can see that x and z are increasing but once again my player is not moving. is there a function to use to sync the server and the client ? I also tried to use LocalPlayer instead of ServerPlayer but my code would stop when i got the object. Also i will change a bit the main topic but is there a way to similate key press ? i found KeyBinding.setKeyBindState on others post but it look like there is no more KeyBinding in 1.20   I found this code : KeyMapping.click(Minecraft.getInstance().options.keyUp.getKey()); But it doesn't seems to work   And i found this one : Minecraft.getInstance().options.keyUp.setDown(true); wich works but doesn't exactly do what i want , it doesn't release the key so for exemple i can't make him run. Minecraft.getInstance().options.keyUp.setDown(true); Minecraft.getInstance().options.keyUp.setDown(false); Minecraft.getInstance().options.keyUp.setDown(true); doesn't make him run
    • Add crash-reports with sites like https://paste.ee/ Maybe an issue with blur, essentials or cumulus_menus
    • Add the crash-report or latest.log (logs-folder) with sites like https://paste.ee/ and paste the link to it here  
  • Topics

×
×
  • Create New...

Important Information

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