Skip to content

Commit 671deac

Browse files
committed
Provide better support for some of the wrench abilities in our blocks, and don't allow rotating certain blocks when not on rotate mode
Also fixed certain blocks throwing an exception when attempting to rotate while facing up or down
1 parent 3e09163 commit 671deac

File tree

5 files changed

+98
-73
lines changed

5 files changed

+98
-73
lines changed

src/main/java/mekanism/common/block/basic/BlockLogisticalSorter.java

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package mekanism.common.block.basic;
22

3-
import mekanism.api.radiation.IRadiationManager;
4-
import mekanism.api.security.IBlockSecurityUtils;
53
import mekanism.common.block.attribute.Attribute;
64
import mekanism.common.block.prefab.BlockTile.BlockTileModel;
75
import mekanism.common.content.blocktype.Machine;
@@ -10,19 +8,13 @@
108
import mekanism.common.tile.TileEntityLogisticalSorter;
119
import mekanism.common.util.EnumUtils;
1210
import mekanism.common.util.InventoryUtils;
13-
import mekanism.common.util.MekanismUtils;
1411
import mekanism.common.util.WorldUtils;
1512
import net.minecraft.core.BlockPos;
1613
import net.minecraft.core.Direction;
17-
import net.minecraft.world.InteractionHand;
18-
import net.minecraft.world.ItemInteractionResult;
19-
import net.minecraft.world.entity.player.Player;
20-
import net.minecraft.world.item.ItemStack;
2114
import net.minecraft.world.item.context.BlockPlaceContext;
2215
import net.minecraft.world.level.Level;
2316
import net.minecraft.world.level.LevelAccessor;
2417
import net.minecraft.world.level.block.state.BlockState;
25-
import net.minecraft.world.phys.BlockHitResult;
2618
import org.jetbrains.annotations.NotNull;
2719
import org.jetbrains.annotations.Nullable;
2820

@@ -58,50 +50,6 @@ public BlockState getStateForPlacement(@NotNull BlockPlaceContext context) {
5850
return state;
5951
}
6052

