目前,我正在制作一个模组,为游戏添加额外的蜜蜂物品。现在我想做一个可以抛光闪长岩、安山岩、花岗岩等东西的物品。作为工具。
但是当我右键单击它时,我似乎无法替换它。我看了很多帖子,没有发现什么好东西。所有答案都已过时。
到目前为止,我尝试使用
Block replaceBlock = Blocks.POLISHED_DIORITE;
和 world.setBlock。但它什么作用也没有。这是我的代码:
package com.technowin.beeutil.item.custom;
import net.minecraft.block.AbstractBlock;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUseContext;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import java.util.Objects;
public class Waxer extends Item {
public Waxer(Properties properties) {
super(properties);
}
@Override
public ActionResultType onItemUseFirst(ItemStack stack, ItemUseContext context) {
World world = context.getWorld();
if(!world.isRemote) {
PlayerEntity playerEntity = Objects.requireNonNull(context.getPlayer());
BlockState clickedBlock = world.getBlockState(context.getPos());
rightClickOnCertinBlockState(clickedBlock, context, playerEntity);
stack.damageItem(1, playerEntity, player -> player.sendBreakAnimation(context.getHand()));
}
return super.onItemUseFirst(stack, context);
}
private void rightClickOnCertinBlockState(BlockState clickedBlock,
ItemUseContext context, PlayerEntity playerEntity) {
if(blockIsValidForWaxDio(clickedBlock)) {
polishdioriteblock(playerEntity, context.getWorld(), context.getPos());
}
if(blockIsValidForWaxGra(clickedBlock)) {
polishgraniteblock(playerEntity, context.getWorld(), context.getPos());
}
}
private boolean blockIsValidForWaxDio(BlockState clickedBlock) {
return clickedBlock.getBlock() == Blocks.DIORITE;
}
private boolean blockIsValidForWaxGra(BlockState clickedBlock) {
return clickedBlock.getBlock() == Blocks.GRANITE;
}
private void polishdioriteblock(PlayerEntity playerEntity, World world, BlockPos pos) {
Block replaceBlock = Blocks.POLISHED_DIORITE;
}
private void polishgraniteblock(PlayerEntity playerEntity, World world, BlockPos pos) {
Block replaceBlock = Blocks.POLISHED_DIORITE;
}
}
注意:
我是我的世界模组新手,这是我的第二个模组。而且我不是Java高手。
这里就得用方法了
world.setBlockState(...)
:
@Override
public ActionResultType onItemUseFirst(ItemStack stack, ItemUseContext context) {
if (!context.getWorld().isRemote()) {
World world = context.getWorld();
BlockPos pos = context.getPos();
BlockState state = world.getBlockState(pos);
PlayerEntity playerEntity = Objects.requireNonNull(context.getPlayer());
if (state.getBlock() == Blocks.DIORITE) {
world.setBlockState(pos, Blocks.POLISHED_DIORITE.getDefaultState());
stack.damageItem(1, playerEntity, player -> player.sendBreakAnimation(context.getHand()));
} else if (state.getBlock() == Blocks.GRANITE) {
world.setBlockState(pos, Blocks.POLISHED_GRANITE.getDefaultState());
stack.damageItem(1, playerEntity, player -> player.sendBreakAnimation(context.getHand()));
}
}
return super.onItemUseFirst(stack, context);
}