WantaSanchez
Опоздун
- 633
- 17
- 150
Доброго времени суток. Я пытаюсь создать "свой компостер" и как полагается скопипастил ванильный. На моё удивление блок успешно зарегистрирован, ставится в мире, но материал, указаный в Property не пашет и по ПКМ естественно тишина. Ваше мнение господа?
Класс блока:
package ua.wantasanchez.simplecompat.blocks;
import java.util.Random;
import javax.annotation.Nullable;
import it.unimi.dsi.fastutil.objects.Object2FloatMap;
import it.unimi.dsi.fastutil.objects.Object2FloatOpenHashMap;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.entity.item.ItemEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.inventory.ISidedInventoryProvider;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.particles.ParticleTypes;
import net.minecraft.pathfinding.PathType;
import net.minecraft.state.IntegerProperty;
import net.minecraft.state.StateContainer;
import net.minecraft.state.properties.BlockStateProperties;
import net.minecraft.util.Direction;
import net.minecraft.util.Hand;
import net.minecraft.util.IItemProvider;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvents;
import net.minecraft.util.Util;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.math.shapes.IBooleanFunction;
import net.minecraft.util.math.shapes.ISelectionContext;
import net.minecraft.util.math.shapes.VoxelShape;
import net.minecraft.util.math.shapes.VoxelShapes;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.IWorld;
import net.minecraft.world.World;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
public class Fester extends Block implements ISidedInventoryProvider {
public static final IntegerProperty LEVEL = BlockStateProperties.LEVEL_0_8;
public static final Object2FloatMap<IItemProvider> CHANCES = new Object2FloatOpenHashMap<>();
public static final VoxelShape field_220300_c = VoxelShapes.fullCube();
private static final VoxelShape[] field_220301_d = Util.make(new VoxelShape[9], (p_220291_0_) -> {
for(int i = 0; i < 8; ++i) {
p_220291_0_[i] = VoxelShapes.combineAndSimplify(field_220300_c, Block.makeCuboidShape(2.0D, (double)Math.max(2, 1 + i * 2), 2.0D, 14.0D, 16.0D, 14.0D), IBooleanFunction.ONLY_FIRST);
}
p_220291_0_[8] = p_220291_0_[7];
});
public static void init() {
CHANCES.defaultReturnValue(-1.0F);
@SuppressWarnings("unused")
float f = 0.5F;
@SuppressWarnings("unused")
float f2 = 0.85F;
@SuppressWarnings("unused")
float f3 = 1.0F;
registerFesterable(0.5F, Items.CHICKEN);
registerFesterable(0.5F, Items.PUFFERFISH);
registerFesterable(0.5F, Items.TROPICAL_FISH);
registerFesterable(0.5F, Items.SPIDER_EYE);
registerFesterable(0.5F, Items.SALMON);
registerFesterable(0.5F, Items.COD);
registerFesterable(0.85F, Items.MUTTON);
registerFesterable(0.85F, Items.RABBIT);
registerFesterable(1.0F, Items.PORKCHOP);
registerFesterable(1.0F, Items.BEEF);
}
private static void registerFesterable(float chance, IItemProvider itemIn) {
CHANCES.put(itemIn.asItem(), chance);
}
public Fester(Block.Properties p_i49986_1_) {
super(p_i49986_1_);
this.setDefaultState(this.stateContainer.getBaseState().with(LEVEL, Integer.valueOf(0)));
}
@OnlyIn(Dist.CLIENT)
public static void func_220292_a(World p_220292_0_, BlockPos p_220292_1_, boolean p_220292_2_) {
BlockState blockstate = p_220292_0_.getBlockState(p_220292_1_);
p_220292_0_.playSound((double)p_220292_1_.getX(), (double)p_220292_1_.getY(), (double)p_220292_1_.getZ(), p_220292_2_ ? SoundEvents.ENTITY_SLIME_ATTACK : SoundEvents.BLOCK_SLIME_BLOCK_PLACE, SoundCategory.BLOCKS, 1.0F, 1.0F, false);
double d0 = blockstate.getShape(p_220292_0_, p_220292_1_).max(Direction.Axis.Y, 0.5D, 0.5D) + 0.03125D;
@SuppressWarnings("unused")
double d1 = (double)0.13125F;
@SuppressWarnings("unused")
double d2 = (double)0.7375F;
Random random = p_220292_0_.getRandom();
for(int i = 0; i < 10; ++i) {
double d3 = random.nextGaussian() * 0.02D;
double d4 = random.nextGaussian() * 0.02D;
double d5 = random.nextGaussian() * 0.02D;
p_220292_0_.addParticle(ParticleTypes.UNDERWATER, (double)p_220292_1_.getX() + (double)0.13125F + (double)0.7375F * (double)random.nextFloat(), (double)p_220292_1_.getY() + d0 + (double)random.nextFloat() * (1.0D - d0), (double)p_220292_1_.getZ() + (double)0.13125F + (double)0.7375F * (double)random.nextFloat(), d3, d4, d5);
}
}
public VoxelShape getShape(BlockState state, IBlockReader worldIn, BlockPos pos, ISelectionContext context) {
return field_220301_d[state.get(LEVEL)];
}
public VoxelShape getRaytraceShape(BlockState state, IBlockReader worldIn, BlockPos pos) {
return field_220300_c;
}
public VoxelShape getCollisionShape(BlockState state, IBlockReader worldIn, BlockPos pos, ISelectionContext context) {
return field_220301_d[0];
}
public void onBlockAdded(BlockState state, World worldIn, BlockPos pos, BlockState oldState, boolean isMoving) {
if (state.get(LEVEL) == 7) {
worldIn.getPendingBlockTicks().scheduleTick(pos, state.getBlock(), 20);
}
}
public boolean onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult hit) {
int i = state.get(LEVEL);
ItemStack itemstack = player.getHeldItem(handIn);
if (i < 8 && CHANCES.containsKey(itemstack.getItem())) {
if (i < 7 && !worldIn.isRemote) {
boolean flag = addItem(state, worldIn, pos, itemstack);
worldIn.playEvent(1500, pos, flag ? 1 : 0);
if (!player.abilities.isCreativeMode) {
itemstack.shrink(1);
}
}
return true;
} else if (i == 8) {
if (!worldIn.isRemote) {
@SuppressWarnings("unused")
float f = 0.7F;
double d0 = (double)(worldIn.rand.nextFloat() * 0.7F) + (double)0.15F;
double d1 = (double)(worldIn.rand.nextFloat() * 0.7F) + (double)0.060000002F + 0.6D;
double d2 = (double)(worldIn.rand.nextFloat() * 0.7F) + (double)0.15F;
ItemEntity itementity = new ItemEntity(worldIn, (double)pos.getX() + d0, (double)pos.getY() + d1, (double)pos.getZ() + d2, new ItemStack(Items.ROTTEN_FLESH));
itementity.setDefaultPickupDelay();
worldIn.addEntity(itementity);
}
clear(state, worldIn, pos);
worldIn.playSound((PlayerEntity)null, pos, SoundEvents.ENTITY_SLIME_HURT, SoundCategory.BLOCKS, 1.0F, 1.0F);
return true;
} else {
return false;
}
}
private static void clear(BlockState p_220294_0_, IWorld p_220294_1_, BlockPos p_220294_2_) {
p_220294_1_.setBlockState(p_220294_2_, p_220294_0_.with(LEVEL, Integer.valueOf(0)), 3);
}
private static boolean addItem(BlockState p_220293_0_, IWorld p_220293_1_, BlockPos p_220293_2_, ItemStack p_220293_3_) {
int i = p_220293_0_.get(LEVEL);
float f = CHANCES.getFloat(p_220293_3_.getItem());
if ((i != 0 || !(f > 0.0F)) && !(p_220293_1_.getRandom().nextDouble() < (double)f)) {
return false;
} else {
int j = i + 1;
p_220293_1_.setBlockState(p_220293_2_, p_220293_0_.with(LEVEL, Integer.valueOf(j)), 3);
if (j == 7) {
p_220293_1_.getPendingBlockTicks().scheduleTick(p_220293_2_, p_220293_0_.getBlock(), 20);
}
return true;
}
}
@SuppressWarnings("deprecation")
public void tick(BlockState state, World worldIn, BlockPos pos, Random random) {
if (state.get(LEVEL) == 7) {
worldIn.setBlockState(pos, state.cycle(LEVEL), 3);
worldIn.playSound((PlayerEntity)null, pos, SoundEvents.BLOCK_SLIME_BLOCK_HIT, SoundCategory.BLOCKS, 1.0F, 1.0F);
}
super.tick(state, worldIn, pos, random);
}
public boolean hasComparatorInputOverride(BlockState state) {
return true;
}
public int getComparatorInputOverride(BlockState blockState, World worldIn, BlockPos pos) {
return blockState.get(LEVEL);
}
protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
builder.add(LEVEL);
}
public boolean allowsMovement(BlockState state, IBlockReader worldIn, BlockPos pos, PathType type) {
return false;
}
@Override
public ISidedInventory createInventory(BlockState p_219966_1_, IWorld p_219966_2_, BlockPos p_219966_3_) {
int i = p_219966_1_.get(LEVEL);
if (i == 8) {
return new Fester.FullInventory(p_219966_1_, p_219966_2_, p_219966_3_, new ItemStack(Items.ROTTEN_FLESH));
} else {
return (ISidedInventory)(i < 7 ? new Fester.PartialInventory(p_219966_1_, p_219966_2_, p_219966_3_) : new Fester.EmptyInventory());
}
}
static class EmptyInventory extends Inventory implements ISidedInventory {
public EmptyInventory() {
super(0);
}
public int[] getSlotsForFace(Direction side) {
return new int[0];
}
public boolean canInsertItem(int index, ItemStack itemStackIn, @Nullable Direction direction) {
return false;
}
public boolean canExtractItem(int index, ItemStack stack, Direction direction) {
return false;
}
}
static class FullInventory extends Inventory implements ISidedInventory {
private final BlockState state;
private final IWorld world;
private final BlockPos pos;
private boolean extracted;
public FullInventory(BlockState p_i50463_1_, IWorld p_i50463_2_, BlockPos p_i50463_3_, ItemStack p_i50463_4_) {
super(p_i50463_4_);
this.state = p_i50463_1_;
this.world = p_i50463_2_;
this.pos = p_i50463_3_;
}
public int getInventoryStackLimit() {
return 1;
}
public int[] getSlotsForFace(Direction side) {
return side == Direction.DOWN ? new int[]{0} : new int[0];
}
public boolean canInsertItem(int index, ItemStack itemStackIn, @Nullable Direction direction) {
return false;
}
public boolean canExtractItem(int index, ItemStack stack, Direction direction) {
return !this.extracted && direction == Direction.DOWN && stack.getItem() == Items.ROTTEN_FLESH;
}
public void markDirty() {
Fester.clear(this.state, this.world, this.pos);
this.extracted = true;
}
}
static class PartialInventory extends Inventory implements ISidedInventory {
private final BlockState state;
private final IWorld world;
private final BlockPos pos;
private boolean inserted;
public PartialInventory(BlockState p_i50464_1_, IWorld p_i50464_2_, BlockPos p_i50464_3_) {
super(1);
this.state = p_i50464_1_;
this.world = p_i50464_2_;
this.pos = p_i50464_3_;
}
public int getInventoryStackLimit() {
return 1;
}
public int[] getSlotsForFace(Direction side) {
return side == Direction.UP ? new int[]{0} : new int[0];
}
public boolean canInsertItem(int index, ItemStack itemStackIn, @Nullable Direction direction) {
return !this.inserted && direction == Direction.UP && Fester.CHANCES.containsKey(itemStackIn.getItem());
}
public boolean canExtractItem(int index, ItemStack stack, Direction direction) {
return false;
}
public void markDirty() {
ItemStack itemstack = this.getStackInSlot(0);
if (!itemstack.isEmpty()) {
this.inserted = true;
Fester.addItem(this.state, this.world, this.pos, itemstack);
this.removeStackFromSlot(0);
}
}
}
}
Регистратор:
package ua.wantasanchez.simplecompat.init;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Food;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.registries.IForgeRegistryEntry;
import net.minecraftforge.registries.ObjectHolder;
import ua.wantasanchez.simplecompat.blocks.Fester;
@ObjectHolder("simplecompat")
@EventBusSubscriber(modid = "simplecompat", bus = EventBusSubscriber.Bus.MOD)
public class ItemsInit {
@ObjectHolder("fester")
public static final Block FESTER = new Fester(Block.Properties.create(Material.WOOD).hardnessAndResistance(2.0F, 10.0F));
@SubscribeEvent
public static void onRegisterBlocks(RegistryEvent.Register<Block> event) {
event.getRegistry().register(setup(new Block(Block.Properties.create(Material.WOOD).hardnessAndResistance(2.0F, 10.0F)), "fester"));
}
@SubscribeEvent
public static void onRegisterItems(RegistryEvent.Register<Item> event) {
event.getRegistry().register(setup(new BlockItem(FESTER, new Item.Properties().group(ItemGroup.DECORATIONS)), FESTER.getRegistryName()));
}
public static <T extends IForgeRegistryEntry<T>> T setup(final T entry, final String name) {
return setup(entry, new ResourceLocation("simplecompat", name));
}
public static <T extends IForgeRegistryEntry<T>> T setup(final T entry, final ResourceLocation registryName) {
entry.setRegistryName(registryName);
return entry;
}
}