Skip to content

Commit

Permalink
Corn is now Corn is now Corn
Browse files Browse the repository at this point in the history
- 2 block tall crops (corn) now extend from CropBlock thereby resolving some mod compat issues
- Expanded animal breeding with crops beyond chickens
  • Loading branch information
hugeblank committed May 11, 2021
1 parent c07b0db commit 6f3e227
Show file tree
Hide file tree
Showing 15 changed files with 142 additions and 325 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ yarn_mappings=1.16.5+build.6
loader_version=0.11.3

# Mod Properties
mod_version = 0.3.2
mod_version = 0.3.3
maven_group = dev.elexi.hugeblank
archives_base_name = bagels_baking

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ public void onInitialize() {
((BasicCropBlock)RICE_PLANT).setSeed(WILD_RICE);
registerItem("rice", RICE);
registerBlock("corn", CORN_STALK);
((BasicCropBlock)CORN_STALK).setSeed(CORN_SEEDS);
registerItem("corn", CORN);
registerItem("cooked_corn", COOKED_CORN);
registerItem("corn_seeds", CORN_SEEDS);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
package dev.elexi.hugeblank.bagels_baking.block;

import net.minecraft.block.*;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.enums.DoubleBlockHalf;
import net.minecraft.entity.Entity;
import net.minecraft.entity.mob.RavagerEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.EnumProperty;
import net.minecraft.state.property.IntProperty;
import net.minecraft.state.property.Properties;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.shape.VoxelShape;
import net.minecraft.world.*;
import net.minecraft.world.BlockView;
import net.minecraft.world.WorldAccess;
import net.minecraft.world.WorldView;
import org.jetbrains.annotations.Nullable;

import java.util.Random;

public class DoubleCropBlock extends PlantBlock implements Fertilizable {
public class DoubleCropBlock extends BasicCropBlock implements Fertilizable {

public static final EnumProperty<DoubleBlockHalf> HALF = Properties.DOUBLE_BLOCK_HALF;
public static final IntProperty AGE = Properties.AGE_7;
Expand Down Expand Up @@ -74,46 +70,10 @@ public void randomTick(BlockState state, ServerWorld world, BlockPos pos, Random
protected static float getAvailableMoisture(Block block, BlockView world, BlockPos pos) {
if (world.getBlockState(pos).get(HALF) == DoubleBlockHalf.UPPER) {
BlockState lowerState = world.getBlockState(pos.down());
return getAvailableMoisture(lowerState.getBlock(), world, pos.down());
}
float f = 1.0F;
BlockPos blockPos = pos.down();

for(int i = -1; i <= 1; ++i) {
for(int j = -1; j <= 1; ++j) {
float g = 0.0F;
BlockState blockState = world.getBlockState(blockPos.add(i, 0, j));
if (blockState.isOf(Blocks.FARMLAND)) {
g = 1.0F;
if (blockState.get(FarmlandBlock.MOISTURE) > 0) {
g = 3.0F;
}
}

if (i != 0 || j != 0) {
g /= 4.0F;
}

f += g;
}
}

BlockPos blockPos2 = pos.north();
BlockPos blockPos3 = pos.south();
BlockPos blockPos4 = pos.west();
BlockPos blockPos5 = pos.east();
boolean bl = block == world.getBlockState(blockPos4).getBlock() || block == world.getBlockState(blockPos5).getBlock();
boolean bl2 = block == world.getBlockState(blockPos2).getBlock() || block == world.getBlockState(blockPos3).getBlock();
if (bl && bl2) {
f /= 2.0F;
return CropBlock.getAvailableMoisture(lowerState.getBlock(), world, pos.down());
} else {
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();
if (bl3) {
f /= 2.0F;
}
return CropBlock.getAvailableMoisture(block, world, pos.down());
}

return f;
}

public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
Expand All @@ -124,75 +84,19 @@ public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos po
}
}

public void onBreak(World world, BlockPos pos, BlockState state, PlayerEntity player) {
if (!world.isClient) {
if (player.isCreative()) {
onBreakInCreative(world, pos, state, player);
} else {
dropStacks(state, world, pos, null, player, player.getMainHandStack());
}
}

super.onBreak(world, pos, state, player);
}

public void afterBreak(World world, PlayerEntity player, BlockPos pos, BlockState state, @Nullable BlockEntity blockEntity, ItemStack stack) {
super.afterBreak(world, player, pos, Blocks.AIR.getDefaultState(), blockEntity, stack);
}

protected static void onBreakInCreative(World world, BlockPos pos, BlockState state, PlayerEntity player) {
DoubleBlockHalf doubleBlockHalf = state.get(HALF);
if (doubleBlockHalf == DoubleBlockHalf.UPPER) {
BlockPos blockPos = pos.down();
BlockState blockState = world.getBlockState(blockPos);
if (blockState.getBlock() == state.getBlock() && blockState.get(HALF) == DoubleBlockHalf.LOWER) {
world.setBlockState(blockPos, Blocks.AIR.getDefaultState(), 35);
world.syncWorldEvent(player, 2001, blockPos, Block.getRawIdFromState(blockState));
}
}

}

public boolean canPlaceAt(BlockState state, WorldView world, BlockPos pos) {
BlockState lower = world.getBlockState(pos.down());
return state.get(HALF) == DoubleBlockHalf.LOWER ? lower.isOf(Blocks.FARMLAND) : (lower.getBlock() == this && state.get(HALF) == DoubleBlockHalf.UPPER);
return state.get(HALF) == DoubleBlockHalf.LOWER ? super.canPlaceAt(state, world, pos) : (lower.getBlock() == this && state.get(HALF) == DoubleBlockHalf.UPPER && lower.get(AGE) > 3);
}

public BlockState getStateForNeighborUpdate(BlockState state, Direction direction, BlockState newState, WorldAccess world, BlockPos pos, BlockPos posFrom) {
DoubleBlockHalf doubleBlockHalf = state.get(HALF);
if (doubleBlockHalf == DoubleBlockHalf.LOWER && direction == Direction.UP && (state.get(AGE) > 3 && world.getBlockState(pos.up()).getBlock() == Blocks.AIR)) {
return Blocks.AIR.getDefaultState();
} else {
return direction == Direction.DOWN && !state.canPlaceAt(world, pos) ? Blocks.AIR.getDefaultState() : super.getStateForNeighborUpdate(state, direction, newState, world, pos, posFrom);
}
}

public void onEntityCollision(BlockState state, World world, BlockPos pos, Entity entity) {
if (entity instanceof RavagerEntity && world.getGameRules().getBoolean(GameRules.DO_MOB_GRIEFING)) {
world.breakBlock(pos, true, entity);
return direction == Direction.DOWN && !state.canPlaceAt(world, pos) ? Blocks.AIR.getDefaultState() : super.getStateForNeighborUpdate(state, direction, newState, world, pos, posFrom);
}

super.onEntityCollision(state, world, pos, entity);
}

public IntProperty getAgeProperty() {
return AGE;
}

public int getMaxAge() {
return 7;
}

protected int getAge(BlockState state) {
return state.get(this.getAgeProperty());
}

public BlockState withAge(int age) {
return this.getDefaultState().with(this.getAgeProperty(), age);
}

public boolean isMature(BlockState state) {
return state.get(this.getAgeProperty()) >= this.getMaxAge();
}

@Override
Expand All @@ -209,15 +113,6 @@ protected void appendProperties(StateManager.Builder<Block, BlockState> builder)
builder.add(HALF, AGE);
}

@Override
public boolean canGrow(World world, Random random, BlockPos pos, BlockState state) {
return true;
}

protected int getGrowthAmount(World world) {
return MathHelper.nextInt(world.random, 2, 5);
}

@Override
public void grow(ServerWorld world, Random random, BlockPos pos, BlockState state) {
if (state.get(HALF) == DoubleBlockHalf.LOWER) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package dev.elexi.hugeblank.bagels_baking.entity;

import dev.elexi.hugeblank.bagels_baking.Baking;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.recipe.Ingredient;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.stream.Stream;

public class FeedItems {
public static Item[] COW = {Baking.CORN};
public static Item[] PIG = {Baking.CORN, Baking.TOMATO};
public static Item[] CHICKEN = {Baking.CORN_MEAL, Baking.CORN_SEEDS, Baking.WILD_RICE, Baking.RICE};

public static ArrayList<ItemStack> getList(Object[] objs) {
Stream<Object> items = Arrays.stream(objs);
ArrayList<ItemStack> stacks = new ArrayList<>();
items.forEach((Object item) -> {
stacks.add(((Item)item).getDefaultStack());
});
return stacks;
}

public static Ingredient set(Item[] a, Object[] b) {
ArrayList<ItemStack> out = getList(a);
out.addAll(getList(b));

return Ingredient.ofStacks(out.stream());
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private void addSplashes(CallbackInfoReturnable<String> cir) {
splashTexts.add("Kroi can't tyep sommtines but that' sokya");
splashTexts.add("Respect your food delivery drivers!");
splashTexts.add("A la minecarte!");
if (field_18934 != null && RANDOM.nextFloat() < 0.25) {
if (field_18934 != null && RANDOM.nextFloat() < 0.1) {
switch (field_18934.getUsername()) { // Surprises for my friends :)
case "rwr":
cir.setReturnValue("Betreucia killed Broseph!");
Expand All @@ -53,7 +53,7 @@ private void addSplashes(CallbackInfoReturnable<String> cir) {
case "roger109z":
cir.setReturnValue("poger! :)");
return;
case "KoriA":
case "Kori_A":
cir.setReturnValue("Korea? No, KoriA.");
return;
case "He_Is_Man":
Expand All @@ -70,7 +70,6 @@ private void addSplashes(CallbackInfoReturnable<String> cir) {
return;
case "ChocolateFrog18":
cir.setReturnValue("Any British frogs?");
return;
}
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package dev.elexi.hugeblank.bagels_baking.mixin.entity.passive;

import dev.elexi.hugeblank.bagels_baking.Baking;
import net.minecraft.entity.passive.AnimalEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.recipe.Ingredient;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(AnimalEntity.class)
public class AnimalBreedItems {

private static final Ingredient breedingItems = Ingredient.ofItems(Items.WHEAT, Baking.CORN);

@Inject(at = @At("HEAD"), method = "isBreedingItem(Lnet/minecraft/item/ItemStack;)Z", cancellable = true)
public void isBreedingItem(ItemStack stack, CallbackInfoReturnable<Boolean> cir) {
cir.setReturnValue(breedingItems.test(stack));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package dev.elexi.hugeblank.bagels_baking.mixin.entity.passive;

import dev.elexi.hugeblank.bagels_baking.entity.FeedItems;
import net.minecraft.entity.passive.ChickenEntity;
import net.minecraft.item.ItemConvertible;
import net.minecraft.recipe.Ingredient;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

@Mixin(ChickenEntity.class)
class ChickenSeeds {
@Redirect(at = @At(value = "INVOKE", target = "Lnet/minecraft/recipe/Ingredient;ofItems([Lnet/minecraft/item/ItemConvertible;)Lnet/minecraft/recipe/Ingredient;", ordinal = 0), method = "<clinit>")
private static Ingredient validSeeds(ItemConvertible[] items) {
return FeedItems.set(FeedItems.CHICKEN, items);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package dev.elexi.hugeblank.bagels_baking.mixin.entity.passive;

import dev.elexi.hugeblank.bagels_baking.entity.FeedItems;
import net.minecraft.entity.passive.CowEntity;
import net.minecraft.item.ItemConvertible;
import net.minecraft.recipe.Ingredient;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

@Mixin(CowEntity.class)
class CowFeed {
@Redirect(at = @At(value = "INVOKE", target = "Lnet/minecraft/recipe/Ingredient;ofItems([Lnet/minecraft/item/ItemConvertible;)Lnet/minecraft/recipe/Ingredient;", ordinal = 0), method = "initGoals()V")
private Ingredient breedItems(ItemConvertible[] items) {
return FeedItems.set(FeedItems.COW, items);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package dev.elexi.hugeblank.bagels_baking.mixin.entity.passive;

import dev.elexi.hugeblank.bagels_baking.entity.FeedItems;
import net.minecraft.entity.passive.PigEntity;
import net.minecraft.item.ItemConvertible;
import net.minecraft.recipe.Ingredient;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

@Mixin(PigEntity.class)
class PigFeed {
@Redirect(at = @At(value = "INVOKE", target = "Lnet/minecraft/recipe/Ingredient;ofItems([Lnet/minecraft/item/ItemConvertible;)Lnet/minecraft/recipe/Ingredient;", ordinal = 0), method = "<clinit>")
private static Ingredient breedItems(ItemConvertible[] items) {
return FeedItems.set(FeedItems.PIG, items);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package dev.elexi.hugeblank.bagels_baking.mixin.entity.passive;

import dev.elexi.hugeblank.bagels_baking.entity.FeedItems;
import net.minecraft.entity.passive.SheepEntity;
import net.minecraft.item.ItemConvertible;
import net.minecraft.recipe.Ingredient;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

@Mixin(SheepEntity.class)
class SheepFeed {
@Redirect(at = @At(value = "INVOKE", target = "Lnet/minecraft/recipe/Ingredient;ofItems([Lnet/minecraft/item/ItemConvertible;)Lnet/minecraft/recipe/Ingredient;", ordinal = 0), method = "initGoals()V")
private Ingredient breedItems(ItemConvertible[] items) {
return FeedItems.set(FeedItems.COW, items);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.elexi.hugeblank.bagels_baking.mixin.entity;
package dev.elexi.hugeblank.bagels_baking.mixin.entity.passive;

import dev.elexi.hugeblank.bagels_baking.Baking;
import net.minecraft.entity.EntityType;
Expand Down
Loading

0 comments on commit 6f3e227

Please sign in to comment.