Jump to content

Container disappears when created by item.


gmod622

Recommended Posts

Hey all!

 

So I have an item, when on right click on a plank, turns it into a crate.. However, the server doesn't seem to update and will disappear with relog or right click, it also has no textures.

 

Here is the item:

 

package com.lambda.PlentifulMisc.item;
import java.util.List;

import com.lambda.PlentifulMisc.block.ModBlocks;
import com.lambda.PlentifulMisc.main.PlentifulMisc;

import net.minecraft.block.Block;
import net.minecraft.block.BlockDirt;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

public class ItemCrateUpgrade extends ItemBase implements IItemModelProvider {

public ItemCrateUpgrade() {
	super("crateUpgrade");
	registerItemModel(this);
}

@Override 
public void registerItemModel(Item item) {
	PlentifulMisc.proxy.registerItemRenderer(item, 0, "crateUpgrade");
}

@Override
public void addInformation(ItemStack stack, EntityPlayer player, List info, boolean par4)
{
	info.add(com.mojang.realmsclient.gui.ChatFormatting.GREEN + "+ Turns a Plank Into a Crate");
	info.add(com.mojang.realmsclient.gui.ChatFormatting.ITALIC + "Right Click on A Plank to Apply");
}

  public EnumActionResult onItemUse(ItemStack stack, EntityPlayer playerIn, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
    {
        if (!playerIn.canPlayerEdit(pos.offset(facing), facing, stack))
        {
            return EnumActionResult.FAIL;
        }
        else
        {
            int hook = net.minecraftforge.event.ForgeEventFactory.onHoeUse(stack, playerIn, worldIn, pos);
            if (hook != 0) return hook > 0 ? EnumActionResult.SUCCESS : EnumActionResult.FAIL;

            IBlockState iblockstate = worldIn.getBlockState(pos);
            Block block = iblockstate.getBlock();

            if (facing != EnumFacing.DOWN && worldIn.isAirBlock(pos.up()))
            {
                if (block == Blocks.PLANKS)
                {
                    this.setBlock(stack, playerIn, worldIn, pos, ModBlocks.CRATE.getDefaultState());
                    return EnumActionResult.SUCCESS;
                }
            }

            return EnumActionResult.PASS;
        }
    }
  
    protected void setBlock(ItemStack stack, EntityPlayer player, World worldIn, BlockPos pos, IBlockState state)
    {
        worldIn.playSound(player, pos, SoundEvents.BLOCK_ANVIL_FALL, SoundCategory.BLOCKS, 1.0F, 1.0F);

        if (!worldIn.isRemote)
        {
            worldIn.setBlockState(pos, state, 11);
            stack.stackSize --;
        }
    }

}

 

So how would I notify the server of this container?

Not new to java >> New to modding.

Link to comment
Share on other sites

Hey all!

 

So I have an item, when on right click on a plank, turns it into a crate.. However, the server doesn't seem to update and will disappear with relog or right click, it also has no textures.

 

Here is the item:

 

package com.lambda.PlentifulMisc.item;
import java.util.List;

import com.lambda.PlentifulMisc.block.ModBlocks;
import com.lambda.PlentifulMisc.main.PlentifulMisc;

import net.minecraft.block.Block;
import net.minecraft.block.BlockDirt;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

public class ItemCrateUpgrade extends ItemBase implements IItemModelProvider {

public ItemCrateUpgrade() {
	super("crateUpgrade");
	registerItemModel(this);
}

@Override 
public void registerItemModel(Item item) {
	PlentifulMisc.proxy.registerItemRenderer(item, 0, "crateUpgrade");
}

@Override
public void addInformation(ItemStack stack, EntityPlayer player, List info, boolean par4)
{
	info.add(com.mojang.realmsclient.gui.ChatFormatting.GREEN + "+ Turns a Plank Into a Crate");
	info.add(com.mojang.realmsclient.gui.ChatFormatting.ITALIC + "Right Click on A Plank to Apply");
}

