我正在为Minecraft创建一个mod。最近,我试图制作一个自定义块,我遇到了两个问题。
我的主要问题是该块渲染不正确。我希望块的大小比完整块小。我成功地使用setBlockBounds()
更改了块边界,并且当 使块渲染更小并使用更小的边界时,它会导致其他渲染问题。当我放置块时,下面的地板变得不可见,我可以看透它,要么是在下面的洞穴,在它后面的块,或者如果那里什么都没有那么空洞。如何修复该块不渲染?这是一个截图:
此外,我对这个区块的目标是发出“光环”,让玩家围绕它加速或其他一些药水效果。我有基本的代码来找到块周围的玩家并给他们速度,但我找不到一种方法来激活这个方法每个滴答或每X个刻度,以确保它以可靠的方式为玩家提供盒子速度。正常游戏中已经有一些块可以做到这一点,所以它必须是可能的。我怎么能这样做?
答案 0 :(得分:0)
对于您的第一个问题,您需要覆盖isOpaqueCube
以返回false。您还希望覆盖isFullCube
代码的其他部分,但这对于渲染并不重要。例如:
public class YourBlock {
// ... existing code ...
/**
* Used to determine ambient occlusion and culling when rebuilding chunks for render
*/
@Override
public boolean isOpaqueCube(IBlockState state) {
return false;
}
@Override
public boolean isFullCube(IBlockState state) {
return false;
}
}
Here's some info on rendering that mentions this
关于你的第二个问题,这个问题更复杂。它通常通过tile实体实现,但您也可以使用块更新(速度慢得多)。这方面的好例子是BlockBeacon
和TileEntityBeacon
(用于使用平铺实体)和BlockFrostedIce
(用于块更新)。这里有一些(可能已过时)info on tile entities。
这是一个(未经测试的)获取更新的示例,每个都使用tile实体打勾:
public class YourBlock {
// ... existing code ...
/**
* Returns a new instance of a block's tile entity class. Called on placing the block.
*/
@Override
public TileEntity createNewTileEntity(World worldIn, int meta) {
return new TileEntityYourBlock();
}
}
/**
* Tile entity for your block.
*
* Tile entities normally store data, but they can also receive an update each
* tick, but to do so they must implement ITickable. So, don't forget the
* "implements ITickable".
*/
public class TileEntityYourBlock extends TileEntity implements ITickable {
@Override
public void update() {
// Your code to give potion effects to nearby players would go here
// If you only want to do it every so often, you can check like this:
if (this.worldObj.getTotalWorldTime() % 80 == 0) {
// Only runs every 80 ticks (4 seconds)
}
}
// The following code isn't required to make a tile entity that gets ticked,
// but you'll want it if you want (EG) to be able to set the effect.
/**
* Example potion effect.
* May be null.
*/
private Potion effect;
public void setEffect(Potion potionEffect) {
this.effect = potionEffect;
}
public Potion getEffect() {
return this.effect;
}
@Override
public void readFromNBT(NBTTagCompound compound) {
super.readFromNBT(compound);
int effectID = compound.getInteger("Effect")
this.effect = Potion.getPotionById(effectID);
}
public void writeToNBT(NBTTagCompound compound) {
super.writeToNBT(compound);
int effectID = Potion.getIdFromPotion(this.effect);
compound.setInteger("Effect", effectID);
}
}
// This line needs to go in the main registration.
// The ID can be anything so long as it isn't used by another mod.
GameRegistry.registerTileEntity(TileEntityYourBlock.class, "YourBlock");