Skip to content

Commit

Permalink
start portal gun, reorganize
Browse files Browse the repository at this point in the history
  • Loading branch information
TropheusJ committed Oct 26, 2023
1 parent 33bb210 commit c9e0f0c
Show file tree
Hide file tree
Showing 24 changed files with 288 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.github.fusionflux.portalcubed.content.portal;

public enum PortalType {
PRIMARY, SECONDARY;
}
Original file line number Diff line number Diff line change
@@ -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) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@MethodsReturnNonnullByDefault
package io.github.fusionflux.portalcubed.content.portal.gun;

import net.minecraft.MethodsReturnNonnullByDefault;
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.fusionflux.portalcubed.registration;
package io.github.fusionflux.portalcubed.framework.registration;

import net.minecraft.client.renderer.RenderType;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<B extends Block> {
ItemBuilder<Item> create(B block, ItemBuilder<Item> builder);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.fusionflux.portalcubed.registration.item;
package io.github.fusionflux.portalcubed.framework.registration.item;

import java.util.function.Consumer;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Boolean> 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();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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<DirectClickItemPacket> DIRECT_CLICK_ITEM = serverbound("direct_click_item", DirectClickItemPacket::new);

public static void init() {
}

private static <T extends ClientboundPacket> PacketType<T> clientbound(String name, Function<FriendlyByteBuf, T> factory) {
PacketType<T> type = PacketType.create(PortalCubed.id(name), factory);
if (MinecraftQuiltLoader.getEnvironmentType() == EnvType.CLIENT) {
registerClientReceiver(type);
}
return type;
}

private static <T extends ServerboundPacket> PacketType<T> serverbound(String name, Function<FriendlyByteBuf, T> factory) {
PacketType<T> type = PacketType.create(PortalCubed.id(name), factory);
ServerPlayNetworking.registerGlobalReceiver(type, ServerboundPacket::handle);
return type;
}

private static <T extends ClientboundPacket> void registerClientReceiver(PacketType<T> type) {
ClientPlayNetworking.registerGlobalReceiver(type, ClientboundPacket::handle);
}

public static <T extends FabricPacket> void sendToClient(ServerPlayer player, T packet) {
ServerPlayNetworking.send(player, packet);
}

@ClientOnly
public static <T extends FabricPacket> void sendToServer(T packet) {
ClientPlayNetworking.send(packet);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
Loading

0 comments on commit c9e0f0c

Please sign in to comment.