  public EnumActionResult onItemUse(ItemStack stack, EntityPlayer playerIn, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
    {
        if (!playerIn.canPlayerEdit(pos.offset(facing), facing, stack))
        {
            return EnumActionResult.FAIL;
        }
        else
        {
            int hook = net.minecraftforge.event.ForgeEventFactory.onHoeUse(stack, playerIn, worldIn, pos);
            if (hook != 0) return hook > 0 ? EnumActionResult.SUCCESS : EnumActionResult.FAIL;

            IBlockState iblockstate = worldIn.getBlockState(pos);
            Block block = iblockstate.getBlock();

            if (facing != EnumFacing.DOWN && worldIn.isAirBlock(pos.up()))
            {
                if (block == Blocks.PLANKS)
                {
                    this.setBlock(stack, playerIn, worldIn, pos, ModBlocks.CRATE.getDefaultState());
                    return EnumActionResult.SUCCESS;
                }
            }

            return EnumActionResult.PASS;
        }
    }
  
    protected void setBlock(ItemStack stack, EntityPlayer player, World worldIn, BlockPos pos, IBlockState state)
    {
        worldIn.playSound(player, pos, SoundEvents.BLOCK_ANVIL_FALL, SoundCategory.BLOCKS, 1.0F, 1.0F);

        if (!worldIn.isRemote)
        {
            worldIn.setBlockState(pos, state, 11);
            stack.stackSize --;
        }
    }

}

 

So how would I notify the server of this container?

This is probably why. Why are you doing this?

int hook = net.minecraftforge.event.ForgeEventFactory.onHoeUse(stack, playerIn, worldIn, pos);
if (hook != 0) return hook > 0 ? EnumActionResult.SUCCESS : EnumActionResult.FAIL;

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

Yeah here it is:

 

package com.lambda.PlentifulMisc.block;
import com.lambda.PlentifulMisc.block.tileentity.TileEntityCrate;
import com.lambda.PlentifulMisc.client.gui.CrateGuiHandler;
import com.lambda.PlentifulMisc.main.PlentifulMisc;

import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.util.EnumBlockRenderType;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;


//Note that in 1.10.*, extending BlockContainer can cause rendering problems if you don't extend getRenderType()
// If you don't want to extend BlockContainer, make sure to add the tile entity manually,
//   using hasTileEntity() and createTileEntity().  See BlockContainer for a couple of other important methods you may
//  need to implement.

public class BlockCrate extends BlockContainer
{   
public BlockCrate()
{
	super(Material.ROCK);
	this.setCreativeTab(PlentifulMisc.instance.creativeTab);     // the block will appear on the Blocks tab.
}

  /**
   * Create the Tile Entity for this block.
   * If your block doesn't extend BlockContainer, use createTileEntity(World worldIn, IBlockState state) instead
   * @param worldIn
   * @param meta
   * @return
   */

  @Override
  public TileEntity createNewTileEntity(World worldIn, int meta) {
    return new TileEntityCrate();
  }

  // not needed if your block implements ITileEntityProvider (in this case implemented by BlockContainer), but it
  //  doesn't hurt to include it anyway...
@Override
public boolean hasTileEntity(IBlockState state)
{
	return true;
}

// Called when the block is right clicked
// In this block it is used to open the blocks gui when right clicked by a player
@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ) {
	// Uses the gui handler registered to your mod to open the gui for the given gui id
	// open on the server side only  (not sure why you shouldn't open client side too... vanilla doesn't, so we better not either)
	if (worldIn.isRemote) return true;

	playerIn.openGui(PlentifulMisc.instance, CrateGuiHandler.getGuiID(), worldIn, pos.getX(), pos.getY(), pos.getZ());
	return true;
}

// This is where you can do something when the block is broken. In this case drop the inventory's contents
@Override
public void breakBlock(World worldIn, BlockPos pos, IBlockState state) {

	IInventory inventory = worldIn.getTileEntity(pos) instanceof IInventory ? (IInventory)worldIn.getTileEntity(pos) : null;

	if (inventory != null){
		// For each slot in the inventory
		for (int i = 0; i < inventory.getSizeInventory(); i++){
			// If the slot is not empty
			if (inventory.getStackInSlot(i) != null)
			{
				// Create a new entity item with the item stack in the slot
				EntityItem item = new EntityItem(worldIn, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, inventory.getStackInSlot(i));

				// Apply some random motion to the item
				float multiplier = 0.1f;
				float motionX = worldIn.rand.nextFloat() - 0.5f;
				float motionY = worldIn.rand.nextFloat() - 0.5f;
				float motionZ = worldIn.rand.nextFloat() - 0.5f;

				item.motionX = motionX * multiplier;
				item.motionY = motionY * multiplier;
				item.motionZ = motionZ * multiplier;

				// Spawn the item in the world
				worldIn.spawnEntityInWorld(item);
			}
		}

		// Clear the inventory so nothing else (such as another mod) can do anything with the items
		inventory.clear();
	}

	// Super MUST be called last because it removes the tile entity
	super.breakBlock(worldIn, pos, state);
}

//---------------------------------------------------------


// the block will render in the SOLID layer.  See http://greyminecraftcoder.blogspot.co.at/2014/12/block-rendering-18.html for more information.
@SideOnly(Side.CLIENT)
public BlockRenderLayer getBlockLayer()
{
	return BlockRenderLayer.SOLID;
}

