Skip to content

Commit

Permalink
Refactored code to match 1.21.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Scillman committed Oct 24, 2024
1 parent ab32728 commit c63a7e3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 145 deletions.
7 changes: 2 additions & 5 deletions src/main/java/com/builtbroken/sbmgrowmeal/GrowmealFabric.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,15 @@
import net.minecraft.util.Identifier;

public class GrowmealFabric implements ModInitializer {

public static final String MOD_ID = "sbmgrowmeal";
public static final String MOD_NAME = "SBM Growmeal";

public static final Logger LOGGER = LoggerFactory.getLogger(MOD_NAME);

public static final Item GROWMEAL = register("growmeal", GrowmealItem::new);
public static final GrowmealItem GROWMEAL = register("growmeal", GrowmealItem::new);

@Override
public void onInitialize() {

}
public void onInitialize() {}

private static <T extends Item> T register(String id, Function<Item.Settings, T> factory) {
Identifier guid = Identifier.of(MOD_ID, id);
Expand Down
162 changes: 22 additions & 140 deletions src/main/java/com/builtbroken/sbmgrowmeal/GrowmealItem.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,22 @@
package com.builtbroken.sbmgrowmeal;

import org.jetbrains.annotations.Nullable;

import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.DeadCoralWallFanBlock;
import net.minecraft.block.Fertilizable;
import net.minecraft.item.BoneMealItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUsageContext;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.registry.Registries;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.registry.tag.BiomeTags;
import net.minecraft.registry.tag.BlockTags;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.ActionResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.random.Random;
import net.minecraft.world.World;
import net.minecraft.world.WorldAccess;
import net.minecraft.world.WorldEvents;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.event.GameEvent;

/**
* @see {@link net.minecraft.item.BoneMealItem BoneMealItem}
* @see {@link net.minecraft.block.SaplingBlock SaplingBlock}
* @see {@link net.minecraft.block.CropBlock CropBlock}
*/
public class GrowmealItem extends Item {
public static int tries = 1000;
public static final int MAX_TRIES = 1000;

public GrowmealItem(Item.Settings settings) {
super(settings);
Expand All @@ -43,141 +27,39 @@ public ActionResult useOnBlock(ItemUsageContext context) {
World world = context.getWorld();
BlockPos blockPos = context.getBlockPos();
BlockPos blockPos2 = blockPos.offset(context.getSide());

boolean isGrown = false;
for(int i = 0; i < tries; i++) {
if (GrowmealItem.useOnFertilizable(context.getStack(), world, blockPos)) {
// GrowmealFabric.LOGGER.info("using on plant " + i + " " + Boolean.toString(isGrown));
if (!world.isClient) {
world.syncWorldEvent(WorldEvents.BONE_MEAL_USED, blockPos, 0);
isGrown = true;
}
} else {

for (int i = 0; i < MAX_TRIES; ++i) {
if (!BoneMealItem.useOnFertilizable(context.getStack(), world, blockPos)) {
break;
}
}

if (isGrown)
return ActionResult.SUCCESS; // ActionResult.success(world.isClient);

BlockState blockState = world.getBlockState(blockPos);
boolean bl = blockState.isSideSolidFullSquare(world, blockPos, context.getSide());
if (bl && BoneMealItem.useOnGround(context.getStack(), world, blockPos2, context.getSide())) {
if (!world.isClient) {
world.syncWorldEvent(WorldEvents.BONE_MEAL_USED, blockPos2, 0);
world.syncWorldEvent(WorldEvents.BONE_MEAL_USED, blockPos, 15);
isGrown = true;
}
return ActionResult.SUCCESS; // ActionResult.success(world.isClient);
}
return ActionResult.PASS;
}

public static boolean useOnFertilizable(ItemStack stack, World world, BlockPos pos) {
BlockState blockState = world.getBlockState(pos);
if (blockState.getBlock() instanceof Fertilizable fertilizable && fertilizable.isFertilizable(world, pos, blockState)) {
if (world instanceof ServerWorld) {
if (fertilizable.canGrow(world, world.random, pos, blockState)) {
// GrowmealFabric.LOGGER.info("Looping " + i);
fertilizable.grow((ServerWorld)world, world.random, pos, blockState);
}
stack.decrement(1);
if (isGrown) {
if (!world.isClient) {
context.getPlayer().emitGameEvent(GameEvent.ITEM_INTERACT_FINISH);
}
// GrowmealFabric.LOGGER.info("used on plant");
return true;
return ActionResult.SUCCESS;
}
return false;
}

public static boolean useOnGround(ItemStack stack, World world, BlockPos blockPos, @Nullable Direction facing) {
if (!world.getBlockState(blockPos).isOf(Blocks.WATER) || world.getFluidState(blockPos).getLevel() != 8) {
return false;
}
if (!(world instanceof ServerWorld)) {
return true;
}
Random random = world.getRandom();
block0: for (int i = 0; i < 128; ++i) {
BlockPos blockPos2 = blockPos;
BlockState blockState = Blocks.SEAGRASS.getDefaultState();
for (int j = 0; j < i / 16; ++j) {
if (world.getBlockState(blockPos2 = blockPos2.add(random.nextInt(3) - 1, (random.nextInt(3) - 1) * random.nextInt(3) / 2, random.nextInt(3) - 1)).isFullCube(world, blockPos2)) continue block0;
}
RegistryEntry<Biome> registryEntry = world.getBiome(blockPos2);
if (registryEntry.isIn(BiomeTags.PRODUCES_CORALS_FROM_BONEMEAL)) {
if (i == 0 && facing != null && facing.getAxis().isHorizontal()) {
blockState = Registries.BLOCK
.getRandomEntry(BlockTags.WALL_CORALS, world.random)
.map(blockEntry -> blockEntry.value().getDefaultState())
.orElse(blockState);
// blockState = Registries.BLOCK
// .getEntryList(BlockTags.WALL_CORALS)
// .flatMap(blocks -> blocks.getRandom(world.random)).map(blockEntry -> ((Block)blockEntry.value()).getDefaultState())
// .orElse(blockState);
if (blockState.contains(DeadCoralWallFanBlock.FACING)) {
blockState = (BlockState)blockState.with(DeadCoralWallFanBlock.FACING, facing);
}
} else if (random.nextInt(4) == 0) {
blockState = Registries.BLOCK
.getRandomEntry(BlockTags.UNDERWATER_BONEMEALS, world.random)
.map(blockEntry -> blockEntry.value().getDefaultState())
.orElse(blockState);
// blockState = Registries.BLOCK
// .getEntryList(BlockTags.UNDERWATER_BONEMEALS)
// .flatMap(blocks -> blocks.getRandom(world.random))
// .map(blockEntry -> ((Block)blockEntry.value()).getDefaultState())
// .orElse(blockState);
}
}
if (blockState.isIn(BlockTags.WALL_CORALS, state -> state.contains(DeadCoralWallFanBlock.FACING))) {
for (int k = 0; !blockState.canPlaceAt(world, blockPos2) && k < 4; ++k) {
blockState = (BlockState)blockState.with(DeadCoralWallFanBlock.FACING, Direction.Type.HORIZONTAL.random(random));
}
}
if (!blockState.canPlaceAt(world, blockPos2)) continue;
BlockState blockState2 = world.getBlockState(blockPos2);
if (blockState2.isOf(Blocks.WATER) && world.getFluidState(blockPos2).getLevel() == 8) {
world.setBlockState(blockPos2, blockState, Block.NOTIFY_ALL);
continue;
BlockState blockState = world.getBlockState(blockPos);
boolean bl = blockState.isSideSolidFullSquare(world, blockPos, context.getSide());

if (bl && BoneMealItem.useOnGround(context.getStack(), world, blockPos2, context.getSide())) {
if (!world.isClient) {
context.getPlayer().emitGameEvent(GameEvent.ITEM_INTERACT_FINISH);
world.syncWorldEvent(WorldEvents.BONE_MEAL_USED, blockPos2, 15);
}
if (!blockState2.isOf(Blocks.SEAGRASS) || random.nextInt(10) != 0) continue;
((Fertilizable)((Object)Blocks.SEAGRASS)).grow((ServerWorld)world, random, blockPos2, blockState2);
}
stack.decrement(1);
return true;
}

public static void createParticles(WorldAccess world, BlockPos pos, int count) {
double e;
BlockState blockState;
if (count == 0) {
count = 15;
}
if ((blockState = world.getBlockState(pos)).isAir()) {
return;
}
double d = 0.5;
if (blockState.isOf(Blocks.WATER)) {
count *= 3;
e = 1.0;
d = 3.0;
} else if (blockState.isOpaqueFullCube()) {
pos = pos.up();
count *= 3;
d = 3.0;
e = 1.0;
} else {
e = blockState.getOutlineShape(world, pos).getMax(Direction.Axis.Y);
}
world.addParticle(ParticleTypes.HAPPY_VILLAGER, (double)pos.getX() + 0.5, (double)pos.getY() + 0.5, (double)pos.getZ() + 0.5, 0.0, 0.0, 0.0);
Random random = world.getRandom();
for (int i = 0; i < count; ++i) {
double m;
double l;
double f = random.nextGaussian() * 0.02;
double g = random.nextGaussian() * 0.02;
double h = random.nextGaussian() * 0.02;
double j = 0.5 - d;
double k = (double)pos.getX() + j + random.nextDouble() * d * 2.0;
if (world.getBlockState(BlockPos.ofFloored(k, l = (double)pos.getY() + random.nextDouble() * e, m = (double)pos.getZ() + j + random.nextDouble() * d * 2.0).down()).isAir()) continue;
world.addParticle(ParticleTypes.HAPPY_VILLAGER, k, l, m, f, g, h);
return ActionResult.SUCCESS;
}

return ActionResult.PASS;
}
}

0 comments on commit c63a7e3

Please sign in to comment.