From 6bea334ef5e76de55fb5debb6b58b4bb13040a8d Mon Sep 17 00:00:00 2001 From: IM23a-spirgif Date: Fri, 23 May 2025 09:13:22 +0200 Subject: [PATCH] Enhance Stalinium tools: Add sapling/plank drops and ally item sharing to axe, enable multi-block breaking and ally haste effect for shovel. --- .../stalinium/item/StaliniumAxeItem.java | 74 +++++++++++++++---- .../stalinium/item/StaliniumShovelItem.java | 36 +++++++-- 2 files changed, 89 insertions(+), 21 deletions(-) diff --git a/src/main/java/net/krituximon/stalinium/item/StaliniumAxeItem.java b/src/main/java/net/krituximon/stalinium/item/StaliniumAxeItem.java index 1029b62..c40c73e 100644 --- a/src/main/java/net/krituximon/stalinium/item/StaliniumAxeItem.java +++ b/src/main/java/net/krituximon/stalinium/item/StaliniumAxeItem.java @@ -2,10 +2,11 @@ package net.krituximon.stalinium.item; import net.krituximon.stalinium.util.PlacedLogStorage; import net.minecraft.core.BlockPos; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.resources.ResourceLocation; import net.minecraft.server.level.ServerLevel; import net.minecraft.tags.BlockTags; import net.minecraft.world.InteractionResult; -import net.minecraft.world.entity.EquipmentSlot; import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.AxeItem; @@ -13,8 +14,12 @@ import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Tier; import net.minecraft.world.item.context.UseOnContext; import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.state.BlockState; +import javax.annotation.Nullable; +import net.minecraft.server.level.ServerPlayer; import java.util.*; public class StaliniumAxeItem extends AxeItem { @@ -32,6 +37,7 @@ public class StaliniumAxeItem extends AxeItem { } private void cutDownTree(Level level, BlockPos start, Player player, ItemStack stack) { + // 1) Gather all logs to break Set toBreak = new HashSet<>(); Deque queue = new ArrayDeque<>(); queue.add(start); @@ -40,27 +46,69 @@ public class StaliniumAxeItem extends AxeItem { while (!queue.isEmpty() && toBreak.size() < 256) { BlockPos current = queue.removeFirst(); if (toBreak.contains(current)) continue; - BlockState bs = level.getBlockState(current); - if (storage.contains(current)) continue; - if (bs.is(BlockTags.LOGS)) { - toBreak.add(current); - for (int dx = -1; dx <= 1; dx++) { - for (int dy = 0; dy <= 1; dy++) { - for (int dz = -1; dz <= 1; dz++) { - BlockPos next = current.offset(dx, dy, dz); - if (!toBreak.contains(next)) { - queue.add(next); - } - } + BlockState bs = level.getBlockState(current); + if (storage.contains(current) || !bs.is(BlockTags.LOGS)) continue; + + toBreak.add(current); + for (int dx = -1; dx <= 1; dx++) { + for (int dy = 0; dy <= 1; dy++) { + for (int dz = -1; dz <= 1; dz++) { + queue.add(current.offset(dx, dy, dz)); } } } } + List allies = level + .getEntitiesOfClass(Player.class, + player.getBoundingBox().inflate(10.0), + p -> p instanceof ServerPlayer); for (BlockPos logPos : toBreak) { + BlockState before = level.getBlockState(logPos); + Block sapling = getSaplingForLog(before.getBlock()); + Block plankBlock = getPlankForLog(before.getBlock()); level.destroyBlock(logPos, true, player); + if (sapling != null) { + ItemStack saplingStack = new ItemStack(sapling); + for (Player ally : allies) { + ally.addItem(saplingStack.copy()); + } + } + if (plankBlock != null) { + ItemStack plankStack = new ItemStack(plankBlock); + for (Player ally : allies) { + ally.addItem(plankStack.copy()); + } + } } } + + private @Nullable Block getSaplingForLog(Block log) { + ResourceLocation id = BuiltInRegistries.BLOCK.getKey(log); + if (id == null ) return null; + String path = id.getPath(); + if (!path.endsWith("_log")) return null; + String saplingPath = path.substring(0, path.length() - 4) + "_sapling"; + ResourceLocation sapId = ResourceLocation.fromNamespaceAndPath(id.getNamespace(), saplingPath); + return BuiltInRegistries.BLOCK.getOptional(sapId) + .orElse(Blocks.OAK_SAPLING); + } + + private @Nullable Block getPlankForLog(Block log) { + ResourceLocation id = BuiltInRegistries.BLOCK.getKey(log); + if (id == null) return null; + String path = id.getPath(); + if (!path.endsWith("_log")) return null; + + String plankPath = path.substring(0, path.length() - 4) + "_planks"; + ResourceLocation plankId = ResourceLocation.fromNamespaceAndPath(id.getNamespace(), plankPath); + + // fall back to oak planks if the specific one isn't registered + return BuiltInRegistries.BLOCK.getOptional(plankId) + .orElse(Blocks.OAK_PLANKS); + } + + @Override diff --git a/src/main/java/net/krituximon/stalinium/item/StaliniumShovelItem.java b/src/main/java/net/krituximon/stalinium/item/StaliniumShovelItem.java index 519d0ec..c0c8ee2 100644 --- a/src/main/java/net/krituximon/stalinium/item/StaliniumShovelItem.java +++ b/src/main/java/net/krituximon/stalinium/item/StaliniumShovelItem.java @@ -2,6 +2,7 @@ package net.krituximon.stalinium.item; import net.minecraft.core.BlockPos; import net.minecraft.server.level.ServerPlayer; +import net.minecraft.tags.BlockTags; import net.minecraft.world.effect.MobEffectInstance; import net.minecraft.world.effect.MobEffects; import net.minecraft.world.entity.LivingEntity; @@ -24,14 +25,33 @@ public class StaliniumShovelItem extends ShovelItem { @Override public boolean mineBlock(ItemStack stack, Level level, BlockState state, BlockPos pos, LivingEntity miningEntity) { - if (!level.isClientSide) { - MobEffectInstance haste = new MobEffectInstance(MobEffects.DIG_SPEED, 100, 0, false, true); - miningEntity.addEffect(haste); - Level world = miningEntity.getCommandSenderWorld(); - var box = miningEntity.getBoundingBox().inflate(10.0); - List nearby = world.getEntitiesOfClass(Player.class, box, p -> p instanceof ServerPlayer && miningEntity.isAlliedTo(p)); - for (Player p : nearby) { - p.addEffect(haste); + if (level.isClientSide) return true; + if (state.is(BlockTags.MINEABLE_WITH_SHOVEL)) { + boolean anyBroken = false; + for (int dx = -1; dx <= 1; dx++) { + for (int dz = -1; dz <= 1; dz++) { + BlockPos target = pos.offset(dx, 0, dz); + BlockState bs = level.getBlockState(target); + if (bs.is(BlockTags.MINEABLE_WITH_SHOVEL)) { + level.destroyBlock(target, true, miningEntity); + anyBroken = true; + } + } + } + + if (anyBroken && miningEntity instanceof Player player) { + MobEffectInstance selfHaste = new MobEffectInstance(MobEffects.DIG_SPEED, 100, 1, false, true, true); + player.addEffect(selfHaste); + var box = player.getBoundingBox().inflate(10.0); + List allies = level.getEntitiesOfClass( + Player.class, + box, + other -> other instanceof ServerPlayer && player.isAlliedTo(other) + ); + MobEffectInstance allyHaste = new MobEffectInstance(MobEffects.DIG_SPEED, 100, 0, false, true, true); + for (Player ally : allies) { + ally.addEffect(allyHaste); + } } } return true;