  // render using a BakedModel (mbe30_inventory_basic.json --> mbe30_inventory_basic_model.json)
  // required because the default (super method) is INVISIBLE for BlockContainers.
  @Override
  public EnumBlockRenderType getRenderType(IBlockState iBlockState) {
    return EnumBlockRenderType.MODEL;
  }


}

Not new to java >> New to modding.

Link to comment
Share on other sites

Okay, so same problem still exists, no texture, can access but disappears on relog.

Try changing the number at the end of the line to 1.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites


package com.lambda.PlentifulMisc.item;

import com.lambda.PlentifulMisc.block.ModBlocks;
import com.lambda.PlentifulMisc.main.PlentifulMisc;

import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemSeedFood;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.registry.GameRegistry;

public class ModItems {

public static ItemCheese cheese;
public static Item ROCKCRESS;
//Craftables
public static ItemAtPAP2 AtPAP2;
public static ItemWaxHoe waxHoe;
public static ItemGaliumT treatedGal;
public static ItemFuelEfficiencyUpgrade fuelEffUpgrade;
public static ItemSpeedUpgrade speedUpgrade;
//Crafties
public static ItemWax wax;
public static ItemVial vial;
public static ItemCrateUpgrade crateUpgrade;


public static void load(FMLPreInitializationEvent event) {
	cheese = new ItemCheese();
	ROCKCRESS = register(new ItemSeedFood(1, 0.3F, ModBlocks.CROP_ROCKCRESS, Blocks.FARMLAND).setUnlocalizedName("rockcress").setRegistryName("rockcress").setCreativeTab(PlentifulMisc.instance.creativeTab));
	AtPAP2 = new ItemAtPAP2();
	waxHoe = new ItemWaxHoe();
	wax = new ItemWax();
	vial = new ItemVial();
	treatedGal = new ItemGaliumT();
	fuelEffUpgrade = new ItemFuelEfficiencyUpgrade();
	speedUpgrade = new ItemSpeedUpgrade();
	crateUpgrade = new ItemCrateUpgrade();

	ModelLoader.setCustomModelResourceLocation(ROCKCRESS, 0, new ModelResourceLocation(ROCKCRESS.getRegistryName(), "inventory"));
}

private static <T extends Item> T register(T item) {
	GameRegistry.register(item);

	if (item instanceof IItemModelProvider ) {
		((IItemModelProvider )item).registerItemModel(item);
	}

	return item;
}
}

 

Registration, I have no itemblock for my blocks.

Not new to java >> New to modding.

Link to comment
Share on other sites


package com.lambda.PlentifulMisc.item;

import com.lambda.PlentifulMisc.block.ModBlocks;
import com.lambda.PlentifulMisc.main.PlentifulMisc;

import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemSeedFood;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.registry.GameRegistry;

public class ModItems {

public static ItemCheese cheese;
public static Item ROCKCRESS;
//Craftables
public static ItemAtPAP2 AtPAP2;
public static ItemWaxHoe waxHoe;
public static ItemGaliumT treatedGal;
public static ItemFuelEfficiencyUpgrade fuelEffUpgrade;
public static ItemSpeedUpgrade speedUpgrade;
//Crafties
public static ItemWax wax;
public static ItemVial vial;
public static ItemCrateUpgrade crateUpgrade;


public static void load(FMLPreInitializationEvent event) {
	cheese = new ItemCheese();
	ROCKCRESS = register(new ItemSeedFood(1, 0.3F, ModBlocks.CROP_ROCKCRESS, Blocks.FARMLAND).setUnlocalizedName("rockcress").setRegistryName("rockcress").setCreativeTab(PlentifulMisc.instance.creativeTab));
	AtPAP2 = new ItemAtPAP2();
	waxHoe = new ItemWaxHoe();
	wax = new ItemWax();
	vial = new ItemVial();
	treatedGal = new ItemGaliumT();
	fuelEffUpgrade = new ItemFuelEfficiencyUpgrade();
	speedUpgrade = new ItemSpeedUpgrade();
	crateUpgrade = new ItemCrateUpgrade();

	ModelLoader.setCustomModelResourceLocation(ROCKCRESS, 0, new ModelResourceLocation(ROCKCRESS.getRegistryName(), "inventory"));
}

private static <T extends Item> T register(T item) {
	GameRegistry.register(item);

	if (item instanceof IItemModelProvider ) {
		((IItemModelProvider )item).registerItemModel(item);
	}

	return item;
}
}

 

Registration, I have no itemblock for my blocks.

I only see one register call. And that is for your crate.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

//Note that in 1.10.*, extending BlockContainer can cause rendering problems if you don't extend getRenderType()
// If you don't want to extend BlockContainer, make sure to add the tile entity manually,
//   using hasTileEntity() and createTileEntity().  See BlockContainer for a couple of other important methods you may
//  need to implement.

public class BlockCrate extends BlockContainer

 

The note there is basically saying "BlockContainer does a bunch of bad things, either don't use it, or override the things it changes to change them back."  You should just make your Crate extend Block.

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



×
×
  • Create New...

Important Information

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