diff --git a/src/main/java/com/gregtechceu/gtceu/api/item/tool/GTToolType.java b/src/main/java/com/gregtechceu/gtceu/api/item/tool/GTToolType.java index 3f189c7c38..4d1f7cd944 100644 --- a/src/main/java/com/gregtechceu/gtceu/api/item/tool/GTToolType.java +++ b/src/main/java/com/gregtechceu/gtceu/api/item/tool/GTToolType.java @@ -53,8 +53,8 @@ public class GTToolType { .toolTag(TagUtil.createItemTag("pickaxes", true)) .toolTag(ItemTags.CLUSTER_MAX_HARVESTABLES) .harvestTag(TagUtil.createBlockTag("mineable/pickaxe", true)) - .toolStats(b -> b.blockBreaking().attackDamage(1.0F).attackSpeed(-2.8F)) - /* .behaviors(TorchPlaceBehavior.INSTANCE) */ + .toolStats(b -> b.blockBreaking().attackDamage(1.0F).attackSpeed(-2.8F) + .behaviors(TorchPlaceBehavior.INSTANCE)) .toolClassNames("pickaxe") .build(); public static final GTToolType SHOVEL = GTToolType.builder("shovel") @@ -89,7 +89,7 @@ public class GTToolType { .toolStats(b -> b.blockBreaking().aoe(1, 1, 0) .efficiencyMultiplier(0.4F).attackDamage(1.5F).attackSpeed(-3.2F) .durabilityMultiplier(3.0F) - /* .behaviors(TorchPlaceBehavior.INSTANCE) */) + .behaviors(TorchPlaceBehavior.INSTANCE)) .toolClasses(GTToolType.PICKAXE) .build(); public static final GTToolType SPADE = GTToolType.builder("spade") @@ -227,7 +227,7 @@ public class GTToolType { .toolStats(b -> b.blockBreaking().aoe(1, 1, 0) .attackDamage(1.0F).attackSpeed(-3.2F).durabilityMultiplier(3.0F) .brokenStack(ToolHelper.SUPPLY_POWER_UNIT_LV) - /* .behaviors(TorchPlaceBehavior.INSTANCE) */) + .behaviors(TorchPlaceBehavior.INSTANCE)) .sound(GTSoundEntries.DRILL_TOOL, true) .electric(GTValues.LV) .toolClassNames("drill") @@ -243,7 +243,7 @@ public class GTToolType { .toolStats(b -> b.blockBreaking().aoe(1, 1, 2) .attackDamage(1.0F).attackSpeed(-3.2F).durabilityMultiplier(4.0F) .brokenStack(ToolHelper.SUPPLY_POWER_UNIT_MV) - /* .behaviors(TorchPlaceBehavior.INSTANCE) */) + .behaviors(TorchPlaceBehavior.INSTANCE)) .sound(GTSoundEntries.DRILL_TOOL, true) .electric(GTValues.MV) .toolClassNames("drill") @@ -259,7 +259,7 @@ public class GTToolType { .toolStats(b -> b.blockBreaking().aoe(2, 2, 4) .attackDamage(1.0F).attackSpeed(-3.2F).durabilityMultiplier(5.0F) .brokenStack(ToolHelper.SUPPLY_POWER_UNIT_HV) - /* .behaviors(TorchPlaceBehavior.INSTANCE) */) + .behaviors(TorchPlaceBehavior.INSTANCE)) .sound(GTSoundEntries.DRILL_TOOL, true) .electric(GTValues.HV) .toolClassNames("drill") @@ -275,7 +275,7 @@ public class GTToolType { .toolStats(b -> b.blockBreaking().aoe(3, 3, 6) .attackDamage(1.0F).attackSpeed(-3.2F).durabilityMultiplier(6.0F) .brokenStack(ToolHelper.SUPPLY_POWER_UNIT_EV) - /* .behaviors(TorchPlaceBehavior.INSTANCE) */) + .behaviors(TorchPlaceBehavior.INSTANCE)) .sound(GTSoundEntries.DRILL_TOOL, true) .electric(GTValues.EV) .toolClassNames("drill") @@ -291,7 +291,7 @@ public class GTToolType { .toolStats(b -> b.blockBreaking().aoe(4, 4, 8) .attackDamage(1.0F).attackSpeed(-3.2F).durabilityMultiplier(7.0F) .brokenStack(ToolHelper.SUPPLY_POWER_UNIT_IV) - /* .behaviors(TorchPlaceBehavior.INSTANCE) */) + .behaviors(TorchPlaceBehavior.INSTANCE)) .sound(GTSoundEntries.DRILL_TOOL, true) .electric(GTValues.IV) .toolClassNames("drill") diff --git a/src/main/java/com/gregtechceu/gtceu/common/item/tool/behavior/TorchPlaceBehavior.java b/src/main/java/com/gregtechceu/gtceu/common/item/tool/behavior/TorchPlaceBehavior.java index fed5c13a0f..41266b1449 100644 --- a/src/main/java/com/gregtechceu/gtceu/common/item/tool/behavior/TorchPlaceBehavior.java +++ b/src/main/java/com/gregtechceu/gtceu/common/item/tool/behavior/TorchPlaceBehavior.java @@ -7,20 +7,13 @@ import net.minecraft.core.BlockPos; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.chat.Component; -import net.minecraft.sounds.SoundSource; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.player.Player; -import net.minecraft.world.item.BlockItem; -import net.minecraft.world.item.Item; -import net.minecraft.world.item.ItemStack; -import net.minecraft.world.item.TooltipFlag; +import net.minecraft.world.item.*; 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.Block; -import net.minecraft.world.level.block.Blocks; -import net.minecraft.world.level.block.SoundType; import net.minecraft.world.level.block.state.BlockState; import org.jetbrains.annotations.NotNull; @@ -30,7 +23,6 @@ import static com.gregtechceu.gtceu.api.item.tool.ToolHelper.TORCH_PLACING_KEY; -// TODO this currently voids the used tool as well as a torch. how fix? public class TorchPlaceBehavior implements IToolBehavior { public static final TorchPlaceBehavior INSTANCE = new TorchPlaceBehavior(); @@ -44,65 +36,73 @@ protected TorchPlaceBehavior() {/**/} ItemStack stack = player.getItemInHand(hand); CompoundTag behaviourTag = ToolHelper.getBehaviorsTag(stack); - if (behaviourTag.getBoolean(TORCH_PLACING_KEY)) { - int cachedTorchSlot; - ItemStack slotStack; - if (behaviourTag.getBoolean(ToolHelper.TORCH_PLACING_CACHE_SLOT_KEY)) { - cachedTorchSlot = behaviourTag.getInt(ToolHelper.TORCH_PLACING_CACHE_SLOT_KEY); - if (cachedTorchSlot < 0) { - slotStack = player.getInventory().offhand.get(0); - } else { - slotStack = player.getInventory().items.get(cachedTorchSlot); - } - if (checkAndPlaceTorch(context, slotStack)) { - return InteractionResult.SUCCESS; - } + + if (!behaviourTag.getBoolean(TORCH_PLACING_KEY)) { + return InteractionResult.PASS; + } + + int cachedTorchSlot; + ItemStack slotStack; + if (behaviourTag.getBoolean(ToolHelper.TORCH_PLACING_CACHE_SLOT_KEY)) { + cachedTorchSlot = behaviourTag.getInt(ToolHelper.TORCH_PLACING_CACHE_SLOT_KEY); + if (cachedTorchSlot < 0) { + slotStack = player.getInventory().offhand.get(0); + } else { + slotStack = player.getInventory().items.get(cachedTorchSlot); } - for (int i = 0; i < player.getInventory().offhand.size(); i++) { - slotStack = player.getInventory().offhand.get(i); - if (checkAndPlaceTorch(context, slotStack)) { - behaviourTag.putInt(ToolHelper.TORCH_PLACING_CACHE_SLOT_KEY, -(i + 1)); - return InteractionResult.SUCCESS; - } + if (checkAndPlaceTorch(context, slotStack)) { + return InteractionResult.SUCCESS; } - for (int i = 0; i < player.getInventory().items.size(); i++) { - slotStack = player.getInventory().items.get(i); - if (checkAndPlaceTorch(context, slotStack)) { - behaviourTag.putInt(ToolHelper.TORCH_PLACING_CACHE_SLOT_KEY, i); - return InteractionResult.SUCCESS; - } + } + for (int i = 0; i < player.getInventory().offhand.size(); i++) { + slotStack = player.getInventory().offhand.get(i); + if (checkAndPlaceTorch(context, slotStack)) { + behaviourTag.putInt(ToolHelper.TORCH_PLACING_CACHE_SLOT_KEY, -(i + 1)); + return InteractionResult.SUCCESS; + } + } + for (int i = 0; i < player.getInventory().items.size(); i++) { + slotStack = player.getInventory().items.get(i); + if (checkAndPlaceTorch(context, slotStack)) { + behaviourTag.putInt(ToolHelper.TORCH_PLACING_CACHE_SLOT_KEY, i); + return InteractionResult.SUCCESS; } } return InteractionResult.PASS; } private static boolean checkAndPlaceTorch(UseOnContext context, ItemStack slotStack) { - if (!slotStack.isEmpty()) { - Item slotItem = slotStack.getItem(); - if (slotItem instanceof BlockItem slotItemBlock) { - Block slotBlock = slotItemBlock.getBlock(); - if (slotBlock == Blocks.TORCH || - slotStack.is(TagUtil.createItemTag("torches"))) { - BlockPos pos = context.getClickedPos(); - BlockState state = context.getLevel().getBlockState(pos); - if (!state.canBeReplaced()) { - pos = pos.relative(context.getClickedFace()); - } - if (context.getPlayer().mayUseItemAt(pos, context.getClickedFace(), slotStack)) { - var blockPlaceContext = new BlockPlaceContext(context); - BlockState slotState = slotBlock.getStateForPlacement(blockPlaceContext); - if (slotItemBlock.place(blockPlaceContext).consumesAction()) { - slotState = context.getLevel().getBlockState(pos); - SoundType soundtype = slotState.getBlock().getSoundType(slotState); - context.getLevel().playSound(context.getPlayer(), pos, soundtype.getPlaceSound(), - SoundSource.BLOCKS, - (soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F); - if (!context.getPlayer().isCreative()) slotStack.shrink(1); - return true; - } - } - } - } + if (context.getLevel().isClientSide) + return false; + + if (slotStack.isEmpty()) + return false; + + Item slotItem = slotStack.getItem(); + + if (slotItem != Items.TORCH && !slotStack.is(TagUtil.createItemTag("torches"))) + return false; + + if (context.getPlayer() == null) + return false; + + if (!(slotItem instanceof BlockItem slotItemBlock)) { + return false; + } + + BlockPos pos = context.getClickedPos(); + BlockState state = context.getLevel().getBlockState(pos); + + if (!state.canBeReplaced()) { + pos = pos.relative(context.getClickedFace()); + } + + if (context.getPlayer().mayUseItemAt(pos, context.getClickedFace(), slotStack)) { + var torchContext = new UseOnContext(context.getLevel(), context.getPlayer(), context.getHand(), slotStack, + context.getHitResult()); + var blockPlaceContext = new BlockPlaceContext(torchContext); + InteractionResult placed = slotItemBlock.place(blockPlaceContext); + return placed.consumesAction(); } return false; }