Enhance Stalinium tools: Add sapling/plank drops and ally item sharing to axe, enable multi-block breaking and ally haste effect for shovel.
This commit is contained in:
@@ -2,10 +2,11 @@ package net.krituximon.stalinium.item;
|
|||||||
|
|
||||||
import net.krituximon.stalinium.util.PlacedLogStorage;
|
import net.krituximon.stalinium.util.PlacedLogStorage;
|
||||||
import net.minecraft.core.BlockPos;
|
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.server.level.ServerLevel;
|
||||||
import net.minecraft.tags.BlockTags;
|
import net.minecraft.tags.BlockTags;
|
||||||
import net.minecraft.world.InteractionResult;
|
import net.minecraft.world.InteractionResult;
|
||||||
import net.minecraft.world.entity.EquipmentSlot;
|
|
||||||
import net.minecraft.world.entity.LivingEntity;
|
import net.minecraft.world.entity.LivingEntity;
|
||||||
import net.minecraft.world.entity.player.Player;
|
import net.minecraft.world.entity.player.Player;
|
||||||
import net.minecraft.world.item.AxeItem;
|
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.Tier;
|
||||||
import net.minecraft.world.item.context.UseOnContext;
|
import net.minecraft.world.item.context.UseOnContext;
|
||||||
import net.minecraft.world.level.Level;
|
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 net.minecraft.world.level.block.state.BlockState;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
import net.minecraft.server.level.ServerPlayer;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class StaliniumAxeItem extends AxeItem {
|
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) {
|
private void cutDownTree(Level level, BlockPos start, Player player, ItemStack stack) {
|
||||||
|
// 1) Gather all logs to break
|
||||||
Set<BlockPos> toBreak = new HashSet<>();
|
Set<BlockPos> toBreak = new HashSet<>();
|
||||||
Deque<BlockPos> queue = new ArrayDeque<>();
|
Deque<BlockPos> queue = new ArrayDeque<>();
|
||||||
queue.add(start);
|
queue.add(start);
|
||||||
@@ -40,27 +46,69 @@ public class StaliniumAxeItem extends AxeItem {
|
|||||||
while (!queue.isEmpty() && toBreak.size() < 256) {
|
while (!queue.isEmpty() && toBreak.size() < 256) {
|
||||||
BlockPos current = queue.removeFirst();
|
BlockPos current = queue.removeFirst();
|
||||||
if (toBreak.contains(current)) continue;
|
if (toBreak.contains(current)) continue;
|
||||||
BlockState bs = level.getBlockState(current);
|
|
||||||
if (storage.contains(current)) continue;
|
|
||||||
|
|
||||||
if (bs.is(BlockTags.LOGS)) {
|
BlockState bs = level.getBlockState(current);
|
||||||
toBreak.add(current);
|
if (storage.contains(current) || !bs.is(BlockTags.LOGS)) continue;
|
||||||
for (int dx = -1; dx <= 1; dx++) {
|
|
||||||
for (int dy = 0; dy <= 1; dy++) {
|
toBreak.add(current);
|
||||||
for (int dz = -1; dz <= 1; dz++) {
|
for (int dx = -1; dx <= 1; dx++) {
|
||||||
BlockPos next = current.offset(dx, dy, dz);
|
for (int dy = 0; dy <= 1; dy++) {
|
||||||
if (!toBreak.contains(next)) {
|
for (int dz = -1; dz <= 1; dz++) {
|
||||||
queue.add(next);
|
queue.add(current.offset(dx, dy, dz));
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
List<Player> allies = level
|
||||||
|
.getEntitiesOfClass(Player.class,
|
||||||
|
player.getBoundingBox().inflate(10.0),
|
||||||
|
p -> p instanceof ServerPlayer);
|
||||||
for (BlockPos logPos : toBreak) {
|
for (BlockPos logPos : toBreak) {
|
||||||
|
BlockState before = level.getBlockState(logPos);
|
||||||
|
Block sapling = getSaplingForLog(before.getBlock());
|
||||||
|
Block plankBlock = getPlankForLog(before.getBlock());
|
||||||
level.destroyBlock(logPos, true, player);
|
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
|
@Override
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package net.krituximon.stalinium.item;
|
|||||||
|
|
||||||
import net.minecraft.core.BlockPos;
|
import net.minecraft.core.BlockPos;
|
||||||
import net.minecraft.server.level.ServerPlayer;
|
import net.minecraft.server.level.ServerPlayer;
|
||||||
|
import net.minecraft.tags.BlockTags;
|
||||||
import net.minecraft.world.effect.MobEffectInstance;
|
import net.minecraft.world.effect.MobEffectInstance;
|
||||||
import net.minecraft.world.effect.MobEffects;
|
import net.minecraft.world.effect.MobEffects;
|
||||||
import net.minecraft.world.entity.LivingEntity;
|
import net.minecraft.world.entity.LivingEntity;
|
||||||
@@ -24,14 +25,33 @@ public class StaliniumShovelItem extends ShovelItem {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean mineBlock(ItemStack stack, Level level, BlockState state, BlockPos pos, LivingEntity miningEntity) {
|
public boolean mineBlock(ItemStack stack, Level level, BlockState state, BlockPos pos, LivingEntity miningEntity) {
|
||||||
if (!level.isClientSide) {
|
if (level.isClientSide) return true;
|
||||||
MobEffectInstance haste = new MobEffectInstance(MobEffects.DIG_SPEED, 100, 0, false, true);
|
if (state.is(BlockTags.MINEABLE_WITH_SHOVEL)) {
|
||||||
miningEntity.addEffect(haste);
|
boolean anyBroken = false;
|
||||||
Level world = miningEntity.getCommandSenderWorld();
|
for (int dx = -1; dx <= 1; dx++) {
|
||||||
var box = miningEntity.getBoundingBox().inflate(10.0);
|
for (int dz = -1; dz <= 1; dz++) {
|
||||||
List<Player> nearby = world.getEntitiesOfClass(Player.class, box, p -> p instanceof ServerPlayer && miningEntity.isAlliedTo(p));
|
BlockPos target = pos.offset(dx, 0, dz);
|
||||||
for (Player p : nearby) {
|
BlockState bs = level.getBlockState(target);
|
||||||
p.addEffect(haste);
|
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<Player> 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;
|
return true;
|
||||||
|
|||||||
Reference in New Issue
Block a user