Jump to content

Recommended Posts

Posted

Hello,

 

I try to open a GUI's custom mob when I right-click on him, but thats crashed directly :


java.lang.NullPointerException: null
	at net.mattbaker.guardscraft.block.containerblock.TestContainer.addOwnSlots(TestContainer.java:52) ~[TestContainer.class:?]
	at net.mattbaker.guardscraft.block.containerblock.TestContainer.<init>(TestContainer.java:24) ~[TestContainer.class:?]
	at net.mattbaker.guardscraft.block.pedestal.ModGuiHandler.getServerGuiElement(ModGuiHandler.java:39) ~[ModGuiHandler.class:?]
	at net.mattbaker.guardscraft.block.pedestal.ModGuiHandler.getClientGuiElement(ModGuiHandler.java:69) ~[ModGuiHandler.class:?]
	at net.minecraftforge.fml.common.network.NetworkRegistry.getLocalGuiContainer(NetworkRegistry.java:276) ~[NetworkRegistry.class:?]
	at net.minecraftforge.fml.common.network.internal.FMLNetworkHandler.openGui(FMLNetworkHandler.java:111) ~[FMLNetworkHandler.class:?]
	at net.minecraft.entity.player.EntityPlayer.openGui(EntityPlayer.java:2809) ~[EntityPlayer.class:?]
	at net.mattbaker.guardscraft.marchand.EntityMarchand.processInteract(EntityMarchand.java:95) ~[EntityMarchand.class:?]
	at net.minecraft.entity.EntityLiving.processInitialInteract(EntityLiving.java:1355) ~[EntityLiving.class:?]
	at net.minecraft.entity.player.EntityPlayer.interactOn(EntityPlayer.java:1299) ~[EntityPlayer.class:?]
	at net.minecraft.client.multiplayer.PlayerControllerMP.interactWithEntity(PlayerControllerMP.java:587) ~[PlayerControllerMP.class:?]
	at net.minecraft.client.Minecraft.rightClickMouse(Minecraft.java:1682) ~[Minecraft.class:?]
	at net.minecraft.client.Minecraft.processKeyBinds(Minecraft.java:2381) ~[Minecraft.class:?]
	at net.minecraft.client.Minecraft.runTickKeyboard(Minecraft.java:2147) ~[Minecraft.class:?]
	at net.minecraft.client.Minecraft.runTick(Minecraft.java:1935) ~[Minecraft.class:?]
	at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1188) ~[Minecraft.class:?]
	at net.minecraft.client.Minecraft.run(Minecraft.java:442) [Minecraft.class:?]
	at net.minecraft.client.main.Main.main(Main.java:118) [Main.class:?]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_201]
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_201]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_201]
	at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_201]
	at net.minecraft.launchwrapper.Launch.launch(Launch.java:135) [launchwrapper-1.12.jar:?]
	at net.minecraft.launchwrapper.Launch.main(Launch.java:28) [launchwrapper-1.12.jar:?]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_201]
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_201]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_201]
	at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_201]
	at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97) [start/:?]
	at GradleStart.main(GradleStart.java:25) [start/:?]
[16:07:18] [Client thread/INFO] [STDOUT]: [net.minecraft.init.Bootstrap:printToSYSOUT:629]: ---- Minecraft Crash Report ----
// Hey, that tickles! Hehehe!

Time: 1/23/19 4:07 PM
Description: Unexpected error

