minecraft-source/src/net/minecraft/world/item/DebugStickItem.java

100 lines
4.5 KiB
Java

package net.minecraft.world.item;
import net.minecraft.world.level.block.state.AbstractStateHolder;
import net.minecraft.network.chat.ChatType;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.Util;
import javax.annotation.Nullable;
import net.minecraft.nbt.CompoundTag;
import java.util.Collection;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.core.Registry;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
public class DebugStickItem extends Item {
public DebugStickItem(final Properties a) {
super(a);
}
@Override
public boolean isFoil(final ItemStack bek) {
return true;
}
@Override
public boolean canAttackBlock(final BlockState byg, final Level bjt, final BlockPos fk, final Player ayg) {
if (!bjt.isClientSide) {
this.handleInteraction(ayg, byg, bjt, fk, false, ayg.getItemInHand(InteractionHand.MAIN_HAND));
}
return false;
}
@Override
public InteractionResult useOn(final UseOnContext bfw) {
final Player ayg3 = bfw.getPlayer();
final Level bjt4 = bfw.getLevel();
if (!bjt4.isClientSide && ayg3 != null) {
final BlockPos fk5 = bfw.getClickedPos();
this.handleInteraction(ayg3, bjt4.getBlockState(fk5), bjt4, fk5, true, bfw.getItemInHand());
}
return InteractionResult.SUCCESS;
}
private void handleInteraction(final Player ayg, final BlockState byg, final LevelAccessor bju, final BlockPos fk, final boolean boolean5, final ItemStack bek) {
if (!ayg.canUseGameMasterBlocks()) {
return;
}
final Block bpe8 = byg.getBlock();
final StateDefinition<Block, BlockState> byh9 = bpe8.getStateDefinition();
final Collection<Property<?>> collection10 = byh9.getProperties();
final String string11 = Registry.BLOCK.getKey(bpe8).toString();
if (collection10.isEmpty()) {
message(ayg, new TranslatableComponent(this.getDescriptionId() + ".empty", new Object[] { string11 }));
return;
}
final CompoundTag jt12 = bek.getOrCreateTagElement("DebugProperty");
final String string12 = jt12.getString(string11);
Property<?> bzj14 = byh9.getProperty(string12);
if (boolean5) {
if (bzj14 == null) {
bzj14 = collection10.iterator().next();
}
final BlockState byg2 = DebugStickItem.cycleState(byg, bzj14, ayg.isSecondaryUseActive());
bju.setBlock(fk, byg2, 18);
message(ayg, new TranslatableComponent(this.getDescriptionId() + ".update", new Object[] { bzj14.getName(), DebugStickItem.getNameHelper(byg2, bzj14) }));
}
else {
bzj14 = DebugStickItem.<Property<?>>getRelative(collection10, bzj14, ayg.isSecondaryUseActive());
final String string13 = bzj14.getName();
jt12.putString(string11, string13);
message(ayg, new TranslatableComponent(this.getDescriptionId() + ".select", new Object[] { string13, DebugStickItem.getNameHelper(byg, bzj14) }));
}
}
private static <T extends Comparable<T>> BlockState cycleState(final BlockState byg, final Property<T> bzj, final boolean boolean3) {
return ((AbstractStateHolder<O, BlockState>)byg).<T, Comparable>setValue(bzj, (Comparable)DebugStickItem.<V>getRelative((Iterable<V>)bzj.getPossibleValues(), (V)byg.<T>getValue((Property<T>)bzj), boolean3));
}
private static <T> T getRelative(final Iterable<T> iterable, @Nullable final T object, final boolean boolean3) {
return boolean3 ? Util.<T>findPreviousInIterable(iterable, object) : Util.<T>findNextInIterable(iterable, object);
}
private static void message(final Player ayg, final Component lf) {
((ServerPlayer)ayg).sendMessage(lf, ChatType.GAME_INFO);
}
private static <T extends Comparable<T>> String getNameHelper(final BlockState byg, final Property<T> bzj) {
return bzj.getName(byg.<T>getValue(bzj));
}
}