gpt4 book ai didi

java - 我应该如何添加 onBlockActivated 事件来激活我的 MC Mod 中的图 block 实体? (锻造 MC 1.11.2

转载 作者:太空宇宙 更新时间:2023-11-04 11:46:13 27 4
gpt4 key购买 nike

我在为 MC mod 中具有图 block 实体的 block 添加“onBlockActivated”事件时遇到问题。该事件应该允许您将特定项目存储在 block 内或从 block 中取出它。我已经设置了其余的代码,但这一部分让我很困惑。我看过很多这方面的教程,但它们都已经过时了。代码如下:

//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();
}
}

请将我需要的代码粘贴到答案部分并进行解释。这会有很大帮助。

最佳答案

您需要从 World 获取 TileEntity,将其转换为 TE_MusicPlayer,然后调用该对象上的方法。

TileEntity tile = worldIn.getTileEntity(pos);
if(tile instanceof TE_MusicPlayer)
{
TE_MusicPlayer musicplayer = (TE_MusicPlayer)tile;
//musicplayer.add/removeDisk logic here
}

标准的java实践是类名以大写字母开头并使用CamelCase(即MusicPlayer而不是music_player)。

关于java - 我应该如何添加 onBlockActivated 事件来激活我的 MC Mod 中的图 block 实体? (锻造 MC 1.11.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42320958/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com