61 lines
2.2 KiB
Java
61 lines
2.2 KiB
Java
/*
|
|
* Decompiled with CFR 0.152.
|
|
*/
|
|
package net.minecraft.world.item;
|
|
|
|
import java.util.List;
|
|
import net.minecraft.core.BlockPos;
|
|
import net.minecraft.tags.BlockTags;
|
|
import net.minecraft.world.InteractionResult;
|
|
import net.minecraft.world.entity.Leashable;
|
|
import net.minecraft.world.entity.decoration.LeashFenceKnotEntity;
|
|
import net.minecraft.world.entity.player.Player;
|
|
import net.minecraft.world.item.Item;
|
|
import net.minecraft.world.item.context.UseOnContext;
|
|
import net.minecraft.world.level.Level;
|
|
import net.minecraft.world.level.block.state.BlockState;
|
|
import net.minecraft.world.level.gameevent.GameEvent;
|
|
import net.minecraft.world.phys.Vec3;
|
|
|
|
public class LeadItem
|
|
extends Item {
|
|
public LeadItem(Item.Properties properties) {
|
|
super(properties);
|
|
}
|
|
|
|
@Override
|
|
public InteractionResult useOn(UseOnContext context) {
|
|
BlockPos pos;
|
|
Level level = context.getLevel();
|
|
BlockState state = level.getBlockState(pos = context.getClickedPos());
|
|
if (state.is(BlockTags.FENCES)) {
|
|
Player player = context.getPlayer();
|
|
if (!level.isClientSide() && player != null) {
|
|
return LeadItem.bindPlayerMobs(player, level, pos);
|
|
}
|
|
}
|
|
return InteractionResult.PASS;
|
|
}
|
|
|
|
public static InteractionResult bindPlayerMobs(Player player, Level level, BlockPos pos) {
|
|
LeashFenceKnotEntity activeKnot = null;
|
|
List<Leashable> entitiesToLeash = Leashable.leashableInArea(level, Vec3.atCenterOf(pos), l -> l.getLeashHolder() == player);
|
|
boolean anyLeashed = false;
|
|
for (Leashable leashable : entitiesToLeash) {
|
|
if (activeKnot == null) {
|
|
activeKnot = LeashFenceKnotEntity.getOrCreateKnot(level, pos);
|
|
activeKnot.playPlacementSound();
|
|
}
|
|
if (!leashable.canHaveALeashAttachedTo(activeKnot)) continue;
|
|
leashable.setLeashedTo(activeKnot, true);
|
|
anyLeashed = true;
|
|
}
|
|
if (anyLeashed) {
|
|
level.gameEvent(GameEvent.BLOCK_ATTACH, pos, GameEvent.Context.of(player));
|
|
return InteractionResult.SUCCESS_SERVER;
|
|
}
|
|
return InteractionResult.PASS;
|
|
}
|
|
}
|
|
|