java.lang.NullPointerException: Unexpected error
	at net.mattbaker.guardscraft.block.containerblock.TestContainer.addOwnSlots(TestContainer.java:52)
	at net.mattbaker.guardscraft.block.containerblock.TestContainer.<init>(TestContainer.java:24)
	at net.mattbaker.guardscraft.block.pedestal.ModGuiHandler.getServerGuiElement(ModGuiHandler.java:39)
	at net.mattbaker.guardscraft.block.pedestal.ModGuiHandler.getClientGuiElement(ModGuiHandler.java:69)
	at net.minecraftforge.fml.common.network.NetworkRegistry.getLocalGuiContainer(NetworkRegistry.java:276)
	at net.minecraftforge.fml.common.network.internal.FMLNetworkHandler.openGui(FMLNetworkHandler.java:111)
	at net.minecraft.entity.player.EntityPlayer.openGui(EntityPlayer.java:2809)
	at net.mattbaker.guardscraft.marchand.EntityMarchand.processInteract(EntityMarchand.java:95)
	at net.minecraft.entity.EntityLiving.processInitialInteract(EntityLiving.java:1355)
	at net.minecraft.entity.player.EntityPlayer.interactOn(EntityPlayer.java:1299)
	at net.minecraft.client.multiplayer.PlayerControllerMP.interactWithEntity(PlayerControllerMP.java:587)
	at net.minecraft.client.Minecraft.rightClickMouse(Minecraft.java:1682)
	at net.minecraft.client.Minecraft.processKeyBinds(Minecraft.java:2381)
	at net.minecraft.client.Minecraft.runTickKeyboard(Minecraft.java:2147)
	at net.minecraft.client.Minecraft.runTick(Minecraft.java:1935)
	at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1188)
	at net.minecraft.client.Minecraft.run(Minecraft.java:442)
	at net.minecraft.client.main.Main.main(Main.java:118)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at net.minecraft.launchwrapper.Launch.launch(Launch.java:135)
	at net.minecraft.launchwrapper.Launch.main(Launch.java:28)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97)
	at GradleStart.main(GradleStart.java:25)


A detailed walkthrough of the error, its code path and all known details is as follows:
---------------------------------------------------------------------------------------

-- Head --
Thread: Client thread
Stacktrace:
	at net.mattbaker.guardscraft.block.containerblock.TestContainer.addOwnSlots(TestContainer.java:52)
	at net.mattbaker.guardscraft.block.containerblock.TestContainer.<init>(TestContainer.java:24)
	at net.mattbaker.guardscraft.block.pedestal.ModGuiHandler.getServerGuiElement(ModGuiHandler.java:39)
	at net.mattbaker.guardscraft.block.pedestal.ModGuiHandler.getClientGuiElement(ModGuiHandler.java:69)
	at net.minecraftforge.fml.common.network.NetworkRegistry.getLocalGuiContainer(NetworkRegistry.java:276)
	at net.minecraftforge.fml.common.network.internal.FMLNetworkHandler.openGui(FMLNetworkHandler.java:111)
	at net.minecraft.entity.player.EntityPlayer.openGui(EntityPlayer.java:2809)
	at net.mattbaker.guardscraft.marchand.EntityMarchand.processInteract(EntityMarchand.java:95)
	at net.minecraft.entity.EntityLiving.processInitialInteract(EntityLiving.java:1355)
	at net.minecraft.entity.player.EntityPlayer.interactOn(EntityPlayer.java:1299)
	at net.minecraft.client.multiplayer.PlayerControllerMP.interactWithEntity(PlayerControllerMP.java:587)
	at net.minecraft.client.Minecraft.rightClickMouse(Minecraft.java:1682)
	at net.minecraft.client.Minecraft.processKeyBinds(Minecraft.java:2381)
	at net.minecraft.client.Minecraft.runTickKeyboard(Minecraft.java:2147)

 

The GUI comes from a tutorial to create and open this GUI, but with a block, and this code works.

 

I you put all classes to know if there is a mistake :

 

The Container :

 

package net.mattbaker.guardscraft.block.containerblock;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;

import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.SlotItemHandler;

public class TestContainer extends Container 
{
	private TestContainerTileEntity te;

    public TestContainer(IInventory playerInventory, TestContainerTileEntity te) 
    {
        this.te = te;

        // This container references items out of our own inventory (the 9 slots we hold ourselves)
        // as well as the slots from the player inventory so that the user can transfer items between
        // both inventories. The two calls below make sure that slots are defined for both inventories.
        addOwnSlots();
        addPlayerSlots(playerInventory);
    }

