NaturesAura/src/main/java/de/ellpeck/naturesaura/items/ItemStructureFinder.java

47 lines
2 KiB
Java
Raw Normal View History

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;
2021-12-04 15:40:09 +01:00
import net.minecraft.entity.player.Player;
2020-05-20 14:55:59 +02:00
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
2021-12-04 15:40:09 +01:00
import net.minecraft.util.InteractionResult;
2020-05-20 14:55:59 +02:00
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
2021-12-04 15:40:09 +01:00
import net.minecraft.level.Level;
import net.minecraft.level.gen.feature.structure.Structure;
import net.minecraft.level.server.ServerLevel;
2020-05-20 14:55:59 +02:00
public class ItemStructureFinder extends ItemImpl {
2020-09-22 03:17:02 +02:00
private final Structure structureName;
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
2021-01-14 23:42:53 +01:00
public ItemStructureFinder(String baseName, Structure structureName, int color, int radius) {
2020-05-20 14:55:59 +02:00
super(baseName);
this.structureName = structureName;
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-04 15:40:09 +01:00
public ActionResult<ItemStack> onItemRightClick(Level levelIn, Player playerIn, Hand handIn) {
2020-05-20 14:55:59 +02:00
ItemStack stack = playerIn.getHeldItem(handIn);
2021-12-04 15:40:09 +01:00
// ServerLevel.getStructureManager().doesGenerateFeatures()
if (!levelIn.isClientSide && ((ServerLevel) levelIn).func_241112_a_().func_235005_a_()) {
BlockPos pos = ((ServerLevel) levelIn).getChunkProvider().getChunkGenerator().func_235956_a_((ServerLevel) levelIn, this.structureName, playerIn.getPosition(), this.radius, false);
2020-05-20 14:55:59 +02:00
if (pos != null) {
2021-12-04 15:40:09 +01:00
EntityStructureFinder entity = new EntityStructureFinder(ModEntities.STRUCTURE_FINDER, levelIn);
2020-05-20 14:55:59 +02:00
entity.setPosition(playerIn.getPosX(), playerIn.getPosYHeight(0.5D), playerIn.getPosZ());
entity.func_213863_b(stack);
entity.getDataManager().set(EntityStructureFinder.COLOR, this.color);
entity.moveTowards(pos.up(64));
2021-12-04 15:40:09 +01:00
levelIn.addEntity(entity);
2020-05-20 14:55:59 +02:00
stack.shrink(1);
}
}
2021-12-04 15:40:09 +01:00
return new ActionResult<>(InteractionResult.SUCCESS, stack);
2020-05-20 14:55:59 +02:00
}
}