Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.65 KB | None | 0 0
  1. package tridentflayer.realisticfood.realisticfood;
  2.  
  3. import net.fabricmc.api.EnvType;
  4. import net.fabricmc.api.Environment;
  5. import net.minecraft.block.*;
  6. import net.minecraft.entity.Entity;
  7. import net.minecraft.entity.EntityContext;
  8. import net.minecraft.entity.mob.RavagerEntity;
  9. import net.minecraft.item.ItemConvertible;
  10. import net.minecraft.item.ItemStack;
  11. import net.minecraft.item.Items;
  12. import net.minecraft.server.world.ServerWorld;
  13. import net.minecraft.state.StateManager;
  14. import net.minecraft.state.property.IntProperty;
  15. import net.minecraft.state.property.Properties;
  16. import net.minecraft.state.property.Property;
  17. import net.minecraft.util.math.BlockPos;
  18. import net.minecraft.util.math.MathHelper;
  19. import net.minecraft.util.shape.VoxelShape;
  20. import net.minecraft.world.BlockView;
  21. import net.minecraft.world.GameRules;
  22. import net.minecraft.world.World;
  23. import net.minecraft.world.WorldView;
  24.  
  25. import java.util.Random;
  26.  
  27. public class TallCropBlock extends TallPlantBlock implements Fertilizable
  28. {
  29.     public static final IntProperty AGE;
  30.     private static final VoxelShape[] AGE_TO_SHAPE;
  31.  
  32.     public TallCropBlock(Settings settings) {
  33.         super(settings);
  34.         this.setDefaultState((BlockState)((BlockState)this.getDefaultState()).with(this.getAgeProperty(), 0));
  35.     }
  36.  
  37.     public VoxelShape getOutlineShape(BlockState state, BlockView view, BlockPos pos, EntityContext context) {
  38.         return AGE_TO_SHAPE[(Integer)state.get(this.getAgeProperty())];
  39.     }
  40.  
  41.     protected boolean canPlantOnTop(BlockState floor, BlockView view, BlockPos pos) {
  42.         return floor.getBlock() == Blocks.FARMLAND;
  43.     }
  44.  
  45.     public IntProperty getAgeProperty() {
  46.         return AGE;
  47.     }
  48.  
  49.     public int getMaxAge() {
  50.         return 7;
  51.     }
  52.  
  53.     protected int getAge(BlockState state) {
  54.         return (Integer)state.get(this.getAgeProperty());
  55.     }
  56.  
  57.     public BlockState withAge(int age) {
  58.         return (BlockState)this.getDefaultState().with(this.getAgeProperty(), age);
  59.     }
  60.  
  61.     public boolean isMature(BlockState state) {
  62.         return (Integer)state.get(this.getAgeProperty()) >= this.getMaxAge();
  63.     }
  64.  
  65.     public void scheduledTick(BlockState state, ServerWorld world, BlockPos pos, Random random) {
  66.         super.scheduledTick(state, world, pos, random);
  67.         if (world.getBaseLightLevel(pos, 0) >= 9) {
  68.             int i = this.getAge(state);
  69.             if (i < this.getMaxAge()) {
  70.                 float f = getAvailableMoisture(this, world, pos);
  71.                 if (random.nextInt((int)(25.0F / f) + 1) == 0) {
  72.                     world.setBlockState(pos, this.withAge(i + 1), 2);
  73.                 }
  74.             }
  75.         }
  76.  
  77.     }
  78.  
  79.     public void applyGrowth(World world, BlockPos pos, BlockState state) {
  80.         int i = this.getAge(state) + this.getGrowthAmount(world);
  81.         int j = this.getMaxAge();
  82.         if (i > j) {
  83.             i = j;
  84.         }
  85.  
  86.         world.setBlockState(pos, this.withAge(i), 2);
  87.     }
  88.  
  89.     protected int getGrowthAmount(World world) {
  90.         return MathHelper.nextInt(world.random, 2, 5);
  91.     }
  92.  
  93.     protected static float getAvailableMoisture(Block block, BlockView world, BlockPos pos) {
  94.         float f = 1.0F;
  95.         BlockPos blockPos = pos.down();
  96.  
  97.         for(int i = -1; i <= 1; ++i) {
  98.             for(int j = -1; j <= 1; ++j) {
  99.                 float g = 0.0F;
  100.                 BlockState blockState = world.getBlockState(blockPos.add(i, 0, j));
  101.                 if (blockState.getBlock() == Blocks.FARMLAND) {
  102.                     g = 1.0F;
  103.                     if ((Integer)blockState.get(FarmlandBlock.MOISTURE) > 0) {
  104.                         g = 3.0F;
  105.                     }
  106.                 }
  107.  
  108.                 if (i != 0 || j != 0) {
  109.                     g /= 4.0F;
  110.                 }
  111.  
  112.                 f += g;
  113.             }
  114.         }
  115.  
  116.         BlockPos blockPos2 = pos.north();
  117.         BlockPos blockPos3 = pos.south();
  118.         BlockPos blockPos4 = pos.west();
  119.         BlockPos blockPos5 = pos.east();
  120.         boolean bl = block == world.getBlockState(blockPos4).getBlock() || block == world.getBlockState(blockPos5).getBlock();
  121.         boolean bl2 = block == world.getBlockState(blockPos2).getBlock() || block == world.getBlockState(blockPos3).getBlock();
  122.         if (bl && bl2) {
  123.             f /= 2.0F;
  124.         } else {
  125.             boolean bl3 = block == world.getBlockState(blockPos4.north()).getBlock() || block == world.getBlockState(blockPos5.north()).getBlock() || block == world.getBlockState(blockPos5.south()).getBlock() || block == world.getBlockState(blockPos4.south()).getBlock();
  126.             if (bl3) {
  127.                 f /= 2.0F;
  128.             }
  129.         }
  130.  
  131.         return f;
  132.     }
  133.  
  134.     public boolean canPlaceAt(BlockState state, WorldView world, BlockPos pos) {
  135.         return (world.getBaseLightLevel(pos, 0) >= 8 || world.isSkyVisible(pos)) && super.canPlaceAt(state, world, pos);
  136.     }
  137.  
  138.     public void onEntityCollision(BlockState state, World world, BlockPos pos, Entity entity) {
  139.         if (entity instanceof RavagerEntity && world.getGameRules().getBoolean(GameRules.MOB_GRIEFING)) {
  140.             world.breakBlock(pos, true, entity);
  141.         }
  142.  
  143.         super.onEntityCollision(state, world, pos, entity);
  144.     }
  145.  
  146.     @Environment(EnvType.CLIENT)
  147.     protected ItemConvertible getSeedsItem() {
  148.         return Items.WHEAT_SEEDS;
  149.     }
  150.  
  151.     @Environment(EnvType.CLIENT)
  152.     public ItemStack getPickStack(BlockView world, BlockPos pos, BlockState state) {
  153.         return new ItemStack(this.getSeedsItem());
  154.     }
  155.  
  156.     public boolean isFertilizable(BlockView world, BlockPos pos, BlockState state, boolean isClient) {
  157.         return !this.isMature(state);
  158.     }
  159.  
  160.     public boolean canGrow(World world, Random random, BlockPos pos, BlockState state) {
  161.         return true;
  162.     }
  163.  
  164.     public void grow(ServerWorld world, Random random, BlockPos pos, BlockState state) {
  165.         this.applyGrowth(world, pos, state);
  166.     }
  167.  
  168.     protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
  169.         builder.add(new Property[]{AGE});
  170.     }
  171.  
  172.     static {
  173.         AGE = Properties.AGE_7;
  174.         AGE_TO_SHAPE = new VoxelShape[]{Block.createCuboidShape(0.0D, 0.0D, 0.0D, 16.0D, 2.0D, 16.0D), Block.createCuboidShape(0.0D, 0.0D, 0.0D, 16.0D, 4.0D, 16.0D), Block.createCuboidShape(0.0D, 0.0D, 0.0D, 16.0D, 6.0D, 16.0D), Block.createCuboidShape(0.0D, 0.0D, 0.0D, 16.0D, 8.0D, 16.0D), Block.createCuboidShape(0.0D, 0.0D, 0.0D, 16.0D, 10.0D, 16.0D), Block.createCuboidShape(0.0D, 0.0D, 0.0D, 16.0D, 12.0D, 16.0D), Block.createCuboidShape(0.0D, 0.0D, 0.0D, 16.0D, 14.0D, 16.0D), Block.createCuboidShape(0.0D, 0.0D, 0.0D, 16.0D, 16.0D, 16.0D)};
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement