diff --git a/src/main/java/io/github/fusionflux/portalcubed/PortalCubed.java b/src/main/java/io/github/fusionflux/portalcubed/PortalCubed.java index e4a8d50c..436a9154 100644 --- a/src/main/java/io/github/fusionflux/portalcubed/PortalCubed.java +++ b/src/main/java/io/github/fusionflux/portalcubed/PortalCubed.java @@ -3,7 +3,7 @@ import io.github.fusionflux.portalcubed.content.PortalCubedBlocks; import io.github.fusionflux.portalcubed.content.PortalCubedItems; import io.github.fusionflux.portalcubed.content.PortalCubedTabs; -import io.github.fusionflux.portalcubed.registration.Registrar; +import io.github.fusionflux.portalcubed.framework.registration.Registrar; import net.minecraft.resources.ResourceLocation; import org.quiltmc.loader.api.ModContainer; diff --git a/src/main/java/io/github/fusionflux/portalcubed/content/PortalCubedBlocks.java b/src/main/java/io/github/fusionflux/portalcubed/content/PortalCubedBlocks.java index cf52dde0..5f621f7e 100644 --- a/src/main/java/io/github/fusionflux/portalcubed/content/PortalCubedBlocks.java +++ b/src/main/java/io/github/fusionflux/portalcubed/content/PortalCubedBlocks.java @@ -7,7 +7,7 @@ import static io.github.fusionflux.portalcubed.PortalCubed.REGISTRAR; -import io.github.fusionflux.portalcubed.registration.block.BlockItemProvider; +import io.github.fusionflux.portalcubed.framework.registration.block.BlockItemProvider; public class PortalCubedBlocks { public static final RotatedPillarBlock TEST_BLOCK = REGISTRAR.blocks.create("test_block", RotatedPillarBlock::new) diff --git a/src/main/java/io/github/fusionflux/portalcubed/content/PortalCubedItems.java b/src/main/java/io/github/fusionflux/portalcubed/content/PortalCubedItems.java index a63d5c7f..2a5e7cf3 100644 --- a/src/main/java/io/github/fusionflux/portalcubed/content/PortalCubedItems.java +++ b/src/main/java/io/github/fusionflux/portalcubed/content/PortalCubedItems.java @@ -2,13 +2,11 @@ import static io.github.fusionflux.portalcubed.PortalCubed.REGISTRAR; -import net.minecraft.world.item.Item; - -import org.quiltmc.qsl.item.setting.api.QuiltItemSettings; +import io.github.fusionflux.portalcubed.content.portal.gun.PortalGunItem; public class PortalCubedItems { - public static final Item TEST_ITEM = REGISTRAR.items.create("test_item") - .settings(QuiltItemSettings::fireproof) + public static final PortalGunItem PORTAL_GUN = REGISTRAR.items.create("portal_gun", PortalGunItem::new) + .settings(s -> s.stacksTo(1).fireResistant()) .build(); public static void init() { diff --git a/src/main/java/io/github/fusionflux/portalcubed/content/portal/PortalType.java b/src/main/java/io/github/fusionflux/portalcubed/content/portal/PortalType.java new file mode 100644 index 00000000..886e59a9 --- /dev/null +++ b/src/main/java/io/github/fusionflux/portalcubed/content/portal/PortalType.java @@ -0,0 +1,5 @@ +package io.github.fusionflux.portalcubed.content.portal; + +public enum PortalType { + PRIMARY, SECONDARY; +} diff --git a/src/main/java/io/github/fusionflux/portalcubed/content/portal/gun/PortalGunItem.java b/src/main/java/io/github/fusionflux/portalcubed/content/portal/gun/PortalGunItem.java new file mode 100644 index 00000000..e23ecee9 --- /dev/null +++ b/src/main/java/io/github/fusionflux/portalcubed/content/portal/gun/PortalGunItem.java @@ -0,0 +1,32 @@ +package io.github.fusionflux.portalcubed.content.portal.gun; + +import io.github.fusionflux.portalcubed.content.portal.PortalType; +import io.github.fusionflux.portalcubed.framework.item.DirectClickItem; +import net.minecraft.world.InteractionHand; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.Level; + +import org.quiltmc.qsl.base.api.util.TriState; + +public class PortalGunItem extends Item implements DirectClickItem { + public PortalGunItem(Properties settings) { + super(settings); + } + + @Override + public TriState onAttack(Level level, Player player, ItemStack stack) { + shoot(level, player, stack, InteractionHand.MAIN_HAND, PortalType.PRIMARY); + return TriState.TRUE; + } + + @Override + public TriState onUse(Level level, Player player, ItemStack stack, InteractionHand hand) { + shoot(level, player, stack, hand, PortalType.SECONDARY); + return TriState.TRUE; + } + + public void shoot(Level level, Player player, ItemStack stack, InteractionHand hand, PortalType type) { + } +} diff --git a/src/main/java/io/github/fusionflux/portalcubed/content/portal/gun/package-info.java b/src/main/java/io/github/fusionflux/portalcubed/content/portal/gun/package-info.java new file mode 100644 index 00000000..eb790c62 --- /dev/null +++ b/src/main/java/io/github/fusionflux/portalcubed/content/portal/gun/package-info.java @@ -0,0 +1,4 @@ +@MethodsReturnNonnullByDefault +package io.github.fusionflux.portalcubed.content.portal.gun; + +import net.minecraft.MethodsReturnNonnullByDefault; diff --git a/src/main/java/io/github/fusionflux/portalcubed/framework/item/DirectClickItem.java b/src/main/java/io/github/fusionflux/portalcubed/framework/item/DirectClickItem.java new file mode 100644 index 00000000..19d207d2 --- /dev/null +++ b/src/main/java/io/github/fusionflux/portalcubed/framework/item/DirectClickItem.java @@ -0,0 +1,23 @@ +package io.github.fusionflux.portalcubed.framework.item; + +import net.minecraft.world.InteractionHand; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.Level; + +import org.quiltmc.qsl.base.api.util.TriState; + +/** + * An item that directly listens for use and attacking. + */ +public interface DirectClickItem { + /** + * @return DEFAULT to fall back to vanilla, FALSE to cancel, TRUE to continue to server / cancel + */ + TriState onAttack(Level level, Player player, ItemStack stack); + + /** + * @return DEFAULT to fall back to vanilla, FALSE to cancel, TRUE to continue to server / cancel + */ + TriState onUse(Level level, Player player, ItemStack stack, InteractionHand hand); +} diff --git a/src/main/java/io/github/fusionflux/portalcubed/registration/Registrar.java b/src/main/java/io/github/fusionflux/portalcubed/framework/registration/Registrar.java similarity index 53% rename from src/main/java/io/github/fusionflux/portalcubed/registration/Registrar.java rename to src/main/java/io/github/fusionflux/portalcubed/framework/registration/Registrar.java index 7b727307..dd046e94 100644 --- a/src/main/java/io/github/fusionflux/portalcubed/registration/Registrar.java +++ b/src/main/java/io/github/fusionflux/portalcubed/framework/registration/Registrar.java @@ -1,7 +1,7 @@ -package io.github.fusionflux.portalcubed.registration; +package io.github.fusionflux.portalcubed.framework.registration; -import io.github.fusionflux.portalcubed.registration.block.BlockHelper; -import io.github.fusionflux.portalcubed.registration.item.ItemHelper; +import io.github.fusionflux.portalcubed.framework.registration.block.BlockHelper; +import io.github.fusionflux.portalcubed.framework.registration.item.ItemHelper; public class Registrar { public final String modId; diff --git a/src/main/java/io/github/fusionflux/portalcubed/registration/RenderTypes.java b/src/main/java/io/github/fusionflux/portalcubed/framework/registration/RenderTypes.java similarity index 89% rename from src/main/java/io/github/fusionflux/portalcubed/registration/RenderTypes.java rename to src/main/java/io/github/fusionflux/portalcubed/framework/registration/RenderTypes.java index fc617377..1992a0e3 100644 --- a/src/main/java/io/github/fusionflux/portalcubed/registration/RenderTypes.java +++ b/src/main/java/io/github/fusionflux/portalcubed/framework/registration/RenderTypes.java @@ -1,4 +1,4 @@ -package io.github.fusionflux.portalcubed.registration; +package io.github.fusionflux.portalcubed.framework.registration; import net.minecraft.client.renderer.RenderType; diff --git a/src/main/java/io/github/fusionflux/portalcubed/registration/block/BlockBuilder.java b/src/main/java/io/github/fusionflux/portalcubed/framework/registration/block/BlockBuilder.java similarity index 83% rename from src/main/java/io/github/fusionflux/portalcubed/registration/block/BlockBuilder.java rename to src/main/java/io/github/fusionflux/portalcubed/framework/registration/block/BlockBuilder.java index 1e6a86d2..06dac5f0 100644 --- a/src/main/java/io/github/fusionflux/portalcubed/registration/block/BlockBuilder.java +++ b/src/main/java/io/github/fusionflux/portalcubed/framework/registration/block/BlockBuilder.java @@ -1,7 +1,6 @@ -package io.github.fusionflux.portalcubed.registration.block; +package io.github.fusionflux.portalcubed.framework.registration.block; -import io.github.fusionflux.portalcubed.registration.RenderTypes; -import net.minecraft.world.item.Item; +import io.github.fusionflux.portalcubed.framework.registration.RenderTypes; import net.minecraft.world.level.block.Block; import org.quiltmc.qsl.block.extensions.api.QuiltBlockSettings; diff --git a/src/main/java/io/github/fusionflux/portalcubed/registration/block/BlockBuilderImpl.java b/src/main/java/io/github/fusionflux/portalcubed/framework/registration/block/BlockBuilderImpl.java similarity index 91% rename from src/main/java/io/github/fusionflux/portalcubed/registration/block/BlockBuilderImpl.java rename to src/main/java/io/github/fusionflux/portalcubed/framework/registration/block/BlockBuilderImpl.java index d9a4a84b..300f72ad 100644 --- a/src/main/java/io/github/fusionflux/portalcubed/registration/block/BlockBuilderImpl.java +++ b/src/main/java/io/github/fusionflux/portalcubed/framework/registration/block/BlockBuilderImpl.java @@ -1,8 +1,8 @@ -package io.github.fusionflux.portalcubed.registration.block; +package io.github.fusionflux.portalcubed.framework.registration.block; -import io.github.fusionflux.portalcubed.registration.Registrar; -import io.github.fusionflux.portalcubed.registration.RenderTypes; -import io.github.fusionflux.portalcubed.registration.item.ItemBuilder; +import io.github.fusionflux.portalcubed.framework.registration.Registrar; +import io.github.fusionflux.portalcubed.framework.registration.RenderTypes; +import io.github.fusionflux.portalcubed.framework.registration.item.ItemBuilder; import net.fabricmc.api.EnvType; import net.minecraft.core.Registry; import net.minecraft.core.registries.BuiltInRegistries; diff --git a/src/main/java/io/github/fusionflux/portalcubed/registration/block/BlockFactory.java b/src/main/java/io/github/fusionflux/portalcubed/framework/registration/block/BlockFactory.java similarity index 74% rename from src/main/java/io/github/fusionflux/portalcubed/registration/block/BlockFactory.java rename to src/main/java/io/github/fusionflux/portalcubed/framework/registration/block/BlockFactory.java index adcd7e7c..b9f79393 100644 --- a/src/main/java/io/github/fusionflux/portalcubed/registration/block/BlockFactory.java +++ b/src/main/java/io/github/fusionflux/portalcubed/framework/registration/block/BlockFactory.java @@ -1,4 +1,4 @@ -package io.github.fusionflux.portalcubed.registration.block; +package io.github.fusionflux.portalcubed.framework.registration.block; import net.minecraft.world.level.block.Block; diff --git a/src/main/java/io/github/fusionflux/portalcubed/registration/block/BlockHelper.java b/src/main/java/io/github/fusionflux/portalcubed/framework/registration/block/BlockHelper.java similarity index 81% rename from src/main/java/io/github/fusionflux/portalcubed/registration/block/BlockHelper.java rename to src/main/java/io/github/fusionflux/portalcubed/framework/registration/block/BlockHelper.java index f9cdb008..9aa3cc71 100644 --- a/src/main/java/io/github/fusionflux/portalcubed/registration/block/BlockHelper.java +++ b/src/main/java/io/github/fusionflux/portalcubed/framework/registration/block/BlockHelper.java @@ -1,7 +1,7 @@ -package io.github.fusionflux.portalcubed.registration.block; +package io.github.fusionflux.portalcubed.framework.registration.block; -import io.github.fusionflux.portalcubed.registration.Registrar; -import io.github.fusionflux.portalcubed.registration.RenderTypes; +import io.github.fusionflux.portalcubed.framework.registration.Registrar; +import io.github.fusionflux.portalcubed.framework.registration.RenderTypes; import net.minecraft.world.level.block.Block; import java.util.HashMap; diff --git a/src/main/java/io/github/fusionflux/portalcubed/registration/block/BlockItemProvider.java b/src/main/java/io/github/fusionflux/portalcubed/framework/registration/block/BlockItemProvider.java similarity index 55% rename from src/main/java/io/github/fusionflux/portalcubed/registration/block/BlockItemProvider.java rename to src/main/java/io/github/fusionflux/portalcubed/framework/registration/block/BlockItemProvider.java index 4ac64783..2a0c6ec0 100644 --- a/src/main/java/io/github/fusionflux/portalcubed/registration/block/BlockItemProvider.java +++ b/src/main/java/io/github/fusionflux/portalcubed/framework/registration/block/BlockItemProvider.java @@ -1,12 +1,9 @@ -package io.github.fusionflux.portalcubed.registration.block; +package io.github.fusionflux.portalcubed.framework.registration.block; -import io.github.fusionflux.portalcubed.registration.item.ItemBuilder; -import net.minecraft.world.item.BlockItem; +import io.github.fusionflux.portalcubed.framework.registration.item.ItemBuilder; import net.minecraft.world.item.Item; import net.minecraft.world.level.block.Block; -import org.quiltmc.qsl.item.setting.api.QuiltItemSettings; - public interface BlockItemProvider { ItemBuilder create(B block, ItemBuilder builder); diff --git a/src/main/java/io/github/fusionflux/portalcubed/registration/item/ItemBuilder.java b/src/main/java/io/github/fusionflux/portalcubed/framework/registration/item/ItemBuilder.java similarity index 91% rename from src/main/java/io/github/fusionflux/portalcubed/registration/item/ItemBuilder.java rename to src/main/java/io/github/fusionflux/portalcubed/framework/registration/item/ItemBuilder.java index 29c8c581..a7386eed 100644 --- a/src/main/java/io/github/fusionflux/portalcubed/registration/item/ItemBuilder.java +++ b/src/main/java/io/github/fusionflux/portalcubed/framework/registration/item/ItemBuilder.java @@ -1,4 +1,4 @@ -package io.github.fusionflux.portalcubed.registration.item; +package io.github.fusionflux.portalcubed.framework.registration.item; import java.util.function.Consumer; diff --git a/src/main/java/io/github/fusionflux/portalcubed/registration/item/ItemBuilderImpl.java b/src/main/java/io/github/fusionflux/portalcubed/framework/registration/item/ItemBuilderImpl.java similarity index 93% rename from src/main/java/io/github/fusionflux/portalcubed/registration/item/ItemBuilderImpl.java rename to src/main/java/io/github/fusionflux/portalcubed/framework/registration/item/ItemBuilderImpl.java index 9ca15c89..1259375b 100644 --- a/src/main/java/io/github/fusionflux/portalcubed/registration/item/ItemBuilderImpl.java +++ b/src/main/java/io/github/fusionflux/portalcubed/framework/registration/item/ItemBuilderImpl.java @@ -1,9 +1,9 @@ -package io.github.fusionflux.portalcubed.registration.item; +package io.github.fusionflux.portalcubed.framework.registration.item; import java.util.Objects; import java.util.function.Consumer; -import io.github.fusionflux.portalcubed.registration.Registrar; +import io.github.fusionflux.portalcubed.framework.registration.Registrar; import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents; import net.minecraft.core.Registry; import net.minecraft.core.registries.BuiltInRegistries; diff --git a/src/main/java/io/github/fusionflux/portalcubed/registration/item/ItemFactory.java b/src/main/java/io/github/fusionflux/portalcubed/framework/registration/item/ItemFactory.java similarity index 72% rename from src/main/java/io/github/fusionflux/portalcubed/registration/item/ItemFactory.java rename to src/main/java/io/github/fusionflux/portalcubed/framework/registration/item/ItemFactory.java index 06472b5f..4bc1a75e 100644 --- a/src/main/java/io/github/fusionflux/portalcubed/registration/item/ItemFactory.java +++ b/src/main/java/io/github/fusionflux/portalcubed/framework/registration/item/ItemFactory.java @@ -1,4 +1,4 @@ -package io.github.fusionflux.portalcubed.registration.item; +package io.github.fusionflux.portalcubed.framework.registration.item; import net.minecraft.world.item.Item; diff --git a/src/main/java/io/github/fusionflux/portalcubed/registration/item/ItemHelper.java b/src/main/java/io/github/fusionflux/portalcubed/framework/registration/item/ItemHelper.java similarity index 80% rename from src/main/java/io/github/fusionflux/portalcubed/registration/item/ItemHelper.java rename to src/main/java/io/github/fusionflux/portalcubed/framework/registration/item/ItemHelper.java index 41a713cb..adb83786 100644 --- a/src/main/java/io/github/fusionflux/portalcubed/registration/item/ItemHelper.java +++ b/src/main/java/io/github/fusionflux/portalcubed/framework/registration/item/ItemHelper.java @@ -1,6 +1,6 @@ -package io.github.fusionflux.portalcubed.registration.item; +package io.github.fusionflux.portalcubed.framework.registration.item; -import io.github.fusionflux.portalcubed.registration.Registrar; +import io.github.fusionflux.portalcubed.framework.registration.Registrar; import net.minecraft.world.item.Item; public class ItemHelper { diff --git a/src/main/java/io/github/fusionflux/portalcubed/mixin/MinecraftMixin.java b/src/main/java/io/github/fusionflux/portalcubed/mixin/MinecraftMixin.java new file mode 100644 index 00000000..d821144a --- /dev/null +++ b/src/main/java/io/github/fusionflux/portalcubed/mixin/MinecraftMixin.java @@ -0,0 +1,74 @@ +package io.github.fusionflux.portalcubed.mixin; + +import io.github.fusionflux.portalcubed.framework.item.DirectClickItem; +import io.github.fusionflux.portalcubed.packet.PortalCubedPackets; +import io.github.fusionflux.portalcubed.packet.serverbound.DirectClickItemPacket; +import net.minecraft.client.Minecraft; + +import net.minecraft.client.multiplayer.ClientLevel; +import net.minecraft.client.player.LocalPlayer; +import net.minecraft.world.InteractionHand; +import net.minecraft.world.item.ItemStack; + +import org.jetbrains.annotations.Nullable; +import org.quiltmc.qsl.base.api.util.TriState; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; +import org.spongepowered.asm.mixin.injection.callback.LocalCapture; + +@Mixin(Minecraft.class) +public class MinecraftMixin { + @Shadow + @Nullable + public ClientLevel level; + + @Shadow + @Nullable + public LocalPlayer player; + + @Inject( + method = "startAttack", + at = @At( + value = "INVOKE", + target = "Lnet/minecraft/world/phys/HitResult;getType()Lnet/minecraft/world/phys/HitResult$Type;" + ), + locals = LocalCapture.CAPTURE_FAILHARD, + cancellable = true + ) + private void onAttack(CallbackInfoReturnable cir, ItemStack stack) { + if (stack.getItem() instanceof DirectClickItem direct) { + TriState result = direct.onAttack(level, player, stack); + if (result != TriState.DEFAULT) { + if (result == TriState.TRUE) { + PortalCubedPackets.sendToServer(new DirectClickItemPacket(true, InteractionHand.MAIN_HAND)); + } + cir.setReturnValue(result.toBoolean()); + } + } + } + + @Inject( + method = "startUseItem", + at = @At( + value = "INVOKE", + target = "Lnet/minecraft/world/phys/HitResult;getType()Lnet/minecraft/world/phys/HitResult$Type;" + ), + locals = LocalCapture.CAPTURE_FAILHARD, + cancellable = true + ) + private void onUse(CallbackInfo ci, InteractionHand[] hands, int var2, int var3, InteractionHand hand, ItemStack stack) { + if (stack.getItem() instanceof DirectClickItem direct) { + TriState result = direct.onUse(level, player, stack, hand); + if (result != TriState.DEFAULT) { + if (result == TriState.TRUE) { + PortalCubedPackets.sendToServer(new DirectClickItemPacket(false, hand)); + } + ci.cancel(); + } + } + } +} diff --git a/src/main/java/io/github/fusionflux/portalcubed/packet/ClientboundPacket.java b/src/main/java/io/github/fusionflux/portalcubed/packet/ClientboundPacket.java new file mode 100644 index 00000000..cfe533f2 --- /dev/null +++ b/src/main/java/io/github/fusionflux/portalcubed/packet/ClientboundPacket.java @@ -0,0 +1,12 @@ +package io.github.fusionflux.portalcubed.packet; + +import net.fabricmc.fabric.api.networking.v1.FabricPacket; +import net.fabricmc.fabric.api.networking.v1.PacketSender; +import net.minecraft.client.player.LocalPlayer; + +import org.quiltmc.loader.api.minecraft.ClientOnly; + +public interface ClientboundPacket extends FabricPacket { + @ClientOnly + void handle(LocalPlayer player, PacketSender responder); +} diff --git a/src/main/java/io/github/fusionflux/portalcubed/packet/PortalCubedPackets.java b/src/main/java/io/github/fusionflux/portalcubed/packet/PortalCubedPackets.java new file mode 100644 index 00000000..09048c21 --- /dev/null +++ b/src/main/java/io/github/fusionflux/portalcubed/packet/PortalCubedPackets.java @@ -0,0 +1,51 @@ +package io.github.fusionflux.portalcubed.packet; + +import io.github.fusionflux.portalcubed.PortalCubed; +import io.github.fusionflux.portalcubed.packet.serverbound.DirectClickItemPacket; +import net.fabricmc.api.EnvType; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; +import net.fabricmc.fabric.api.networking.v1.FabricPacket; +import net.fabricmc.fabric.api.networking.v1.PacketType; +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; +import net.minecraft.network.FriendlyByteBuf; + +import net.minecraft.server.level.ServerPlayer; + +import org.quiltmc.loader.api.minecraft.ClientOnly; +import org.quiltmc.loader.api.minecraft.MinecraftQuiltLoader; + +import java.util.function.Function; + +public class PortalCubedPackets { + public static final PacketType DIRECT_CLICK_ITEM = serverbound("direct_click_item", DirectClickItemPacket::new); + + public static void init() { + } + + private static PacketType clientbound(String name, Function factory) { + PacketType type = PacketType.create(PortalCubed.id(name), factory); + if (MinecraftQuiltLoader.getEnvironmentType() == EnvType.CLIENT) { + registerClientReceiver(type); + } + return type; + } + + private static PacketType serverbound(String name, Function factory) { + PacketType type = PacketType.create(PortalCubed.id(name), factory); + ServerPlayNetworking.registerGlobalReceiver(type, ServerboundPacket::handle); + return type; + } + + private static void registerClientReceiver(PacketType type) { + ClientPlayNetworking.registerGlobalReceiver(type, ClientboundPacket::handle); + } + + public static void sendToClient(ServerPlayer player, T packet) { + ServerPlayNetworking.send(player, packet); + } + + @ClientOnly + public static void sendToServer(T packet) { + ClientPlayNetworking.send(packet); + } +} diff --git a/src/main/java/io/github/fusionflux/portalcubed/packet/ServerboundPacket.java b/src/main/java/io/github/fusionflux/portalcubed/packet/ServerboundPacket.java new file mode 100644 index 00000000..8e00552c --- /dev/null +++ b/src/main/java/io/github/fusionflux/portalcubed/packet/ServerboundPacket.java @@ -0,0 +1,9 @@ +package io.github.fusionflux.portalcubed.packet; + +import net.fabricmc.fabric.api.networking.v1.FabricPacket; +import net.fabricmc.fabric.api.networking.v1.PacketSender; +import net.minecraft.server.level.ServerPlayer; + +public interface ServerboundPacket extends FabricPacket { + void handle(ServerPlayer player, PacketSender responder); +} diff --git a/src/main/java/io/github/fusionflux/portalcubed/packet/serverbound/DirectClickItemPacket.java b/src/main/java/io/github/fusionflux/portalcubed/packet/serverbound/DirectClickItemPacket.java new file mode 100644 index 00000000..da548cf6 --- /dev/null +++ b/src/main/java/io/github/fusionflux/portalcubed/packet/serverbound/DirectClickItemPacket.java @@ -0,0 +1,49 @@ +package io.github.fusionflux.portalcubed.packet.serverbound; + +import io.github.fusionflux.portalcubed.framework.item.DirectClickItem; +import io.github.fusionflux.portalcubed.packet.PortalCubedPackets; +import io.github.fusionflux.portalcubed.packet.ServerboundPacket; +import net.fabricmc.fabric.api.networking.v1.PacketSender; +import net.fabricmc.fabric.api.networking.v1.PacketType; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.InteractionHand; +import net.minecraft.world.item.ItemStack; + +public class DirectClickItemPacket implements ServerboundPacket { + private final boolean attack; + private final InteractionHand hand; + + public DirectClickItemPacket(boolean attack, InteractionHand hand) { + this.attack = attack; + this.hand = hand; + } + + public DirectClickItemPacket(FriendlyByteBuf buf) { + this.attack = buf.readBoolean(); + this.hand = buf.readEnum(InteractionHand.class); + } + + @Override + public void write(FriendlyByteBuf buf) { + buf.writeBoolean(attack); + buf.writeEnum(hand); + } + + @Override + public PacketType getType() { + return PortalCubedPackets.DIRECT_CLICK_ITEM; + } + + @Override + public void handle(ServerPlayer player, PacketSender responder) { + ItemStack stack = player.getItemInHand(hand); + if (stack.getItem() instanceof DirectClickItem direct) { + if (this.attack) { + direct.onAttack(player.level(), player, stack); + } else { + direct.onUse(player.level(), player, stack, hand); + } + } + } +} diff --git a/src/main/resources/portalcubed.mixins.json b/src/main/resources/portalcubed.mixins.json index e9d8ef4f..ffc0620e 100644 --- a/src/main/resources/portalcubed.mixins.json +++ b/src/main/resources/portalcubed.mixins.json @@ -1,11 +1,12 @@ { "required": true, "minVersion": "0.8", - "package": "io.github.fusionflux.modid.mixin", + "package": "io.github.fusionflux.portalcubed.mixin", "compatibilityLevel": "JAVA_17", "mixins": [ ], "client": [ + "MinecraftMixin" ], "injectors": { "defaultRequire": 1