2020-05-20 14:55:59 +02:00
|
|
|
package de.ellpeck.naturesaura.items;
|
|
|
|
|
|
|
|
import de.ellpeck.naturesaura.entities.EntityStructureFinder;
|
|
|
|
import de.ellpeck.naturesaura.entities.ModEntities;
|
2022-03-04 15:59:04 +01:00
|
|
|
import net.minecraft.core.HolderSet;
|
2022-09-15 15:59:32 +02:00
|
|
|
import net.minecraft.core.Registry;
|
|
|
|
import net.minecraft.resources.ResourceKey;
|
2021-12-05 23:32:31 +01:00
|
|
|
import net.minecraft.server.level.ServerLevel;
|
|
|
|
import net.minecraft.world.InteractionHand;
|
2021-12-15 16:24:53 +01:00
|
|
|
import net.minecraft.world.InteractionResult;
|
2021-12-05 23:32:31 +01:00
|
|
|
import net.minecraft.world.InteractionResultHolder;
|
|
|
|
import net.minecraft.world.entity.player.Player;
|
|
|
|
import net.minecraft.world.item.ItemStack;
|
|
|
|
import net.minecraft.world.level.Level;
|
2022-06-27 15:24:04 +02:00
|
|
|
import net.minecraft.world.level.levelgen.structure.Structure;
|
2020-05-20 14:55:59 +02:00
|
|
|
|
|
|
|
public class ItemStructureFinder extends ItemImpl {
|
2021-12-05 23:32:31 +01:00
|
|
|
|
2022-09-15 15:59:32 +02:00
|
|
|
private final ResourceKey<Structure> structure;
|
2020-05-20 14:55:59 +02:00
|
|
|
private final int color;
|
2021-01-14 23:42:53 +01:00
|
|
|
private final int radius;
|
2020-05-20 14:55:59 +02:00
|
|
|
|
2022-09-15 15:59:32 +02:00
|
|
|
public ItemStructureFinder(String baseName, ResourceKey<Structure> structure, int color, int radius) {
|
2020-05-20 14:55:59 +02:00
|
|
|
super(baseName);
|
2022-06-27 15:24:04 +02:00
|
|
|
this.structure = structure;
|
2020-05-20 14:55:59 +02:00
|
|
|
this.color = color;
|
2021-01-14 23:42:53 +01:00
|
|
|
this.radius = radius;
|
2020-05-20 14:55:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-05 23:32:31 +01:00
|
|
|
public InteractionResultHolder<ItemStack> use(Level levelIn, Player playerIn, InteractionHand handIn) {
|
2021-12-15 16:30:22 +01:00
|
|
|
var stack = playerIn.getItemInHand(handIn);
|
2022-06-27 15:24:04 +02:00
|
|
|
if (!levelIn.isClientSide && ((ServerLevel) levelIn).structureManager().shouldGenerateStructures()) {
|
2022-09-15 15:59:32 +02:00
|
|
|
var registry = levelIn.registryAccess().registryOrThrow(Registry.STRUCTURE_REGISTRY);
|
|
|
|
var holderSet = registry.getHolder(this.structure).map(HolderSet::direct).orElse(null);
|
|
|
|
if (holderSet != null) {
|
|
|
|
var pos = ((ServerLevel) levelIn).getChunkSource().getGenerator().findNearestMapStructure((ServerLevel) levelIn, holderSet, playerIn.blockPosition(), this.radius, false);
|
|
|
|
if (pos != null) {
|
|
|
|
var entity = new EntityStructureFinder(ModEntities.STRUCTURE_FINDER, levelIn);
|
|
|
|
entity.setPos(playerIn.getX(), playerIn.getY(0.5D), playerIn.getZ());
|
|
|
|
entity.setItem(stack);
|
|
|
|
entity.getEntityData().set(EntityStructureFinder.COLOR, this.color);
|
|
|
|
entity.signalTo(pos.getFirst().above(64));
|
|
|
|
levelIn.addFreshEntity(entity);
|
2020-05-20 14:55:59 +02:00
|
|
|
|
2022-09-15 15:59:32 +02:00
|
|
|
stack.shrink(1);
|
|
|
|
}
|
2020-05-20 14:55:59 +02:00
|
|
|
}
|
|
|
|
}
|
2021-12-15 16:24:53 +01:00
|
|
|
return new InteractionResultHolder<>(InteractionResult.SUCCESS, stack);
|
2020-05-20 14:55:59 +02:00
|
|
|
}
|
|
|
|
}
|