    private void addPlayerSlots(IInventory playerInventory) 
    {
        // Slots for the main inventory
        for(int row = 0; row < 3; ++row) 
        {
            for(int col = 0; col < 9; ++col) 
            {
                int x = 9 + col * 18;
                int y = row * 18 + 70;
                this.addSlotToContainer(new Slot(playerInventory, col + row * 9 + 10, x, y));
            }
        }

        // Slots for the hotbar
        for(int row = 0; row < 9; ++row) 
        {
            int x = 9 + row * 18;
            int y = 58 + 70;
            this.addSlotToContainer(new Slot(playerInventory, row, x, y));
        }
    }

    private void addOwnSlots() 
    {
        IItemHandler itemHandler = this.te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
        int x = 9;
        int y = 6;

        // Add our own slots
        int slotIndex = 0;
        for (int i = 0; i < itemHandler.getSlots(); i++) 
        {
            addSlotToContainer(new SlotItemHandler(itemHandler, slotIndex, x, y));
            slotIndex++;
            x += 18;
        }
    }

    @Override
    public ItemStack transferStackInSlot(EntityPlayer playerIn, int index) 
    {
        ItemStack itemstack = ItemStack.EMPTY;
        Slot slot = this.inventorySlots.get(index);

        if(slot != null && slot.getHasStack()) 
        {
            ItemStack itemstack1 = slot.getStack();
            itemstack = itemstack1.copy();

            if(index < TestContainerTileEntity.SIZE) 
            {
                if(!this.mergeItemStack(itemstack1, TestContainerTileEntity.SIZE, this.inventorySlots.size(), true)) 
                { return ItemStack.EMPTY; }
            } 
            else if (!this.mergeItemStack(itemstack1, 0, TestContainerTileEntity.SIZE, false)) 
            { return ItemStack.EMPTY; }

            if (itemstack1.isEmpty()) 
            { slot.putStack(ItemStack.EMPTY); } 
            else 
            { slot.onSlotChanged(); }
        }

        return itemstack;
    }

    @Override
    public boolean canInteractWith(EntityPlayer playerIn) 
    { return te.canInteractWith(playerIn); }
}

 

The original block which serves of base for my mob and which works :

 

package net.mattbaker.guardscraft.block.containerblock;

import net.mattbaker.guardscraft.GuardsCraft;
import net.mattbaker.guardscraft.block.BlockBase;
import net.mattbaker.guardscraft.block.pedestal.ModGuiHandler;

import net.minecraft.block.Block;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.INpc;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.world.World;

