Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.20.1 Backport] Backport some QoL improvements for wrench (configurator). #8298

Open
wants to merge 3 commits into
base: 1.20.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import mekanism.common.block.prefab.BlockTile.BlockTileModel;
import mekanism.common.content.blocktype.BlockTypeTile;
import mekanism.common.tile.base.WrenchResult;
import mekanism.common.util.WorldUtils;
import mekanism.generators.common.item.ItemTurbineBlade;
import mekanism.generators.common.registries.GeneratorsBlockTypes;
Expand Down Expand Up @@ -34,9 +33,11 @@ public InteractionResult use(@NotNull BlockState state, @NotNull Level world, @N
if (tile == null) {
return InteractionResult.PASS;
} else if (world.isClientSide) {
return genericClientActivated(player, hand);
} else if (tile.tryWrench(state, player, hand, hit) != WrenchResult.PASS) {
return InteractionResult.SUCCESS;
return genericClientActivated(player, hand, tile);
}
InteractionResult wrenchResult = tile.tryWrench(state, player, hand, hit).getInteractionResult();
if (wrenchResult != InteractionResult.PASS) {
return wrenchResult;
}
ItemStack stack = player.getItemInHand(hand);
if (!player.isShiftKeyDown()) {
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/mekanism/client/ClientRegistration.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
import mekanism.common.content.gear.shared.ModuleColorModulationUnit;
import mekanism.common.integration.MekanismHooks;
import mekanism.common.item.ItemConfigurationCard;
import mekanism.common.item.ItemConfigurator;
import mekanism.common.item.ItemCraftingFormula;
import mekanism.common.item.block.ItemBlockCardboardBox;
import mekanism.common.item.block.machine.ItemBlockFluidTank;
Expand Down Expand Up @@ -263,6 +264,15 @@ public static void init(FMLClientSetupEvent event) {
});
ClientRegistrationUtil.setPropertyOverride(MekanismItems.CONFIGURATION_CARD, Mekanism.rl("encoded"),
(stack, world, entity, seed) -> ((ItemConfigurationCard) stack.getItem()).hasData(stack) ? 1 : 0);
ClientRegistrationUtil.setPropertyOverride(MekanismItems.CONFIGURATOR, Mekanism.rl("mode"), (stack, world, entity, seed) -> {
ItemConfigurator.ConfiguratorMode mode = ((ItemConfigurator) stack.getItem()).getMode(stack);
return switch (mode) {
default -> 0;
case EMPTY -> 1;
case ROTATE -> 2;
case WRENCH -> 3;
};
});

ClientRegistrationUtil.setPropertyOverride(MekanismItems.ELECTRIC_BOW, Mekanism.rl("pull"),
(stack, world, entity, seed) -> entity != null && entity.getUseItem() == stack ? (stack.getUseDuration() - entity.getUseItemRemainingTicks()) / 20.0F : 0);
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/mekanism/common/block/BlockMekanism.java
Original file line number Diff line number Diff line change
Expand Up @@ -431,8 +431,13 @@ public void animateTick(@NotNull BlockState state, @NotNull Level world, @NotNul
}
}

