Finished stalinium hoe, still needs to be tested
This commit is contained in:
@@ -1,10 +1,24 @@
|
|||||||
package net.krituximon.stalinium.item;
|
package net.krituximon.stalinium.item;
|
||||||
|
|
||||||
|
import net.minecraft.core.BlockPos;
|
||||||
|
import net.minecraft.server.level.ServerLevel;
|
||||||
|
import net.minecraft.server.level.ServerPlayer;
|
||||||
|
import net.minecraft.util.RandomSource;
|
||||||
|
import net.minecraft.world.InteractionResult;
|
||||||
import net.minecraft.world.entity.LivingEntity;
|
import net.minecraft.world.entity.LivingEntity;
|
||||||
|
import net.minecraft.world.entity.player.Player;
|
||||||
import net.minecraft.world.item.HoeItem;
|
import net.minecraft.world.item.HoeItem;
|
||||||
import net.minecraft.world.item.Item;
|
import net.minecraft.world.item.Item;
|
||||||
import net.minecraft.world.item.ItemStack;
|
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.level.Level;
|
||||||
|
import net.minecraft.world.level.block.Block;
|
||||||
|
import net.minecraft.world.level.block.Blocks;
|
||||||
|
import net.minecraft.world.level.block.CropBlock;
|
||||||
|
import net.minecraft.world.level.block.state.BlockState;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class StaliniumHoeItem extends HoeItem {
|
public class StaliniumHoeItem extends HoeItem {
|
||||||
public StaliniumHoeItem(Tier tier, Item.Properties properties) {
|
public StaliniumHoeItem(Tier tier, Item.Properties properties) {
|
||||||
@@ -12,8 +26,55 @@ public class StaliniumHoeItem extends HoeItem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hurtEnemy(ItemStack stack, LivingEntity target, LivingEntity attacker) {
|
public InteractionResult useOn(UseOnContext ctx) {
|
||||||
return true;
|
Level level = ctx.getLevel();
|
||||||
|
BlockPos pos = ctx.getClickedPos();
|
||||||
|
BlockState state = level.getBlockState(pos);
|
||||||
|
Player player = ctx.getPlayer();
|
||||||
|
|
||||||
|
// Only run on server side and if it's raining at this position
|
||||||
|
if (!level.isClientSide &&
|
||||||
|
level.isRaining() &&
|
||||||
|
level.isRainingAt(pos) &&
|
||||||
|
state.getBlock() instanceof CropBlock) {
|
||||||
|
|
||||||
|
// Get the block's drops
|
||||||
|
List<ItemStack> drops = Block.getDrops(
|
||||||
|
state,
|
||||||
|
(ServerLevel) level,
|
||||||
|
pos,
|
||||||
|
null, // no block entity
|
||||||
|
ctx.getPlayer(),
|
||||||
|
ctx.getItemInHand()
|
||||||
|
);
|
||||||
|
|
||||||
|
// Multiply each drop's count by 2 and spawn them
|
||||||
|
for (ItemStack drop : drops) {
|
||||||
|
ItemStack doubled = drop.copy();
|
||||||
|
doubled.setCount(drop.getCount() * 2);
|
||||||
|
Block.popResource(level, pos, doubled);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Destroy the block
|
||||||
|
level.setBlock(pos, Blocks.AIR.defaultBlockState(), 3);
|
||||||
|
|
||||||
|
return InteractionResult.SUCCESS;
|
||||||
|
}
|
||||||
|
List<Player> comrades = level.getEntitiesOfClass(
|
||||||
|
Player.class,
|
||||||
|
player.getBoundingBox().inflate(8),
|
||||||
|
p -> p instanceof ServerPlayer && player.isAlliedTo(p)
|
||||||
|
);
|
||||||
|
RandomSource rand = level.getRandom();
|
||||||
|
if (rand.nextFloat() < 0.5f) {
|
||||||
|
for (ItemStack drop : Block.getDrops(state, (ServerLevel) level, pos, null)) {
|
||||||
|
for (Player buddy : comrades) {
|
||||||
|
buddy.addItem(drop.copy());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return super.useOn(ctx); // fallback to normal hoe behavior
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user