Skip to content

Instantly share code, notes, and snippets.

@Frontrider
Last active January 1, 2019 19:48
Show Gist options
  • Save Frontrider/4b537c06d4c14245ba3e36934d1b6434 to your computer and use it in GitHub Desktop.
Save Frontrider/4b537c06d4c14245ba3e36934d1b6434 to your computer and use it in GitHub Desktop.
/**
* Allows for any block to be farmland, if it's tagged correctly.
*/
@Mixin(CropBlock.class)
public abstract class CropBlockMixin {
@Inject(method = "Lnet/minecraft/block/CropBlock;canPlantOnTop(Lnet/minecraft/block/BlockState;Lnet/minecraft/world/BlockView;Lnet/minecraft/util/math/BlockPos;)Z",
at = @At("HEAD"))
protected void canPlantOn(BlockState blockState_1, BlockView blockView_1, BlockPos blockPos_1, CallbackInfoReturnable<Boolean> cir) {
if (canPlantOn(blockState_1, blockView_1, blockPos_1))
cir.setReturnValue(true);
}
private static boolean canPlantOn(BlockState blockState_1, BlockView blockView_1, BlockPos blockPos_1) {
if(blockState_1.getBlock() instanceof FarmlandInterface)
return true;
Collection<Identifier> tagsFor = BlockTags.getContainer().getTagsFor(blockState_1.getBlock());
return tagsFor.contains(new Identifier(Arcanum.INSTANCE.getMODID(), "farmland"));
}
/**
* Providing hooks to be able to create custom farmland.
*
* @author Frontrider
*/
@Overwrite
public static float method_9830(Block block_1, BlockView blockView_1, BlockPos blockPos_1) {
float float_1 = 1.0F;
BlockPos blockPos_2 = blockPos_1.down();
for (int int_1 = -1; int_1 <= 1; ++int_1) {
for (int int_2 = -1; int_2 <= 1; ++int_2) {
float float_2 = 0.0F;
BlockState blockState_1 = blockView_1.getBlockState(blockPos_2.add(int_1, 0, int_2));
if (blockState_1.getBlock() == Blocks.FARMLAND) {
float_2 = 1.0F;
if (blockState_1.get(FarmlandBlock.field_11009) > 0) {
float_2 = 3.0F;
}
}
//the added code starts here
else {
if ((blockState_1.getBlock() instanceof FarmlandInterface)) {
float_2 = 1.0F;
if (((FarmlandInterface) blockState_1.getBlock()).getWetness(blockState_1, blockPos_2, blockView_1) > 0) {
float_2 = 3.0F;
}
} else {
if (canPlantOn(blockState_1, blockView_1, blockPos_2))
float_2 = 3.0F;
}
}//the added code ends here.
if (int_1 != 0 || int_2 != 0) {
float_2 /= 4.0F;
}
float_1 += float_2;
}
}
BlockPos blockPos_3 = blockPos_1.north();
BlockPos blockPos_4 = blockPos_1.south();
BlockPos blockPos_5 = blockPos_1.west();
BlockPos blockPos_6 = blockPos_1.east();
boolean boolean_1 = block_1 == blockView_1.getBlockState(blockPos_5).getBlock() || block_1 == blockView_1.getBlockState(blockPos_6).getBlock();
boolean boolean_2 = block_1 == blockView_1.getBlockState(blockPos_3).getBlock() || block_1 == blockView_1.getBlockState(blockPos_4).getBlock();
if (boolean_1 && boolean_2) {
float_1 /= 2.0F;
} else {
boolean boolean_3 = block_1 == blockView_1.getBlockState(blockPos_5.north()).getBlock() || block_1 == blockView_1.getBlockState(blockPos_6.north()).getBlock() || block_1 == blockView_1.getBlockState(blockPos_6.south()).getBlock() || block_1 == blockView_1.getBlockState(blockPos_5.south()).getBlock();
if (boolean_3) {
float_1 /= 2.0F;
}
}
return float_1;
}
}
/**
* Allows blocks NOT extending the farmland to serve as such.
* */
public interface FarmlandInterface {
int getWetness(BlockState blockState, BlockPos pos, BlockView blockView);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment