我在添加" onBlockActivated"时遇到问题。我的MC mod中具有tile实体的块的事件。该事件应该允许您在块中存储特定项目或从块中获取它。我已经设置了其余的代码,但是这一部分让我感到满意。我已经看了很多这方面的教程,但它们都已经过时了。这是代码:
//tile entity code
package com.cayoayt.musiccrafter.tileentity;
import com.cayoayt.musiccrafter.init.ModItems;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
public class TE_MusicPlayer extends TileEntity {
private int musicDiscs = 0;
//private String discName = "";
//adds a disc
public boolean addDisc() {
if (musicDiscs < 1) {
musicDiscs++;
System.out.println("Discs: " + musicDiscs);
return true;
}
return false;
}
//removes a disc
public void removeDisc() {
if (musicDiscs > 0) {
worldObj.spawnEntityInWorld(new EntityItem(worldObj, pos.getX() + 0.5, pos.getY() + 1.0, pos.getZ() + 0.5, new ItemStack(ModItems.exampleitem)));
musicDiscs--;
}
}
//writes to memory
@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
super.writeToNBT(compound);
compound.setInteger("musicDiscs", this.musicDiscs);
return compound;
}
//reads from memory
@Override
public void readFromNBT(NBTTagCompound compound) {
super.readFromNBT(compound);
this.musicDiscs = compound.getInteger("musicDiscs");
}
}
//block code
package com.cayoayt.musiccrafter.blocks;
import java.util.List;
import java.util.Random;
import com.cayoayt.musiccrafter.MusicCrafter;
import com.cayoayt.musiccrafter.Reference;
import com.cayoayt.musiccrafter.init.ModBlocks;
import com.cayoayt.musiccrafter.init.ModItems;
import com.cayoayt.musiccrafter.tileentity.TE_MusicPlayer;
import net.minecraft.block.Block;
import net.minecraft.block.BlockHorizontal;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.material.MapColor;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockRenderLayer;
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;
public class music_player extends BlockHorizontal implements ITileEntityProvider {
public static final AxisAlignedBB COLLISION_BOX = new AxisAlignedBB(4 * 0.0625, 0, 3 * 0.0625, 12 * 0.0625, 4 * 0.0625, 13 * 0.0625);
public static final AxisAlignedBB BOUNDING_BOX = new AxisAlignedBB(3 * 0.0625, 0, 2 * 0.0625, 13 * 0.0625, 5 * 0.0625, 14 * 0.0625);
public music_player() {
//material
super(Material.GROUND);
//names
setRegistryName(Reference.MusicCrafterBlocks.MUSICPLAYER.getRegistryName());
setUnlocalizedName(Reference.MusicCrafterBlocks.MUSICPLAYER.getUnlocalizedName());
//creative tab
setCreativeTab(MusicCrafter.CREATIVE_TAB);
//default state
setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH));
}
//item dropped
public Item getItemDropped() {
return Item.getItemFromBlock(ModBlocks.music_player);
}
//color on map
public MapColor getMapColor() {
return MapColor.BLACK;
}
//determines if full cube or not
@Override
public boolean isFullCube(IBlockState state) {
return false;
}
//defines if opaque
@Override
public boolean isOpaqueCube(IBlockState state) {
return false;
}
//defines that block is cutout of whole block
@Override
public BlockRenderLayer getBlockLayer() {
return BlockRenderLayer.CUTOUT;
}
//outline of box
@Override
public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos) {
return BOUNDING_BOX;
}
//collision detection
@Override
public void addCollisionBoxToList(IBlockState state, World worldIn, BlockPos pos, AxisAlignedBB entityBox, List<AxisAlignedBB> collidingBoxes, Entity entityIn) {
super.addCollisionBoxToList(pos, entityBox, collidingBoxes, COLLISION_BOX);
}
//sets direction
@Override
public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer) {
IBlockState state = super.onBlockPlaced(worldIn, pos, facing, hitX, hitY, hitZ, meta, placer);
return state.withProperty(FACING, placer.getHorizontalFacing());
}
//meta from state
@Override
public int getMetaFromState(IBlockState state) {
return state.getValue(FACING).getHorizontalIndex();
}
//state from meta
@Override
public IBlockState getStateFromMeta(int meta) {
return getDefaultState().withProperty(FACING, EnumFacing.getHorizontal(meta));
}
//creating block state
@Override
protected BlockStateContainer createBlockState() {
return new BlockStateContainer(this, FACING);
}
//AREA OF FOCUS
@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing heldItem, float side, float hitX, float hitY) {
//CODE HERE
}
//END OF AREA OF FOCUS
//makes container
@Override
public TileEntity createNewTileEntity(World worldIn, int meta) {
return new TE_MusicPlayer();
}
}
请在答案部分粘贴我需要的代码并进行解释。 这会有很大的帮助。
答案 0 :(得分:0)
你需要从世界中获取TileEntity,将其转换为TE_MusicPlayer,然后调用该对象上的方法。
14
标准的java实践是用大写字母启动类名并使用CamelCase(即MusicPlayer而不是music_player)。