61-
@NotNull
62-
@Override
63-
protected ItemInteractionResult useItemOn(@NotNull ItemStack stack, @NotNull BlockState state, @NotNull Level world, @NotNull BlockPos pos, @NotNull Player player,
64-
@NotNull InteractionHand hand, @NotNull BlockHitResult hit) {
65-
if (stack.isEmpty()) {
66-
return ItemInteractionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION;
67-
}
68-
TileEntityLogisticalSorter tile = WorldUtils.getTileEntity(TileEntityLogisticalSorter.class, world, pos);
69-
if (tile == null) {
70-
//No tile, we can just skip trying to use without an item
71-
return ItemInteractionResult.SKIP_DEFAULT_BLOCK_INTERACTION;
72-
} else if (world.isClientSide) {
73-
return genericClientActivated(stack, tile);
74-
}
75-
//TODO: Make this be moved into the logistical sorter tile
76-
if (MekanismUtils.canUseAsWrench(stack)) {
77-
if (!IBlockSecurityUtils.INSTANCE.canAccessOrDisplayError(player, world, pos, tile)) {
78-
return ItemInteractionResult.FAIL;
79-
}
80-
if (player.isShiftKeyDown()) {
81-
if (IRadiationManager.INSTANCE.isRadiationEnabled() && tile.getRadiationScale() > 0) {
82-
//Note: This should always be false for the logistical sorter, but we keep it here for good measure
83-
return ItemInteractionResult.FAIL;
84-
}
85-
WorldUtils.dismantleBlock(state, world, pos, player, stack);
86-
return ItemInteractionResult.SUCCESS;
87-
}
88-
Direction change = tile.getDirection().getClockWise();
89-
if (!tile.hasConnectedInventory()) {
90-
for (Direction dir : EnumUtils.DIRECTIONS) {
91-
Direction opposite = dir.getOpposite();
92-
if (InventoryUtils.isItemHandler(world, pos.relative(dir), opposite)) {
93-
change = opposite;
94-
break;
95-
}
96-
}
97-
}
98-
tile.setFacing(change);
99-
world.updateNeighborsAt(pos, this);
100-
return ItemInteractionResult.SUCCESS;
101-
}
102-
return ItemInteractionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION;
103-
}
104-
10553
@NotNull
10654
@Override
10755
protected BlockState updateShape(BlockState state, @NotNull Direction dir, @NotNull BlockState facingState, @NotNull LevelAccessor world, @NotNull BlockPos pos,

src/main/java/mekanism/common/block/prefab/BlockTile.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ public TileEntityTypeRegistryObject<TILE> getTileType() {
5252
@Override
5353
protected ItemInteractionResult useItemOn(@NotNull ItemStack stack, @NotNull BlockState state, @NotNull Level world, @NotNull BlockPos pos, @NotNull Player player,
5454
@NotNull InteractionHand hand, @NotNull BlockHitResult hit) {
55+
if (stack.isEmpty()) {
56+
return ItemInteractionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION;
57+
}
5558
TileEntityMekanism tile = WorldUtils.getTileEntity(TileEntityMekanism.class, world, pos);
5659
if (tile == null) {
5760
//No tile, we can just skip trying to use without an item

src/main/java/mekanism/common/tile/TileEntityLogisticalSorter.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import java.util.Collection;
44
import java.util.List;
55
import mekanism.api.IContentsListener;
6-
import mekanism.api.SerializationConstants;
76
import mekanism.api.RelativeSide;
7+
import mekanism.api.SerializationConstants;
88
import mekanism.api.text.EnumColor;
99
import mekanism.client.sound.SoundHandler;
1010
import mekanism.common.attachments.containers.ContainerType;
@@ -30,7 +30,10 @@
3030
import mekanism.common.registries.MekanismBlocks;
3131
import mekanism.common.registries.MekanismDataComponents;
3232
import mekanism.common.tile.base.TileEntityMekanism;
33+
import mekanism.common.tile.base.WrenchResult;
3334
import mekanism.common.tile.interfaces.ITileFilterHolder;
35+
import mekanism.common.util.EnumUtils;
36+
import mekanism.common.util.InventoryUtils;
3437
import mekanism.common.util.MekanismUtils;
3538
import mekanism.common.util.NBTUtils;
3639
import mekanism.common.util.TransporterUtils;
@@ -43,6 +46,7 @@
4346
import net.minecraft.nbt.CompoundTag;
4447
import net.minecraft.nbt.Tag;
4548
import net.minecraft.server.level.ServerLevel;
49+
import net.minecraft.world.entity.player.Player;
4650
import net.minecraft.world.item.Item;
4751
import net.minecraft.world.item.ItemStack;
4852
import net.minecraft.world.level.block.entity.BlockEntity;
@@ -293,6 +297,23 @@ public boolean supportsMode(RedstoneControl mode) {
293297
return true;
294298
}
295299

300+
@Override
301+
protected WrenchResult tryWrenchRotate(BlockState state, Player player, ItemStack stack) {
302+
Direction change = MekanismUtils.rotate(getDirection(), true);
303+
if (!hasConnectedInventory()) {
304+
for (Direction dir : EnumUtils.DIRECTIONS) {
305+
Direction opposite = dir.getOpposite();
306+
if (InventoryUtils.isItemHandler(level, worldPosition.relative(dir), opposite)) {
307+
change = opposite;
308+
break;
309+
}
310+
}
311+
}
312+
setFacing(change);
313+
level.updateNeighborsAt(worldPosition, state.getBlock());
314+
return WrenchResult.SUCCESS;
315+
}
316+
296317
@Override
297318
public void writeSustainedData(HolderLookup.Provider provider, CompoundTag dataMap) {
298319
super.writeSustainedData(provider, dataMap);

src/main/java/mekanism/common/tile/base/TileEntityMekanism.java

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import mekanism.api.Action;
1515
import mekanism.api.IConfigCardAccess;
1616
import mekanism.api.IContentsListener;
17+
import mekanism.api.MekanismItemAbilities;
1718
import mekanism.api.SerializationConstants;
1819
import mekanism.api.Upgrade;
1920
import mekanism.api.chemical.ChemicalStack;
@@ -97,6 +98,7 @@
9798
import mekanism.common.lib.security.BlockSecurityUtils;
9899
import mekanism.common.lib.security.ISecurityTile;
99100
import mekanism.common.registries.MekanismDataComponents;
101+
import mekanism.common.tags.MekanismTags;
100102
import mekanism.common.tile.component.ITileComponent;
101103
import mekanism.common.tile.component.TileComponentConfig;
102104
import mekanism.common.tile.component.TileComponentSecurity;
@@ -138,6 +140,7 @@
138140
import net.minecraft.world.level.block.Block;
139141
import net.minecraft.world.level.block.entity.BlockEntity;
140142
import net.minecraft.world.level.block.state.BlockState;
143+
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
141144
import net.neoforged.neoforge.capabilities.BlockCapabilityCache;
142145
import net.neoforged.neoforge.fluids.FluidStack;
143146
import org.jetbrains.annotations.NotNull;
@@ -515,26 +518,57 @@ protected void notifyComparatorChange() {
515518
level.updateNeighbourForOutputSignal(worldPosition, getBlockType());
516519
}
517520

521+
protected WrenchResult tryWrenchDismantle(BlockState state, Player player, ItemStack stack) {
522+
if (player.isShiftKeyDown()) {
523+
if (IRadiationManager.INSTANCE.isRadiationEnabled() && getRadiationScale() > 0) {
524+
//Don't allow dismantling radioactive blocks
525+
return WrenchResult.RADIOACTIVE;
526+
}
527+
WorldUtils.dismantleBlock(state, getLevel(), worldPosition, this, player, stack);
528+
return WrenchResult.DISMANTLED;
529+
}
530+
return WrenchResult.PASS;
531+
}
532+
533+
protected WrenchResult tryWrenchRotate(BlockState state, Player player, ItemStack stack) {
534+
//Special ITileDirectional handling
535+
if (isDirectional()) {
536+
AttributeStateFacing attribute = Attribute.getOrThrow(getBlockType(), AttributeStateFacing.class);
537+
if (attribute.canRotate()) {
538+
setFacing(MekanismUtils.rotate(getDirection(), attribute.getFacingProperty() == BlockStateProperties.FACING));
539+
return WrenchResult.SUCCESS;
540+
}
541+
}
542+
return WrenchResult.PASS;
543+
}
544+
518545
public WrenchResult tryWrench(BlockState state, Player player, ItemStack stack) {
519-
if (MekanismUtils.canUseAsWrench(stack)) {
546+
if (stack.isEmpty()) {
547+
return WrenchResult.PASS;
548+
}
549+
WrenchResult result = WrenchResult.PASS;
550+
boolean canRotate = stack.canPerformAction(MekanismItemAbilities.WRENCH_ROTATE);
551+
boolean canDismantle = stack.canPerformAction(MekanismItemAbilities.WRENCH_DISMANTLE);
552+
if (!canRotate && !canDismantle) {
553+
if (stack.canPerformAction(MekanismItemAbilities.WRENCH_EMPTY) || stack.canPerformAction(MekanismItemAbilities.WRENCH_CONFIGURE)) {
554+
//The stack provides some wrench actions, it is likely intentional that it can't rotate or dismantle blocks
555+
return result;
556+
}
557+
//If the item doesn't explicitly declare the ability to rotate or dismantle,
558+
// then mark that it can do both if it is in the configurator tag
559+
canRotate = canDismantle = stack.is(MekanismTags.Items.CONFIGURATORS);
560+
}
561+
if (canRotate || canDismantle) {
520562
if (hasSecurity() && !IBlockSecurityUtils.INSTANCE.canAccessOrDisplayError(player, getWorldNN(), worldPosition, this)) {
521563
return WrenchResult.NO_SECURITY;
564+
} else if (canDismantle) {
565+
result = tryWrenchDismantle(state, player, stack);
522566
}
523-
if (player.isShiftKeyDown()) {
524-
if (IRadiationManager.INSTANCE.isRadiationEnabled() && getRadiationScale() > 0) {
525-
//Don't allow dismantling radioactive blocks
526-
return WrenchResult.RADIOACTIVE;
527-
}
528-
WorldUtils.dismantleBlock(state, getLevel(), worldPosition, this, player, stack);
529-
return WrenchResult.DISMANTLED;
530-
}
531-
//Special ITileDirectional handling
532-
if (isDirectional() && Attribute.getOrThrow(getBlockType(), AttributeStateFacing.class).canRotate()) {
533-
setFacing(getDirection().getClockWise());
567+
if (result == WrenchResult.PASS && canRotate) {
568+
result = tryWrenchRotate(state, player, stack);
534569
}
535-
return WrenchResult.SUCCESS;
536570
}
537-
return WrenchResult.PASS;
571+
return result;
538572
}
539573

540574
public InteractionResult openGui(Player player) {

src/main/java/mekanism/common/util/MekanismUtils.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import mekanism.api.AutomationType;
2121
import mekanism.api.MekanismAPI;
2222
import mekanism.api.MekanismAPITags;
23+
import mekanism.api.MekanismItemAbilities;
2324
import mekanism.api.Upgrade;
2425
import mekanism.api.chemical.IChemicalTank;
2526
import mekanism.api.energy.IEnergyContainer;
@@ -34,8 +35,6 @@
3435
import mekanism.common.block.attribute.Attribute;
3536
import mekanism.common.block.attribute.AttributeFactoryType;
3637
import mekanism.common.config.MekanismConfig;
37-
import mekanism.common.item.ItemConfigurator;
38-
import mekanism.common.item.ItemConfigurator.ConfiguratorMode;
3938
import mekanism.common.lib.frequency.Frequency.FrequencyIdentity;
4039
import mekanism.common.lib.frequency.IFrequencyItem;
4140
import mekanism.common.registries.MekanismDataComponents;
@@ -206,6 +205,20 @@ public static Direction getRight(Direction orientation) {
206205
return orientation.getCounterClockWise();
207206
}
208207

208+
public static Direction rotate(Direction orientation, boolean supportY) {
209+
if (supportY) {
210+
return switch (orientation) {
211+
case UP -> Direction.NORTH;
212+
case DOWN -> Direction.SOUTH;
213+
case NORTH -> Direction.EAST;
214+
case SOUTH -> Direction.WEST;
215+
case WEST -> Direction.UP;
216+
case EAST -> Direction.DOWN;
217+
};
218+
}
219+
return orientation.getClockWise();
220+
}
221+
209222
public static double fractionUpgrades(IUpgradeTile tile, Upgrade type) {
210223
if (tile.supportsUpgrade(type)) {
211224
return tile.getComponent().getUpgrades(type) / (double) type.getMax();
@@ -530,15 +543,21 @@ public static CraftingInput.Positioned getCraftingInputSlots(int width, int heig
530543
}
531544

532545
/**
533-
* Gets the wrench if the item is an IMekWrench, or a generic implementation if the item is in the forge wrenches tag
546+
* Checks if the stack can be used as a wrench for dismantling purposes
534547
*/
535548
public static boolean canUseAsWrench(ItemStack stack) {
536549
if (stack.isEmpty()) {
537550
return false;
538-
} else if (stack.getItem() instanceof ItemConfigurator configurator) {
539-
return configurator.getMode(stack) == ConfiguratorMode.WRENCH;
551+
} else if (stack.canPerformAction(MekanismItemAbilities.WRENCH_DISMANTLE)) {
552+
return true;
553+
} else if (stack.is(MekanismTags.Items.CONFIGURATORS)) {
554+
//If it is in the tag, validate it isn't exposing one of the other wrench action types, as then it probably wants to act
555+
// as only that type instead of as any type
556+
return !stack.canPerformAction(MekanismItemAbilities.WRENCH_ROTATE) &&
557+
!stack.canPerformAction(MekanismItemAbilities.WRENCH_EMPTY) &&
558+
!stack.canPerformAction(MekanismItemAbilities.WRENCH_CONFIGURE);
540559
}
541-
return stack.is(MekanismTags.Items.CONFIGURATORS);
560+
return false;
542561
}
543562

544563
@NotNull

0 commit comments

Comments
 (0)