protected InteractionResult genericClientActivated(@NotNull Player player, @NotNull InteractionHand hand) {
if (Attribute.has(this, AttributeGui.class) || MekanismUtils.canUseAsWrench(player.getItemInHand(hand))) {
protected InteractionResult genericClientActivated(@NotNull Player player, @NotNull InteractionHand hand, BlockEntity blockEntity) {
if (Attribute.has(this, AttributeGui.class)) {
return InteractionResult.SUCCESS;
} else if (MekanismUtils.canUseAsWrench(player.getItemInHand(hand))) {
if (blockEntity instanceof ITileRadioactive tileRadioactive && tileRadioactive.getRadiationScale() > 0) {
return InteractionResult.FAIL;
}
return InteractionResult.SUCCESS;
}
return InteractionResult.PASS;
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/mekanism/common/block/basic/BlockBin.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import mekanism.common.content.blocktype.BlockTypeTile;
import mekanism.common.inventory.slot.BinInventorySlot;
import mekanism.common.tile.TileEntityBin;
import mekanism.common.tile.base.WrenchResult;
import mekanism.common.util.MekanismUtils;
import mekanism.common.util.WorldUtils;
import net.minecraft.core.BlockPos;
Expand Down Expand Up @@ -81,8 +80,10 @@ public InteractionResult use(@NotNull BlockState state, @NotNull Level world, @N
TileEntityBin bin = WorldUtils.getTileEntity(TileEntityBin.class, world, pos);
if (bin == null) {
return InteractionResult.PASS;
} else if (bin.tryWrench(state, player, hand, hit) != WrenchResult.PASS) {
return InteractionResult.SUCCESS;
}
InteractionResult wrenchResult = bin.tryWrench(state, player, hand, hit).getInteractionResult();
if (wrenchResult != InteractionResult.PASS) {
return wrenchResult;
}
ItemStack stack = player.getItemInHand(hand);
if (stack.isEmpty() && player.isShiftKeyDown() && hit.getDirection() == bin.getDirection()) {
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/mekanism/common/block/basic/BlockFluidTank.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import mekanism.common.content.blocktype.Machine;
import mekanism.common.resource.BlockResourceInfo;
import mekanism.common.tile.TileEntityFluidTank;
import mekanism.common.tile.base.WrenchResult;
import mekanism.common.util.FluidUtils;
import mekanism.common.util.WorldUtils;
import net.minecraft.core.BlockPos;
Expand Down Expand Up @@ -52,9 +51,11 @@ public InteractionResult use(@NotNull BlockState state, @NotNull Level world, @N
if (tile == null) {
return InteractionResult.PASS;
} else if (world.isClientSide) {
return genericClientActivated(player, hand);
} else if (tile.tryWrench(state, player, hand, hit) != WrenchResult.PASS) {
return InteractionResult.SUCCESS;
return genericClientActivated(player, hand, tile);
}
InteractionResult wrenchResult = tile.tryWrench(state, player, hand, hit).getInteractionResult();
if (wrenchResult != InteractionResult.PASS) {
return wrenchResult;
}
//Handle filling fluid tank
if (!player.isShiftKeyDown()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package mekanism.common.block.basic;

import mekanism.api.radiation.IRadiationManager;
import mekanism.api.security.ISecurityUtils;
import mekanism.common.block.attribute.Attribute;
import mekanism.common.block.prefab.BlockTile.BlockTileModel;
Expand Down Expand Up @@ -69,7 +70,7 @@ public InteractionResult use(@NotNull BlockState state, @NotNull Level world, @N
if (tile == null) {
return InteractionResult.PASS;
} else if (world.isClientSide) {
return genericClientActivated(player, hand);
return genericClientActivated(player, hand, tile);
}
//TODO: Make this be moved into the logistical sorter tile
ItemStack stack = player.getItemInHand(hand);
Expand All @@ -78,7 +79,11 @@ public InteractionResult use(@NotNull BlockState state, @NotNull Level world, @N
return InteractionResult.FAIL;
}
if (player.isShiftKeyDown()) {
WorldUtils.dismantleBlock(state, world, pos);
if (IRadiationManager.INSTANCE.isRadiationEnabled() && tile.getRadiationScale() > 0) {
//Note: This should always be false for the logistical sorter, but we keep it here for good measure
return InteractionResult.FAIL;
}
WorldUtils.dismantleBlock(state, world, pos, player);
return InteractionResult.SUCCESS;
}
Direction change = tile.getDirection().getClockWise();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import mekanism.common.block.prefab.BlockTileGlass;
import mekanism.common.content.blocktype.BlockTypeTile;
import mekanism.common.tile.base.WrenchResult;
import mekanism.common.tile.prefab.TileEntityStructuralMultiblock;
import mekanism.common.util.MekanismUtils;
import mekanism.common.util.WorldUtils;
Expand Down Expand Up @@ -38,8 +37,10 @@ public InteractionResult use(@NotNull BlockState state, @NotNull Level world, @N
}
}
return InteractionResult.SUCCESS;
} else if (tile.tryWrench(state, player, hand, hit) != WrenchResult.PASS) {
return InteractionResult.SUCCESS;
}
InteractionResult wrenchResult = tile.tryWrench(state, player, hand, hit).getInteractionResult();
if (wrenchResult != InteractionResult.PASS) {
return wrenchResult;
}
return tile.onActivate(player, hand, player.getItemInHand(hand));
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/mekanism/common/block/prefab/BlockBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@ public VoxelShape getShape(@NotNull BlockState state, @NotNull BlockGetter world
public InteractionResult use(@NotNull BlockState state, @NotNull Level world, @NotNull BlockPos pos, @NotNull Player player, @NotNull InteractionHand hand,
@NotNull BlockHitResult hit) {
if (player.isShiftKeyDown() && MekanismUtils.canUseAsWrench(player.getItemInHand(hand))) {
//Note: We don't handle checking if it is radioactive here, as the assumption is it doesn't have a tile so won't have that information
if (!world.isClientSide) {
WorldUtils.dismantleBlock(state, world, pos);
WorldUtils.dismantleBlock(state, world, pos, player);
}
return InteractionResult.SUCCESS;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.function.UnaryOperator;
import mekanism.common.content.blocktype.BlockTypeTile;
import mekanism.common.tile.base.TileEntityMekanism;
import mekanism.common.tile.base.WrenchResult;
import mekanism.common.tile.prefab.TileEntityMultiblock;
import mekanism.common.util.MekanismUtils;
import mekanism.common.util.WorldUtils;
Expand Down Expand Up @@ -43,8 +42,10 @@ public InteractionResult use(@NotNull BlockState state, @NotNull Level world, @N
}
}
return InteractionResult.SUCCESS;
} else if (tile.tryWrench(state, player, hand, hit) != WrenchResult.PASS) {
return InteractionResult.SUCCESS;
}
InteractionResult wrenchResult = tile.tryWrench(state, player, hand, hit).getInteractionResult();
if (wrenchResult != InteractionResult.PASS) {
return wrenchResult;
}
return tile.onActivate(player, hand, player.getItemInHand(hand));
}
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/mekanism/common/block/prefab/BlockTile.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import mekanism.common.content.blocktype.BlockTypeTile;
import mekanism.common.registration.impl.TileEntityTypeRegistryObject;
import mekanism.common.tile.base.TileEntityMekanism;
import mekanism.common.tile.base.WrenchResult;
import mekanism.common.util.WorldUtils;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
Expand Down Expand Up @@ -58,9 +57,11 @@ public InteractionResult use(@NotNull BlockState state, @NotNull Level world, @N
if (tile == null) {
return InteractionResult.PASS;
} else if (world.isClientSide) {
return genericClientActivated(player, hand);
} else if (tile.tryWrench(state, player, hand, hit) != WrenchResult.PASS) {
return InteractionResult.SUCCESS;
return genericClientActivated(player, hand, tile);
}
InteractionResult wrenchResult = tile.tryWrench(state, player, hand, hit).getInteractionResult();
if (wrenchResult != InteractionResult.PASS) {
return wrenchResult;
}
return type.has(AttributeGui.class) ? tile.openGui(player) : InteractionResult.PASS;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public InteractionResult use(@NotNull BlockState state, @NotNull Level world, @N
ItemStack stack = player.getItemInHand(hand);
if (MekanismUtils.canUseAsWrench(stack) && player.isShiftKeyDown()) {
if (!world.isClientSide) {
WorldUtils.dismantleBlock(state, world, pos);
WorldUtils.dismantleBlock(state, world, pos, player);
}
return InteractionResult.SUCCESS;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,11 @@ public WrenchResult tryWrench(BlockState state, Player player, InteractionHand h
return WrenchResult.NO_SECURITY;
}
if (player.isShiftKeyDown()) {
WorldUtils.dismantleBlock(state, getLevel(), worldPosition, this);
if (IRadiationManager.INSTANCE.isRadiationEnabled() && getRadiationScale() > 0) {
//Don't allow dismantling radioactive blocks
return WrenchResult.RADIOACTIVE;
}
WorldUtils.dismantleBlock(state, getLevel(), worldPosition, this, player);
return WrenchResult.DISMANTLED;
}
//Special ITileDirectional handling
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/mekanism/common/tile/base/WrenchResult.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
package mekanism.common.tile.base;

import net.minecraft.world.InteractionResult;

//TODO: Move this to a different package
public enum WrenchResult {
DISMANTLED,
SUCCESS,
PASS,
NO_SECURITY
NO_SECURITY,
RADIOACTIVE;

public InteractionResult getInteractionResult() {
return switch (this) {
case PASS -> InteractionResult.PASS;
case RADIOACTIVE -> InteractionResult.FAIL;
default -> InteractionResult.SUCCESS;
};
}
}
26 changes: 18 additions & 8 deletions src/main/java/mekanism/common/util/WorldUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.BlockAndTintGetter;
Expand Down Expand Up @@ -362,19 +363,28 @@ public static void markChunkDirty(Level world, BlockPos pos) {
}

/**
* Dismantles a block, dropping it and removing it from the world.
* Dismantles a block, adding to player inventory (or dropping it) and removing it from the world.
*/
public static void dismantleBlock(BlockState state, Level world, BlockPos pos) {
dismantleBlock(state, world, pos, getTileEntity(world, pos));
public static void dismantleBlock(BlockState state, Level world, BlockPos pos, @Nullable Entity entity) {
dismantleBlock(state, world, pos, getTileEntity(world, pos), entity);
}

/**
* Dismantles a block, dropping it and removing it from the world.
* Dismantles a block, adding to player inventory (or dropping it) and removing it from the world.
*/
public static void dismantleBlock(BlockState state, Level world, BlockPos pos, @Nullable BlockEntity tile) {
if (world instanceof ServerLevel level) {//Copy of Block#dropResources but skipping the xp dropping
Block.getDrops(state, level, pos, tile).forEach(stack -> Block.popResource(world, pos, stack));
state.spawnAfterBreak(level, pos, ItemStack.EMPTY, false);
public static void dismantleBlock(BlockState state, Level world, BlockPos pos, @Nullable BlockEntity tile, @Nullable Entity entity) {
if (entity instanceof Player player) {
if (world instanceof ServerLevel) {
Block.getDrops(state, (ServerLevel) world, pos, tile, entity, ItemStack.EMPTY).forEach(dropStack -> {
if (player.getInventory().add(dropStack)) {
world.playSound(null, entity.getX(), entity.getY(), entity.getZ(), SoundEvents.ITEM_PICKUP, SoundSource.PLAYERS, 0.2F, (world.random.nextFloat() - world.random.nextFloat()) * 1.4F + 2.0F);
} else {
player.drop(dropStack, false);
}
});
}
} else {
Block.dropResources(state, world, pos, tile, entity, ItemStack.EMPTY, false);
}
world.removeBlock(pos, false);
}
Expand Down
24 changes: 22 additions & 2 deletions src/main/resources/assets/mekanism/models/item/configurator.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
{
"parent": "item/handheld",
"textures": {
"layer0": "mekanism:item/configurator"
}
"layer0": "mekanism:item/configurator_config"
},
"overrides": [
{
"predicate": {
"mekanism:mode": 1
},
"model": "mekanism:item/configurator_empty"
},
{
"predicate": {
"mekanism:mode": 2
},
"model": "mekanism:item/configurator_rotate"
},
{
"predicate": {
"mekanism:mode": 3
},
"model": "mekanism:item/configurator_wrench"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "item/handheld",
"textures": {
"layer0": "mekanism:item/configurator_empty"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "item/handheld",
"textures": {
"layer0": "mekanism:item/configurator_rotate"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "item/handheld",
"textures": {
"layer0": "mekanism:item/configurator_wrench"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.