import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class TestContainerBlock extends Block implements ITileEntityProvider//, net.minecraft.entity.monster.IMob
{
	public static final int GUI_ID = 1;
	protected String name;
	
	/*
	public TestContainerBlock(Material material, String unlocalizedName, String registryName) 
	{ this(material, SoundType.STONE, unlocalizedName, registryName); }
	
	//Constructeur (Avec le son en plus).
    public TestContainerBlock(Material material, SoundType sound, String unlocalizedName, String registryName) 
    {
    	//"super()" permet d'appeler un constructeur parent."
    	super(material);
    	//Fonction utilisée pour les fichiers de langues.
        setUnlocalizedName(GuardsCraft.MODID + "." + unlocalizedName);
        //Fonction qui enregistre l'objet auprès de Forge avec un ID unique.
        setRegistryName(registryName);
        //Fonction qui place le bloc dans la table "TUTORIAL_TAB" (En Creative).
        setCreativeTab(GuardsCraft.TUTORIAL_TAB);
        //Fonction qui change le son.
        setSoundType(sound);
    } 
    */
	/*
	public TestContainerBlock() 
	{
        super(Material.ROCK);
        setUnlocalizedName(GuardsCraft.MODID + ".testcontainerblock");
        setRegistryName("testcontainerblock");
    }
	*/
	
	public TestContainerBlock(String name) 
	{
		super(Material.ROCK);
		//super(Material.ROCK, name);
	}
	
	public TestContainerBlock(Material material, String name) 
	{
		super(material);
	
		this.name = name;
	
		setUnlocalizedName(name);
		setRegistryName(name);
	}
	
	public TestContainerBlock(Material material, String name, BlockPos position) 
	{
		super(material);
		this.name = name;
	
		setUnlocalizedName(name);
		setRegistryName(name);
		
	}
	
	public void registerItemModel(Item itemBlock) 
	{ GuardsCraft.proxy.registerItemRenderer(itemBlock, 0, name); }
	
	public Item createItemBlock() 
	{ return new ItemBlock(this).setRegistryName(getRegistryName()); }
	
	@Override
	public TestContainerBlock setCreativeTab(CreativeTabs tab) 
	{
		super.setCreativeTab(tab);
		return this;
	}
    
    @SideOnly(Side.CLIENT)
    public void initModel() 
    { ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(this), 0, new ModelResourceLocation(getRegistryName(), "inventory")); }
    
    @Override
    public TileEntity createNewTileEntity(World worldIn, int meta) 
    { return new TestContainerTileEntity(); }
      
    @Override
    public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) 
    {   	
    	//TestContainerBlock test = new TestContainerBlock();
    	//BlockPos testPos = new BlockPos(playerIn);
    	
    	if(worldIn.isRemote)
    	{	
    		/*
    		System.out.println("Interaction avec le bloc : ");
    		System.out.println("GuardsCraft.instance : " + GuardsCraft.instance);
    		System.out.println("world : " + worldIn);
    		System.out.println("this.getPosition().getX() : " + pos.getX());
    		System.out.println("this.getPosition().getY() : " + pos.getY());
    		System.out.println("this.getPosition().getZ() : " + pos.getZ());
    		
	    	playerIn.sendMessage(new TextComponentString("testContainerBlock : "));
	    	playerIn.sendMessage(new TextComponentString("GuardsCraft.instance : " + GuardsCraft.instance));
	    	*/
	    	/*
	    	playerIn.sendMessage(new TextComponentString("testPos.getX() : " + testPos.getX()));
	    	playerIn.sendMessage(new TextComponentString("testPos.getY() : " + testPos.getY()));
	    	playerIn.sendMessage(new TextComponentString("testPos.getZ() : " + testPos.getZ()));
	    	*/
	    	/*
	    	playerIn.sendMessage(new TextComponentString("world : " + worldIn));
	    	playerIn.sendMessage(new TextComponentString("pos.getX() : " + pos.getX()));
	    	playerIn.sendMessage(new TextComponentString("pos.getY() : " + pos.getY()));
	    	playerIn.sendMessage(new TextComponentString("pos.getZ() : " + pos.getZ()));
	    	*/
	    	//X : "151" Y : "81" Z : "255"
	    	//playerIn.openGui(GuardsCraft.instance, GUI_ID, worldIn, 151, 81, 254);
	    	//playerIn.openGui(GuardsCraft.instance, 1, worldIn, testPos.getX(), testPos.getY(), testPos.getZ());
	    	
	    	playerIn.openGui(GuardsCraft.instance, 1, worldIn, pos.getX(), pos.getY(), pos.getZ());
	    	//playerIn.openGui(GuardsCraft.instance, ModGuiHandler.PEDESTAL, worldIn, pos.getX(), pos.getY(), pos.getZ());
	    	
	    	//Only execute on the server
	    	/*
	        if (worldIn.isRemote) 
	        { return true; }
	               
	        TileEntity te = worldIn.getTileEntity(pos);
	        if(!(te instanceof TestContainerTileEntity)) 
	        { return false; }
	    	*/
	    	
	    	
	    	//return super.onBlockActivated(worldIn, pos, state, playerIn, hand, facing, hitX, hitY, hitZ);
	    	
	    	return true; 
    	}
    	else { return false ;}	
    }   
}

 

The GuiContainer :

 

package net.mattbaker.guardscraft.block.containerblock;

import net.mattbaker.guardscraft.GuardsCraft;

import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.util.ResourceLocation;

public class TestContainerGui extends GuiContainer 
{
	public static final int WIDTH = 180;
    public static final int HEIGHT = 152;

    private static final ResourceLocation background = new ResourceLocation(GuardsCraft.MODID, "textures/gui/testcontainer.png");

    public TestContainerGui(TestContainerTileEntity tileEntity, TestContainer container) 
    {
        super(container);

        xSize = WIDTH;
        ySize = HEIGHT;
    }
    
    private InventoryPlayer playerInv;
	
	public TestContainerGui(Container container, InventoryPlayer playerInv) 
	{
		super(container);
		this.playerInv = playerInv;
	}

