added shears harvest any living items...

This commit is contained in:
Jottyfan
2026-07-26 18:15:17 +02:00
parent 90a469534a
commit 75925e4d55
@@ -1,6 +1,7 @@
package de.jottyfan.minecraft.item; package de.jottyfan.minecraft.item;
import net.minecraft.core.component.DataComponents; import net.minecraft.core.component.DataComponents;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult; import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.LivingEntity;
@@ -15,6 +16,10 @@ import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items; import net.minecraft.world.item.Items;
import net.minecraft.world.item.ShearsItem; import net.minecraft.world.item.ShearsItem;
import net.minecraft.world.level.storage.loot.LootParams;
import net.minecraft.world.level.storage.loot.LootTable;
import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets;
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
import net.minecraft.world.phys.Vec3; import net.minecraft.world.phys.Vec3;
/** /**
@@ -79,6 +84,20 @@ public class QShears extends ShearsItem {
user.level() user.level()
.addFreshEntity(new ItemEntity(user.level(), pos.x, pos.y, pos.z, new ItemStack(Items.FEATHER, amount))); .addFreshEntity(new ItemEntity(user.level(), pos.x, pos.y, pos.z, new ItemStack(Items.FEATHER, amount)));
return InteractionResult.SUCCESS; return InteractionResult.SUCCESS;
} else if (user.level() instanceof ServerLevel serverLevel){
if (entity.getLootTable().isPresent()) {
LootTable lootTable = serverLevel.getServer().reloadableRegistries().getLootTable(entity.getLootTable().get());
LootParams lootParams = new LootParams.Builder(serverLevel)
.withParameter(LootContextParams.ORIGIN, entity.position())
.withParameter(LootContextParams.THIS_ENTITY, entity)
.withParameter(LootContextParams.DAMAGE_SOURCE, entity.damageSources().generic())
.create(LootContextParamSets.ENTITY);
lootTable.getRandomItems(lootParams).forEach(itemStack -> {
entity.spawnAtLocation(serverLevel, itemStack);
});
entity.hurtServer(serverLevel, entity.damageSources().generic(), 0.1f);
return InteractionResult.SUCCESS;
}
} }
return InteractionResult.PASS; return InteractionResult.PASS;
} }