Skip to content

Commit

Permalink
Add validation of plant operation in case of invalid count down.
Browse files Browse the repository at this point in the history
  • Loading branch information
MUYUTwilighter committed Oct 6, 2024
1 parent 19ebaff commit 8471105
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Done to increase the memory available to gradle.
org.gradle.jvmargs=-Xmx1G
# Mod Properties
mod_version=1.2.7
mod_version=1.2.8
maven_group=cool.muyucloud
archives_base_name=saplanting-fabric-1.21

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,11 @@ private void plant() {
BlockPos tmpPos = this.findLargeSpace(pos);
if (tmpPos != null) {
PlantContext context = new PlantContext();
context.setItem(stack.getItem());
context.setStack(stack);
context.setPos(tmpPos);
context.setWorld((ServerWorld) world);
context.setLarge(true);
PlantContext.PLANT_TASKS.offer(context);
stack.setCount(stack.getCount() - 4);
return;
}
}
Expand All @@ -207,12 +206,11 @@ private void plant() {

/* Plant Small Objects(including sapling) */
PlantContext context = new PlantContext();
context.setItem(stack.getItem());
context.setStack(stack);
context.setPos(pos);
context.setWorld((ServerWorld) world);
context.setLarge(false);
PlantContext.PLANT_TASKS.offer(context);
stack.setCount(stack.getCount() - 1);
}

@Unique
Expand Down
27 changes: 21 additions & 6 deletions src/main/java/cool/muyucloud/saplanting/util/PlantContext.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package cool.muyucloud.saplanting.util;

import net.fabricmc.fabric.api.entity.FakePlayer;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUsageContext;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.Hand;
Expand All @@ -15,7 +19,7 @@
public class PlantContext {
public static final ConcurrentLinkedQueue<PlantContext> PLANT_TASKS = new ConcurrentLinkedQueue<>();

private Item item;
private ItemStack stack;
private ServerWorld world;
private BlockPos pos;
private Boolean large;
Expand All @@ -37,15 +41,26 @@ private void useOnBlock() {

private void useOnBlock(BlockPos pos) {
ItemUsageContext context = new ItemUsageContext(fakePlayer, Hand.MAIN_HAND, new BlockHitResult(pos.up().toCenterPos(), Direction.UP, pos, false));
item.useOnBlock(context);
stack.useOnBlock(context);
if (this.getTargetBlock(pos).isOf(this.getBlock())) {
stack.setCount(stack.getCount() - 1);
}
}

private Block getBlock() {
return ((BlockItem) stack.getItem()).getBlock();
}

private BlockState getTargetBlock(BlockPos pos) {
return this.world.getBlockState(pos);
}

public Item getItem() {
return item;
public ItemStack getStack() {
return stack;
}

public void setItem(Item item) {
this.item = item;
public void setStack(ItemStack stack) {
this.stack = stack;
}

public World getWorld() {
Expand Down

0 comments on commit 8471105

Please sign in to comment.