Added Replenish function (replanting crops)
This commit is contained in:
@ -8,7 +8,7 @@ import java.util.List;
|
|||||||
|
|
||||||
public class Config {
|
public class Config {
|
||||||
|
|
||||||
public String configVersion = "1.0";
|
public String configVersion = "1.1";
|
||||||
|
|
||||||
public boolean joinMessageEnabled = true;
|
public boolean joinMessageEnabled = true;
|
||||||
public String joinMessage = "§8[§a+§8] §7%s";
|
public String joinMessage = "§8[§a+§8] §7%s";
|
||||||
@ -40,6 +40,8 @@ public class Config {
|
|||||||
|
|
||||||
public boolean chatCalcEnabled = true;
|
public boolean chatCalcEnabled = true;
|
||||||
|
|
||||||
|
public boolean replenishEnabled = false;
|
||||||
|
|
||||||
public ScreenHandlerType<GenericContainerScreenHandler> screenHandlerType() {
|
public ScreenHandlerType<GenericContainerScreenHandler> screenHandlerType() {
|
||||||
return switch (sharedEnderChestRows) {
|
return switch (sharedEnderChestRows) {
|
||||||
case 1 -> ScreenHandlerType.GENERIC_9X1;
|
case 1 -> ScreenHandlerType.GENERIC_9X1;
|
||||||
|
62
src/main/java/wtf/hak/survivalfabric/mixin/BlockMixin.java
Normal file
62
src/main/java/wtf/hak/survivalfabric/mixin/BlockMixin.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -3,6 +3,7 @@
|
|||||||
"package": "wtf.hak.survivalfabric.mixin",
|
"package": "wtf.hak.survivalfabric.mixin",
|
||||||
"compatibilityLevel": "JAVA_21",
|
"compatibilityLevel": "JAVA_21",
|
||||||
"mixins": [
|
"mixins": [
|
||||||
|
"BlockMixin",
|
||||||
"PlayerManagerMixin",
|
"PlayerManagerMixin",
|
||||||
"ServerPlayerEntityMixin",
|
"ServerPlayerEntityMixin",
|
||||||
"ServerPlayNetworkHandlerMixin",
|
"ServerPlayNetworkHandlerMixin",
|
||||||
|
Reference in New Issue
Block a user