9
0
mirror of https://github.com/LeavesMC/Leaves.git synced 2025-12-30 04:19:30 +00:00
* Fix #690

* Clean up

* Remove useless check

* 1 long line

* Reformat
This commit is contained in:
Lumine1909
2025-08-14 06:46:54 -07:00
committed by GitHub
parent bb5a1dfff0
commit d87bc544d5
6 changed files with 156 additions and 141 deletions

View File

@@ -0,0 +1,124 @@
package org.leavesmc.leaves.util;
import net.minecraft.Util;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.*;
import net.minecraft.world.level.block.piston.PistonBaseBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.block.state.properties.RailShape;
import org.leavesmc.leaves.LeavesConfig;
import java.util.List;
public class ShearsWrenchUtil {
public static InteractionResult tryApplyRotate(UseOnContext context, BlockState blockState) {
Level level = context.getLevel();
BlockPos clickedPos = context.getClickedPos();
Block block = blockState.getBlock();
if (!LeavesConfig.modify.redstoneShearsWrench || !(block instanceof ObserverBlock || block instanceof DispenserBlock || block instanceof PistonBaseBlock || block instanceof HopperBlock || block instanceof RepeaterBlock || block instanceof ComparatorBlock || block instanceof CrafterBlock || block instanceof LeverBlock || block instanceof CocoaBlock || block instanceof TrapDoorBlock || block instanceof FenceGateBlock || block instanceof LightningRodBlock || block instanceof CalibratedSculkSensorBlock || block instanceof BaseRailBlock)) {
return null;
}
StateDefinition<Block, BlockState> blockstatelist = block.getStateDefinition();
Property<?> iblockstate;
if (block instanceof CrafterBlock) {
iblockstate = blockstatelist.getProperty("orientation");
} else if (block instanceof BaseRailBlock) {
iblockstate = blockstatelist.getProperty("shape");
} else {
iblockstate = blockstatelist.getProperty("facing");
}
Player player = context.getPlayer();
if (iblockstate == null || player == null) {
return InteractionResult.FAIL;
}
if (block instanceof BaseRailBlock) {
if (block instanceof RailBlock) {
if (blockState.getValue(RailBlock.SHAPE).isSlope()) {
return InteractionResult.FAIL;
}
} else {
if (getNameHelper(blockState, PoweredRailBlock.POWERED).equals("true")) {
return InteractionResult.FAIL;
}
if (blockState.getValue(PoweredRailBlock.SHAPE).isSlope()) {
return InteractionResult.FAIL;
}
}
}
if (block instanceof PistonBaseBlock) {
if (getNameHelper(blockState, PistonBaseBlock.EXTENDED).equals("true")) {
return InteractionResult.FAIL;
}
}
if (block instanceof RepeaterBlock || block instanceof ComparatorBlock) {
if (getNameHelper(blockState, ComparatorBlock.POWERED).equals("true")) {
return InteractionResult.FAIL;
}
if (block instanceof RepeaterBlock) {
if (getNameHelper(blockState, RepeaterBlock.LOCKED).equals("true")) {
return InteractionResult.FAIL;
}
}
}
if (block instanceof CrafterBlock) {
if (getNameHelper(blockState, CrafterBlock.CRAFTING).equals("true")) {
return InteractionResult.FAIL;
}
}
BlockState iblockdata1 = cycleState(blockState, iblockstate, player.isSecondaryUseActive());
level.setBlock(clickedPos, iblockdata1, 18);
message(player, Component.translatable("item.minecraft.debug_stick.update", iblockstate.getName(), getNameHelper(iblockdata1, iblockstate)));
return InteractionResult.CONSUME;
}
public static boolean shouldSkipPlace(BlockPlaceContext context) {
return LeavesConfig.modify.redstoneShearsWrench &&
context.getPlayer() != null &&
context.getPlayer().getItemInHand(InteractionHand.MAIN_HAND).is(Items.SHEARS);
}
private static <T extends Comparable<T>> BlockState cycleState(BlockState state, Property<T> property, boolean inverse) {
List<T> possibleValues = property.getPossibleValues();
if (possibleValues.getFirst() instanceof RailShape) {
boolean isRailBlock = state.getBlock() instanceof RailBlock;
possibleValues = possibleValues.stream().filter(possibleValue -> {
RailShape shape = (RailShape) possibleValue;
if (isRailBlock) {
return !shape.isSlope();
}
return !shape.isSlope() && shape != RailShape.NORTH_EAST && shape != RailShape.NORTH_WEST && shape != RailShape.SOUTH_EAST && shape != RailShape.SOUTH_WEST;
}).toList();
}
return state.setValue(property, getRelative(possibleValues, state.getValue(property), inverse)); // CraftBukkit - decompile error
}
private static <T> T getRelative(Iterable<T> elements, T current, boolean inverse) {
return inverse ? Util.findPreviousInIterable(elements, current) : Util.findNextInIterable(elements, current);
}
private static void message(Player player, Component message) {
((ServerPlayer) player).sendSystemMessage(message, true);
}
private static <T extends Comparable<T>> String getNameHelper(BlockState state, Property<T> property) {
return property.getName(state.getValue(property));
}
}