diff --git a/src/main/java/appeng/hooks/AEToolItem.java b/src/main/java/appeng/hooks/AEToolItem.java index c38757523c9..8b89b53d070 100644 --- a/src/main/java/appeng/hooks/AEToolItem.java +++ b/src/main/java/appeng/hooks/AEToolItem.java @@ -5,5 +5,7 @@ import net.minecraft.world.item.context.UseOnContext; public interface AEToolItem { - InteractionResult onItemUseFirst(ItemStack stack, UseOnContext context); + default InteractionResult onItemUseFirst(ItemStack stack, UseOnContext context) { + return InteractionResult.PASS; + }; } diff --git a/src/main/java/appeng/items/materials/UpgradeCardItem.java b/src/main/java/appeng/items/materials/UpgradeCardItem.java index ae037cde7d8..f5ca069795d 100644 --- a/src/main/java/appeng/items/materials/UpgradeCardItem.java +++ b/src/main/java/appeng/items/materials/UpgradeCardItem.java @@ -20,6 +20,7 @@ import java.util.List; +import appeng.hooks.AEToolItem; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.network.chat.Component; @@ -40,9 +41,8 @@ import appeng.core.localization.PlayerMessages; import appeng.items.AEBaseItem; import appeng.util.InteractionUtil; -import starry.refabricated.ae2.patches.IItemExtended; -public class UpgradeCardItem extends AEBaseItem implements IItemExtended { +public class UpgradeCardItem extends AEBaseItem implements AEToolItem { public UpgradeCardItem(Properties properties) { super(properties); @@ -112,6 +112,6 @@ public InteractionResult onItemUseFirst(ItemStack stack, UseOnContext context) { } } - return IItemExtended.super.onItemUseFirst(stack, context); + return AEToolItem.super.onItemUseFirst(stack, context); } } diff --git a/src/main/java/appeng/items/parts/FacadeItem.java b/src/main/java/appeng/items/parts/FacadeItem.java index 6976ad475f5..dc727dc9ef1 100644 --- a/src/main/java/appeng/items/parts/FacadeItem.java +++ b/src/main/java/appeng/items/parts/FacadeItem.java @@ -18,6 +18,7 @@ package appeng.items.parts; +import appeng.hooks.AEToolItem; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.core.registries.BuiltInRegistries; @@ -48,9 +49,8 @@ import appeng.core.definitions.AEItems; import appeng.facade.FacadePart; import appeng.items.AEBaseItem; -import starry.refabricated.ae2.patches.IItemExtended; -public class FacadeItem extends AEBaseItem implements IFacadeItem, IItemExtended { +public class FacadeItem extends AEBaseItem implements IFacadeItem, AEToolItem { public FacadeItem(Properties properties) { super(properties); diff --git a/src/main/java/appeng/items/tools/MemoryCardItem.java b/src/main/java/appeng/items/tools/MemoryCardItem.java index ac337d4077e..f7cde33e0de 100644 --- a/src/main/java/appeng/items/tools/MemoryCardItem.java +++ b/src/main/java/appeng/items/tools/MemoryCardItem.java @@ -23,6 +23,9 @@ import java.util.List; import java.util.Set; +import appeng.hooks.AEToolItem; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; import org.jetbrains.annotations.Nullable; import net.minecraft.ChatFormatting; @@ -44,8 +47,6 @@ import net.minecraft.world.item.context.UseOnContext; import net.minecraft.world.level.Level; import net.minecraft.world.level.LevelReader; -import net.neoforged.api.distmarker.Dist; -import net.neoforged.api.distmarker.OnlyIn; import it.unimi.dsi.fastutil.objects.Object2IntMap; import it.unimi.dsi.fastutil.objects.Object2IntOpenCustomHashMap; @@ -277,7 +278,7 @@ private static void restoreUpgrades(Player player, ExportedUpgrades desiredUpgra } @Override - @OnlyIn(Dist.CLIENT) + @Environment(EnvType.CLIENT) public void appendHoverText(ItemStack stack, TooltipContext context, List lines, TooltipFlag advancedTooltips) { diff --git a/src/main/java/appeng/items/tools/NetworkToolItem.java b/src/main/java/appeng/items/tools/NetworkToolItem.java index 806d4edcdad..8cd9e8ecc00 100644 --- a/src/main/java/appeng/items/tools/NetworkToolItem.java +++ b/src/main/java/appeng/items/tools/NetworkToolItem.java @@ -24,6 +24,7 @@ import java.util.List; import java.util.Optional; +import appeng.hooks.AEToolItem; import org.jetbrains.annotations.Nullable; import net.minecraft.core.BlockPos; @@ -64,7 +65,7 @@ import appeng.util.inv.InternalInventoryHost; import appeng.util.inv.filter.IAEItemFilter; -public class NetworkToolItem extends AEBaseItem implements IMenuItem { +public class NetworkToolItem extends AEBaseItem implements IMenuItem, AEToolItem { public NetworkToolItem(Properties properties) { super(properties); diff --git a/src/main/java/starry/refabricated/ae2/mixin/MultiPlayerGameModeMixin.java b/src/main/java/starry/refabricated/ae2/mixin/MultiPlayerGameModeMixin.java new file mode 100644 index 00000000000..c4579caabaa --- /dev/null +++ b/src/main/java/starry/refabricated/ae2/mixin/MultiPlayerGameModeMixin.java @@ -0,0 +1,19 @@ +package starry.refabricated.ae2.mixin; + +import com.llamalad7.mixinextras.sugar.Local; +import net.minecraft.client.multiplayer.MultiPlayerGameMode; +import net.minecraft.client.player.LocalPlayer; +import net.minecraft.core.BlockPos; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.ModifyVariable; + +@Mixin(MultiPlayerGameMode.class) +public class MultiPlayerGameModeMixin { + + @ModifyVariable(method = "performUseItemOn", at = @At(value = "STORE"), ordinal = 1) + private boolean injected(boolean x, @Local(argsOnly = true) LocalPlayer player, @Local BlockPos blockpos) { + return x && !(player.getMainHandItem().doesSneakBypassUse(player.level(), blockpos, player) && player.getOffhandItem().doesSneakBypassUse(player.level(), blockpos, player)); + } + +} diff --git a/src/main/java/starry/refabricated/ae2/mixin/ServerPlayerGameModeMixin.java b/src/main/java/starry/refabricated/ae2/mixin/ServerPlayerGameModeMixin.java new file mode 100644 index 00000000000..78c870ced6a --- /dev/null +++ b/src/main/java/starry/refabricated/ae2/mixin/ServerPlayerGameModeMixin.java @@ -0,0 +1,20 @@ +package starry.refabricated.ae2.mixin; + +import com.llamalad7.mixinextras.sugar.Local; +import net.minecraft.core.BlockPos; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.server.level.ServerPlayerGameMode; +import net.minecraft.world.level.Level; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.ModifyVariable; + +@Mixin(ServerPlayerGameMode.class) +public class ServerPlayerGameModeMixin { + + @ModifyVariable(method = "useItemOn", at = @At(value = "STORE"), ordinal = 1) + private boolean injected(boolean x, @Local(argsOnly = true) ServerPlayer player, @Local(argsOnly = true) Level level, @Local BlockPos blockpos) { + return x && !(player.getMainHandItem().doesSneakBypassUse(level, blockpos, player) && player.getOffhandItem().doesSneakBypassUse(level, blockpos, player)); + } + +} diff --git a/src/main/java/starry/refabricated/ae2/patches/IItemExtended.java b/src/main/java/starry/refabricated/ae2/patches/IItemExtended.java index 04fa9595751..98a36b2c0ba 100644 --- a/src/main/java/starry/refabricated/ae2/patches/IItemExtended.java +++ b/src/main/java/starry/refabricated/ae2/patches/IItemExtended.java @@ -1,17 +1,19 @@ package starry.refabricated.ae2.patches; +import net.minecraft.core.BlockPos; import net.minecraft.world.InteractionResult; +import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.context.UseOnContext; public interface IItemExtended { - default InteractionResult onItemUseFirst(ItemStack stack, UseOnContext context) { - return InteractionResult.PASS; - } - default boolean shouldCauseReequipAnimation(ItemStack oldStack, ItemStack newStack, boolean slotChanged) { return !oldStack.equals(newStack); // !ItemStack.areItemStacksEqual(oldStack, newStack); } + default boolean doesSneakBypassUse(ItemStack stack, net.minecraft.world.level.LevelReader level, BlockPos pos, Player player) { + return false; + } + } diff --git a/src/main/java/starry/refabricated/ae2/patches/IItemStackExtended.java b/src/main/java/starry/refabricated/ae2/patches/IItemStackExtended.java index 4ccaa32d882..3057dd07359 100644 --- a/src/main/java/starry/refabricated/ae2/patches/IItemStackExtended.java +++ b/src/main/java/starry/refabricated/ae2/patches/IItemStackExtended.java @@ -17,22 +17,8 @@ private ItemStack self() { return (ItemStack) (Object) this; } - default InteractionResult onItemUseFirst(UseOnContext context) { - Player entityplayer = context.getPlayer(); - BlockPos blockpos = context.getClickedPos(); - BlockInWorld state = new BlockInWorld(context.getLevel(), blockpos, false); - AdventureModePredicate adventureModePredicate = self().get(DataComponents.CAN_PLACE_ON); - if (entityplayer != null && !entityplayer.getAbilities().mayBuild && (adventureModePredicate == null || !adventureModePredicate.test(state))) { - return InteractionResult.PASS; - } else { - Item item = self().getItem(); - InteractionResult result = ((IItemExtended) item).onItemUseFirst(self(), context); - if (entityplayer != null && result == InteractionResult.SUCCESS) { - entityplayer.awardStat(Stats.ITEM_USED.get(item)); - } - - return result; - } + default boolean doesSneakBypassUse(net.minecraft.world.level.LevelReader level, BlockPos pos, Player player) { + return self().isEmpty() || self().getItem().doesSneakBypassUse(self(), level, pos, player); } } diff --git a/src/main/resources/ae2.refabricated.mixins.json b/src/main/resources/ae2.refabricated.mixins.json index 59562ec617e..e59d2c58c0a 100644 --- a/src/main/resources/ae2.refabricated.mixins.json +++ b/src/main/resources/ae2.refabricated.mixins.json @@ -6,14 +6,16 @@ "mixins": [ "MinecraftServerMixin", "PlayerMixin", - "ServerLevelMixin" + "ServerLevelMixin", + "ServerPlayerGameModeMixin" ], "server": [], "injectors": { "defaultRequire": 1 }, "client": [ - "client.MouseHandlerMixin", - "client.ItemInHandRendererMixin" + "MultiPlayerGameModeMixin", + "client.ItemInHandRendererMixin", + "client.MouseHandlerMixin" ] } \ No newline at end of file