    @Override
    protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) 
    {
        mc.getTextureManager().bindTexture(background);
        drawTexturedModalRect(guiLeft, guiTop, 0, 0, xSize, ySize);
    }
}

 

The TileEntity :

 

package net.mattbaker.guardscraft.block.containerblock;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;

import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.ItemStackHandler;

public class TestContainerTileEntity extends TileEntity 
{	
	public static final int SIZE = 9;

    // This item handler will hold our nine inventory slots
    private ItemStackHandler itemStackHandler = new ItemStackHandler(SIZE) 
    {
        @Override
        protected void onContentsChanged(int slot) 
        {
            // We need to tell the tile entity that something has changed so
            // that the chest contents is persisted
            TestContainerTileEntity.this.markDirty();
        }
    };

    @Override
    public void readFromNBT(NBTTagCompound compound) 
    {
        super.readFromNBT(compound);
        if (compound.hasKey("items")) 
        { itemStackHandler.deserializeNBT((NBTTagCompound) compound.getTag("items")); }
    }

    @Override
    public NBTTagCompound writeToNBT(NBTTagCompound compound) 
    {
        super.writeToNBT(compound);
        compound.setTag("items", itemStackHandler.serializeNBT());
        return compound;
    }

    public boolean canInteractWith(EntityPlayer playerIn) 
    {
        // If we are too far away from this tile entity you cannot use it
        return !isInvalid() && playerIn.getDistanceSq(pos.add(0.5D, 0.5D, 0.5D)) <= 64D;
    }

    @Override
    public boolean hasCapability(Capability<?> capability, EnumFacing facing) 
    {
        if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) 
        { return true; }
        
        return super.hasCapability(capability, facing);
    }

    @Override
    public <T> T getCapability(Capability<T> capability, EnumFacing facing) 
    {
        if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) 
        { return CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.cast(itemStackHandler); }
        
        return super.getCapability(capability, facing);
    }
}

 

The IGuiHandler :

 

package net.mattbaker.guardscraft.block.pedestal;

import net.mattbaker.guardscraft.block.containerblock.TestContainer;
import net.mattbaker.guardscraft.block.containerblock.TestContainerGui;
import net.mattbaker.guardscraft.block.containerblock.TestContainerTileEntity;
import net.mattbaker.guardscraft.block.furnace.ContainerAlloyFurnace;
import net.mattbaker.guardscraft.block.furnace.GuiAlloyFurnace;
import net.mattbaker.guardscraft.block.furnace.TileEntityAlloyFurnace;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

import net.minecraftforge.fml.common.network.IGuiHandler;

public class ModGuiHandler implements IGuiHandler 
{
	public static final int PEDESTAL = 0;
	public static final int CONTAINER = 1;
	public static final int ALLOY_FURNACE = 2;
	
	@Override
	public Container getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) 
	{
		switch (ID) 
		{	
			case PEDESTAL :
				return new ContainerPedestal(player.inventory, (TileEntityPedestal)world.getTileEntity(new BlockPos(x, y, z)));
				
			case CONTAINER :
				
				BlockPos pos = new BlockPos(x, y, z);
		        TileEntity te = world.getTileEntity(pos);
		        
		        if(te instanceof TestContainerTileEntity) 
		        { return new TestContainer(player.inventory, (TestContainerTileEntity) te); }
			
			case ALLOY_FURNACE :
				return new ContainerAlloyFurnace(player.inventory, (TileEntityAlloyFurnace) world.getTileEntity(new BlockPos(x, y, z)));
	
			default :
				return null;
		}
	}
	
	@Override
	public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) 
	{
		switch (ID) 
		{
			case PEDESTAL :
				return new GuiPedestal(getServerGuiElement(ID, player, world, x, y, z), player.inventory);
				//return new GuiPedestal(getServerGuiElement(ID, player, world, x, y, z));
				
			case CONTAINER :
				
				BlockPos pos = new BlockPos(x, y, z);
		        TileEntity te = world.getTileEntity(pos);
		        
		        if(te instanceof TestContainerTileEntity) 
		        {
		            TestContainerTileEntity containerTileEntity = (TestContainerTileEntity) te;
		            return new TestContainerGui(containerTileEntity, new TestContainer(player.inventory, containerTileEntity));
		        }	      
			
			case ALLOY_FURNACE :
				return new GuiAlloyFurnace(getServerGuiElement(ID, player, world, x, y, z), player.inventory);
					
			default :
				return null;
		}
	}
}

 

