Added Replenish function (replanting crops)

This commit is contained in:
2025-05-19 10:20:47 +02:00
parent ef29652e07
commit 35148c2159
3 changed files with 66 additions and 1 deletions

View File

@ -8,7 +8,7 @@ import java.util.List;
public class Config {
public String configVersion = "1.0";
public String configVersion = "1.1";
public boolean joinMessageEnabled = true;
public String joinMessage = "§8[§a+§8] §7%s";
@ -40,6 +40,8 @@ public class Config {
public boolean chatCalcEnabled = true;
public boolean replenishEnabled = false;
public ScreenHandlerType<GenericContainerScreenHandler> screenHandlerType() {
return switch (sharedEnderChestRows) {
case 1 -> ScreenHandlerType.GENERIC_9X1;

View File

@ -0,0 +1,62 @@
package wtf.hak.survivalfabric.mixin;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.CropBlock;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.HoeItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.stat.Stats;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.GameMode;
import net.minecraft.world.World;
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;
import wtf.hak.survivalfabric.config.ConfigManager;
import java.util.List;
@Mixin(Block.class)
public abstract class BlockMixin {
@Inject(method = "onBreak", at = @At("HEAD"), cancellable = true)
public void onBreak(World world, BlockPos pos, BlockState state, PlayerEntity player, CallbackInfoReturnable<BlockState> cir) {
if (world.isClient()) return;
if (state.getBlock() instanceof CropBlock && ConfigManager.getConfig().replenishEnabled) {
ItemStack mainHand = player.getStackInHand(Hand.MAIN_HAND);
if (mainHand.getItem() instanceof HoeItem) {
Item seedItem = state.getBlock().asItem();
Block seedBlock = state.getBlock();
List<ItemStack> drops = Block.getDroppedStacks(state, (ServerWorld) world, pos, null, player, mainHand);
if (removeIfAvailable(drops, seedItem)) {
if(player.getGameMode() != GameMode.CREATIVE) {
for (ItemStack drop : drops) {
Block.dropStack(world, pos, drop);
}
}
world.getServer().executeSync(() -> {
world.setBlockState(pos, seedBlock.getDefaultState());
});
player.incrementStat(Stats.USED.getOrCreateStat(seedItem));
cir.cancel();
}
}
}
}
private boolean removeIfAvailable(List<ItemStack> drops, Item item) {
for (ItemStack drop : drops) {
if (drop.getItem() == item) {
drop.decrement(1);
return true;
}
}
return false;
}
}

View File

@ -3,6 +3,7 @@
"package": "wtf.hak.survivalfabric.mixin",
"compatibilityLevel": "JAVA_21",
"mixins": [
"BlockMixin",
"PlayerManagerMixin",
"ServerPlayerEntityMixin",
"ServerPlayNetworkHandlerMixin",