The mob :

 

package net.mattbaker.guardscraft.merchant;

import javax.annotation.Nullable;

//import javax.annotation.Nullable;

import net.mattbaker.guardscraft.GuardsCraft;
import net.mattbaker.guardscraft.block.containerblock.TestContainerTileEntity;
import net.mattbaker.guardscraft.block.pedestal.BlockPedestal;
import net.mattbaker.guardscraft.block.pedestal.ModGuiHandler;
import net.mattbaker.guardscraft.block.pedestal.TileEntityPedestal;
import net.mattbaker.guardscraft.weirdzombie.EntityWeirdZombie;

import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.EntityAIHurtByTarget;
import net.minecraft.entity.ai.EntityAILookIdle;
import net.minecraft.entity.ai.EntityAIMoveTowardsRestriction;
import net.minecraft.entity.ai.EntityAINearestAttackableTarget;
import net.minecraft.entity.ai.EntityAISwimming;
import net.minecraft.entity.ai.EntityAIWatchClosest;
import net.minecraft.entity.monster.*;
import net.minecraft.entity.passive.EntityZombieHorse;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.pathfinding.PathNavigate;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilityProvider;

public class EntityMerchant extends EntityMob implements ITileEntityProvider
{
	public static final int GUI_ID = 1;
    public static final ResourceLocation LOOT = new ResourceLocation(GuardsCraft.MODID, "entities/soldat");

    public EntityMerchant(World worldIn)
    {
        super(worldIn);
        setSize(0.6F, 1.95F);
    }
    
    public EntityMerchant setCreativeTab(CreativeTabs tab) 
	{
		//super.setCreativeTab(tab);
		return this;
	}
	
	public Class<TestContainerTileEntity> getTileEntityClass() 
	{ return TestContainerTileEntity.class; }
	
	@Nullable
	public TestContainerTileEntity createTileEntity(World world, IBlockState state) 
	{ return new TestContainerTileEntity(); }
	
    
	@Override
    public TileEntity createNewTileEntity(World worldIn, int meta) 
    { return new TestContainerTileEntity(); }
	
    @Override
    public boolean processInteract(EntityPlayer player, EnumHand hand) 
    {    	    	
    	if(this.getEntityWorld().isRemote)
    	{
    		   		
	    	player.sendMessage(new TextComponentString("Interaction with the merchant : "));
	    	player.sendMessage(new TextComponentString("GuardsCraft.instance : " + GuardsCraft.instance));
	    	player.sendMessage(new TextComponentString("world : " + world));
	    	player.sendMessage(new TextComponentString("this.getPosition().getX() : " + this.getPosition().getX()));
	    	player.sendMessage(new TextComponentString("this.getPosition().getY() : " + this.getPosition().getY()));
	    	player.sendMessage(new TextComponentString("this.getPosition().getZ() : " + this.getPosition().getZ()));
	    	
	    	player.openGui(GuardsCraft.instance, 1, this.getEntityWorld(), this.getPosition().getX(), this.getPosition().getY(), this.getPosition().getZ());
	    	
	    	//player.openGui(GuardsCraft.instance, 1, this.getEntityWorld(), 0, 0, 0);	 	
	    	//player.openGui(GuardsCraft.instance, GUI_ID, world, this.chunkCoordX, this.chunkCoordY, this.chunkCoordZ);    	
	    	    	
	    	//player.openGui(GuardsCraft.instance, GUI_ID, world, 153, 81, 251);
	    	//return super.processInteract(player, hand);
	    	return true;
    	}
    	else { return false; }  	
    }
       
    @Override
    protected void applyEntityAttributes()
    {
        super.applyEntityAttributes();
        // Here we set various attributes for our mob. Like maximum health, armor, speed, ...
        this.getEntityAttribute(SharedMonsterAttributes.FOLLOW_RANGE).setBaseValue(35.0D);
        this.getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).setBaseValue(0.35D);
        this.getEntityAttribute(SharedMonsterAttributes.ATTACK_DAMAGE).setBaseValue(3.0D);
        this.getEntityAttribute(SharedMonsterAttributes.ARMOR).setBaseValue(2.0D);
    }

    //Fonction qui regroupe les actions possibles du mob.
    @Override
    protected void initEntityAI()
    {
    	//this.getTileEntity(world, pos);
    	//this.getTileEntityClass();
        this.tasks.addTask(0, new EntityAISwimming(this));
        //this.tasks.addTask(2, new EntityAISoldatAttack(this, 1.0D, false));
        //this.tasks.addTask(5, new EntityAIMoveTowardsRestriction(this, 1.0D));
        //this.tasks.addTask(7, new EntityAIWander(this, 1.0D));
        this.tasks.addTask(8, new EntityAIWatchClosest(this, EntityPlayer.class, 8.0F));
        this.tasks.addTask(8, new EntityAILookIdle(this));
        this.applyEntityAI();
    }

    //Fonction qui cible les ennemis.
    private void applyEntityAI() 
    {
    	//this.tasks.addTask(6, new EntityAIMoveThroughVillage(this, 1.0D, false));
        //this.targetTasks.addTask(1, new EntityAIHurtByTarget(this, true, new Class[]{EntityPigZombie.class}));
        //Tue les zombies.
        //this.targetTasks.addTask(2, new EntityAINearestAttackableTarget<EntityZombie>(this, EntityZombie.class, true));
        //Tue les araignées.
        //this.targetTasks.addTask(2, new EntityAINearestAttackableTarget<EntityCaveSpider>(this, EntityCaveSpider.class, true));
        //Tue les chevaux-zombies.
        //this.targetTasks.addTask(2, new EntityAINearestAttackableTarget<EntityZombieHorse>(this, EntityZombieHorse.class, true));
        //Tue les creepers.
        //this.targetTasks.addTask(1, new EntityAINearestAttackableTarget<EntityCreeper>(this, EntityCreeper.class, true));
        //Tue les endermen.
        //this.targetTasks.addTask(2, new EntityAINearestAttackableTarget<EntityEnderman>(this, EntityEnderman.class, true));
        //Tue les squelettes.
        //this.targetTasks.addTask(3, new EntityAINearestAttackableTarget<EntitySkeleton>(this, EntitySkeleton.class, true));
        //Tue les weird zombies.
        //this.targetTasks.addTask(2, new EntityAINearestAttackableTarget<EntityWeirdZombie>(this, EntityWeirdZombie.class, true));       
    }

    @Override
    //@Nullable
    protected ResourceLocation getLootTable() 
    {
        return LOOT;
    }

    @Override
    protected PathNavigate createNavigator(World worldIn) 
    {
    	// TODO Auto-generated method stub
    	return super.createNavigator(worldIn);
    }
}

 

The main class :

 

package net.mattbaker.guardscraft;

import net.mattbaker.guardscraft.events.MyForgeEventHandler;
import net.mattbaker.guardscraft.item.ModItems;
import net.mattbaker.guardscraft.network.PacketRequestUpdatePedestal;
import net.mattbaker.guardscraft.network.PacketUpdatePedestal;
import net.mattbaker.guardscraft.proxy.ClientProxy;
//import net.mattbaker.guardscraft.init.ModItems_autre;
import net.mattbaker.guardscraft.proxy.CommonProxy;
import net.mattbaker.guardscraft.proxy.IProxy;
import net.mattbaker.guardscraft.proxy.ServerProxy;
import net.mattbaker.guardscraft.recipes.TutorialRecipes;
//import net.mattbaker.guardscraft.soldat.EntitySoldat;
import net.mattbaker.guardscraft.tabs.TutorialTab;
import net.mattbaker.guardscraft.util.ModEntities;
import net.mattbaker.guardscraft.world.gen.TutorialWorldGen;
import net.mattbaker.guardscraft.block.containerblock.GuiProxy;
import net.mattbaker.guardscraft.block.furnace.GuiHandler;
import net.mattbaker.guardscraft.block.pedestal.ModGuiHandler;

import net.minecraft.block.Block;
import net.minecraft.creativetab.CreativeTabs;
//import net.minecraft.init.Blocks;
//import net.minecraft.world.World;
import net.minecraft.item.Item;

import net.minecraftforge.client.event.ModelRegistryEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.SidedProxy;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.fml.relauncher.Side;

import org.apache.logging.log4j.core.Logger;

@Mod(modid = GuardsCraft.MODID, name = GuardsCraft.NAME, version = GuardsCraft.VERSION)
public class GuardsCraft 
{
	//Doit être "guardscraft" et non "GuardsCraft".
	public static final String MODID = "guardscraft";
    public static final String NAME = "GuardsCraft";
    public static final String VERSION = "1.0";
    public static final String MC_VERSION = "[1.12.2]";

    public static final String CLIENT = "net.mattbaker.guardscraft.proxy.ClientProxy";
    public static final String SERVER = "net.mattbaker.guardscraft.proxy.ServerProxy";
    
    //Ajoute une instance de la table "TUTORIAL_TAB".
    public static final CreativeTabs TUTORIAL_TAB = new TutorialTab("tabTutorialMod");
        
    @Mod.Instance
    public static GuardsCraft instance;
    
    @SidedProxy(clientSide = GuardsCraft.CLIENT, serverSide = GuardsCraft.SERVER)

    public static CommonProxy proxy;
    
    private static Logger logger;
    
    public static SimpleNetworkWrapper network;
    
    @EventHandler
    public void preInit(FMLPreInitializationEvent event)
    {
        logger = (Logger) event.getModLog(); 
        proxy.preInit(event);
        
        //Appels pour le zombie modifié et le soldat.
        //ModEntities.init();
        //ModEntities.initModels();
        
        //Four
        //NetworkRegistry.INSTANCE.registerGuiHandler(this, new GuiHandler());  
        //Pedestal
        NetworkRegistry.INSTANCE.registerGuiHandler(this, new ModGuiHandler());
        //ContainerBlock
        //NetworkRegistry.INSTANCE.registerGuiHandler(this, new GuiProxy());
        
        //Le dernier "NetworkRegistry.INSTANCE.registerGuiHandler()" gagne.
        

        network = NetworkRegistry.INSTANCE.newSimpleChannel(MODID);
        network.registerMessage(new PacketUpdatePedestal.Handler(), PacketUpdatePedestal.class, 0, Side.CLIENT);
        network.registerMessage(new PacketRequestUpdatePedestal.Handler(), PacketRequestUpdatePedestal.class, 1, Side.SERVER);
		
		proxy.registerRenderers();
    }
    
    @EventHandler
    public void init(FMLInitializationEvent event)
    {
        // some example code
        //logger.info("DIRT BLOCK >> {}", Blocks.DIRT.getRegistryName());
        //Item.ToolMaterial;
        
        //Appel de la fonction de la classe TutorialRecipes pour le four.
        //TutorialRecipes.initSmelting();
        
        //Appel de la fonction pour la génération de "minerai".
        //GameRegistry.registerWorldGenerator(new TutorialWorldGen(), 0);
    	
        proxy.init(event);
    }
    
    @EventHandler
    public void postInit(FMLPostInitializationEvent event) 
    {
    	//MinecraftForge.EVENT_BUS.register(new MyForgeEventHandler());
    	
    	proxy.postInit(event);
    }
    
    @Mod.EventBusSubscriber
	public static class RegistrationHandler 
	{
		@SubscribeEvent
		public static void registerItems(RegistryEvent.Register<Item> event) 
		{
			ModItems.register(event.getRegistry());
		}	
		
		@SubscribeEvent
		public static void registerItems(ModelRegistryEvent event) 
		{
			ModItems.registerModels();
		}
	}
}

 

I have too tried to open a GUI with an item, but this is the same thing : a crash.

 

Tell me if you need more informations, I searched since 3 weeks, this which explains why there are so  codes lines in comments.

 

Regards.

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.