-
Notifications
You must be signed in to change notification settings - Fork 22
Lazy flap linking #250
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
Open
blockninja124
wants to merge
3
commits into
1.20.1/main
Choose a base branch
from
1.20.1/lazy_flap_linking
base: 1.20.1/main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Lazy flap linking #250
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...src/main/java/org/valkyrienskies/clockwork/LinkedControllerClientHandlerMixinStorage.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package org.valkyrienskies.clockwork; | ||
|
|
||
| import net.minecraft.core.Direction; | ||
|
|
||
| /** | ||
| * This rather sus class exists for the purpose of storing a public static variable (clicked face) | ||
| * to be set in MixinLinkedControllerItem and accessed from MixinLinkedControllerClientHandler | ||
| */ | ||
| public class LinkedControllerClientHandlerMixinStorage { | ||
| public static Direction face; | ||
|
|
||
| public static void doNothing() { | ||
| System.out.println("I hate kotlin sometimes"); | ||
| } | ||
| } |
75 changes: 75 additions & 0 deletions
75
...lkyrienskies/clockwork/mixin/content/flap_bearing/MixinLinkedControllerClientHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| package org.valkyrienskies.clockwork.mixin.content.flap_bearing; | ||
|
|
||
| import com.llamalad7.mixinextras.injector.wrapoperation.Operation; | ||
| import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; | ||
| import com.llamalad7.mixinextras.sugar.Local; | ||
| import com.simibubi.create.content.redstone.link.LinkBehaviour; | ||
| import com.simibubi.create.content.redstone.link.controller.LinkedControllerClientHandler; | ||
| import com.simibubi.create.content.redstone.link.controller.LinkedControllerItem; | ||
| import com.simibubi.create.foundation.blockEntity.behaviour.BehaviourType; | ||
| import com.simibubi.create.foundation.blockEntity.behaviour.BlockEntityBehaviour; | ||
| import org.apache.commons.lang3.tuple.Pair; | ||
| import net.minecraft.core.BlockPos; | ||
| import net.minecraft.world.level.BlockGetter; | ||
| import org.spongepowered.asm.mixin.Mixin; | ||
| import org.spongepowered.asm.mixin.Shadow; | ||
| import org.spongepowered.asm.mixin.Unique; | ||
| import org.spongepowered.asm.mixin.injection.At; | ||
| import org.spongepowered.asm.mixin.injection.Coerce; | ||
| import org.valkyrienskies.clockwork.ClockworkBlocks; | ||
| import org.valkyrienskies.clockwork.ClockworkPackets; | ||
| import org.valkyrienskies.clockwork.LinkedControllerClientHandlerMixinStorage; | ||
| import org.valkyrienskies.clockwork.content.contraptions.flap.smart_flap.FlapLinkedControllerBindPacket; | ||
|
|
||
|
|
||
| @Mixin(LinkedControllerClientHandler.class) | ||
| public class MixinLinkedControllerClientHandler { | ||
|
|
||
| @Shadow | ||
| private static BlockPos selectedLocation; | ||
|
|
||
| @WrapOperation( | ||
| method = "tick", | ||
| at = @At(value = "INVOKE", target = "Lcom/simibubi/create/foundation/blockEntity/behaviour/BlockEntityBehaviour;get(Lnet/minecraft/world/level/BlockGetter;Lnet/minecraft/core/BlockPos;Lcom/simibubi/create/foundation/blockEntity/behaviour/BehaviourType;)Lcom/simibubi/create/foundation/blockEntity/behaviour/BlockEntityBehaviour;"), | ||
| remap = false | ||
| ) | ||
| private static <T extends BlockEntityBehaviour> T wrapGetBehaviour(BlockGetter be, BlockPos e, BehaviourType<T> reader, Operation<T> original) { | ||
| // Clockwork flap bearing will never return the LinkBehaviour create is wanting, since it's using a custom behaviour. | ||
| // However, we still have to pass a null check, hence the weird sus null LinkBehaviour. | ||
| if (!ClockworkBlocks.SMART_FLAP_BEARING.has(be.getBlockState(e))) return original.call(be, e, reader); | ||
|
|
||
| // Just needs to be a not-null be behaviour to pass a null check | ||
| return (T) LinkBehaviour.receiver(null, Pair.of(null, null), null); | ||
| } | ||
|
|
||
|
|
||
| @WrapOperation( | ||
| method = "tick", | ||
| at = @At(value = "INVOKE", target = "Lme/pepperbell/simplenetworking/SimpleChannel;sendToServer(Lme/pepperbell/simplenetworking/C2SPacket;)V", ordinal = 3), | ||
| require = 0, | ||
| remap = false | ||
| ) | ||
| private static void wrapSendToServerFabric(@Coerce Object instance, @Coerce Object packet, Operation<Void> original, @Local LinkBehaviour l, @Local Integer button) { | ||
| wrapSendToServerCommon(instance, packet, original, l, button); | ||
| } | ||
|
|
||
| @WrapOperation( | ||
| method = "tick", | ||
| at = @At(value = "INVOKE", target = "Lnet/minecraftforge/network/simple/SimpleChannel;sendToServer(Ljava/lang/Object;)V", ordinal = 3), | ||
| require = 0, | ||
| remap = false | ||
| ) | ||
| private static void wrapSendToServerForge(@Coerce Object instance, @Coerce Object packet, Operation<Void> original, @Local LinkBehaviour l, @Local Integer button) { | ||
| wrapSendToServerCommon(instance, packet, original, l, button); | ||
| } | ||
|
|
||
| @Unique | ||
| private static void wrapSendToServerCommon(Object instance, Object packet, Operation<Void> original, LinkBehaviour l, Integer button) { | ||
| // No one else should be stupid enough to be passing in a LinkBehaviour with a null be | ||
| if (l.blockEntity != null) { | ||
| original.call(instance, packet); | ||
| return; | ||
| } | ||
| ClockworkPackets.sendToServer(new FlapLinkedControllerBindPacket(button, selectedLocation, LinkedControllerClientHandlerMixinStorage.face, LinkedControllerItem.class.getName())); | ||
| } | ||
| } |
81 changes: 81 additions & 0 deletions
81
...va/org/valkyrienskies/clockwork/mixin/content/flap_bearing/MixinLinkedControllerItem.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| package org.valkyrienskies.clockwork.mixin.content.flap_bearing; | ||
|
|
||
| import com.simibubi.create.content.redstone.link.controller.LinkedControllerClientHandler; | ||
| import com.simibubi.create.content.redstone.link.controller.LinkedControllerItem; | ||
| import com.simibubi.create.foundation.blockEntity.behaviour.BehaviourType; | ||
| import com.simibubi.create.foundation.blockEntity.behaviour.BlockEntityBehaviour; | ||
| import net.createmod.catnip.data.Iterate; | ||
| import net.minecraft.core.BlockPos; | ||
| import net.minecraft.core.Direction; | ||
| import net.minecraft.world.InteractionResult; | ||
| import net.minecraft.world.entity.player.Player; | ||
| import net.minecraft.world.item.Item; | ||
| import net.minecraft.world.item.ItemStack; | ||
| import net.minecraft.world.item.context.UseOnContext; | ||
| import net.minecraft.world.level.Level; | ||
| import net.minecraft.world.level.block.state.BlockState; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.spongepowered.asm.mixin.Mixin; | ||
| import org.spongepowered.asm.mixin.Shadow; | ||
| import org.spongepowered.asm.mixin.Unique; | ||
| 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.valkyrienskies.clockwork.ClockworkBlocks; | ||
| import org.valkyrienskies.clockwork.LinkedControllerClientHandlerMixinStorage; | ||
| import org.valkyrienskies.clockwork.content.contraptions.flap.dual_link.DualLinkBehaviour; | ||
| import org.valkyrienskies.clockwork.platform.PlatformUtils; | ||
|
|
||
| import static org.valkyrienskies.clockwork.content.contraptions.flap.dual_link.DualLinkHandler.getFrontFacing; | ||
|
|
||
| @Mixin(LinkedControllerItem.class) | ||
| public class MixinLinkedControllerItem { | ||
| @Shadow | ||
| private void toggleBindMode(BlockPos pos) {} | ||
|
|
||
| @Unique | ||
| public Direction clickedFace = null; | ||
|
|
||
| @Inject( | ||
| method = "onItemUseFirst", | ||
| at = @At("HEAD"), | ||
| remap = false, | ||
| cancellable = true | ||
| ) | ||
| private void injectStateCheck(ItemStack stack, UseOnContext ctx, CallbackInfoReturnable<InteractionResult> cir) { | ||
| // region Copied from beginning of create method | ||
| Player player = ctx.getPlayer(); | ||
| if (player == null) return; | ||
| Level world = ctx.getLevel(); | ||
| BlockPos pos = ctx.getClickedPos(); | ||
| BlockState hitState = world.getBlockState(pos); | ||
| // endregion | ||
|
|
||
| if (!player.mayBuild()) return; | ||
| if (player.isShiftKeyDown()) return; | ||
|
|
||
| if (ClockworkBlocks.SMART_FLAP_BEARING.has(hitState)) { | ||
| clickedFace = ctx.getClickedFace(); | ||
|
|
||
| if (!(clickedFace == getFrontFacing(hitState) || clickedFace == getFrontFacing(hitState).getOpposite())) return; | ||
|
|
||
| if (world.isClientSide) | ||
| PlatformUtils.getEnvExecutor(() -> () -> toggleBindMode(ctx.getClickedPos())); | ||
| player.getCooldowns() | ||
| .addCooldown((Item)(Object)this, 2); | ||
| cir.setReturnValue(InteractionResult.SUCCESS); | ||
| cir.cancel(); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| @Inject( | ||
| method = "toggleBindMode", | ||
| at = @At("HEAD"), | ||
| remap = false | ||
| ) | ||
| private void injectToggleBindMode(BlockPos pos, CallbackInfo ci) { | ||
| LinkedControllerClientHandlerMixinStorage.face = clickedFace; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
...ienskies/clockwork/content/contraptions/flap/smart_flap/FlapLinkedControllerBindPacket.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| package org.valkyrienskies.clockwork.content.contraptions.flap.smart_flap | ||
|
|
||
| import com.simibubi.create.AllItems | ||
| import com.simibubi.create.content.redstone.link.RedstoneLinkNetworkHandler | ||
| import com.simibubi.create.content.redstone.link.controller.LinkedControllerItem | ||
| import com.simibubi.create.foundation.blockEntity.behaviour.BehaviourType | ||
| import com.simibubi.create.foundation.blockEntity.behaviour.BlockEntityBehaviour | ||
| import net.minecraft.core.BlockPos | ||
| import net.minecraft.core.Direction | ||
| import net.minecraft.nbt.Tag | ||
| import net.minecraft.network.FriendlyByteBuf | ||
| import net.minecraft.world.InteractionHand | ||
| import net.minecraft.world.item.ItemStack | ||
| import org.valkyrienskies.clockwork.LinkedControllerClientHandlerMixinStorage | ||
| import org.valkyrienskies.clockwork.LinkedControllerClientHandlerMixinStorage.doNothing | ||
| import org.valkyrienskies.clockwork.content.contraptions.flap.dual_link.DualLinkBehaviour | ||
| import org.valkyrienskies.clockwork.content.contraptions.flap.dual_link.DualLinkHandler.getFrontFacing | ||
| import org.valkyrienskies.clockwork.platform.api.network.C2SCWPacket | ||
| import org.valkyrienskies.clockwork.platform.api.network.ServerNetworkContext | ||
|
|
||
| class FlapLinkedControllerBindPacket(var button: Int, var linkLocation: BlockPos, var face: Direction, var itemClassName: String): C2SCWPacket { | ||
| constructor(buffer: FriendlyByteBuf) : this(buffer.readVarInt(), buffer.readBlockPos(), buffer.readEnum(Direction::class.java), buffer.readUtf()) | ||
|
|
||
| override fun write(buffer: FriendlyByteBuf) { | ||
| buffer.writeVarInt(button) | ||
| buffer.writeBlockPos(linkLocation) | ||
| buffer.writeEnum(face) | ||
| buffer.writeUtf(itemClassName) | ||
| } | ||
|
|
||
| override fun handle(context: ServerNetworkContext) { | ||
| context.enqueueWork { | ||
| val player = context.sender | ||
| if (player.isSpectator) return@enqueueWork | ||
|
|
||
| var controller = player.mainHandItem | ||
|
|
||
| // We use reflection for this as a lazy way to use TweakedLinkedControllerItem instead of LinkedControllerItem | ||
| // (also we have to use reflection on it later anyway, so we might as well) | ||
| val itemClass = Class.forName(itemClassName) | ||
|
|
||
| if (!itemClass.isInstance(controller.item)) { | ||
| controller = player.offhandItem | ||
| if (!itemClass.isInstance(controller.item)) return@enqueueWork | ||
| } | ||
|
|
||
| doNothing() | ||
|
|
||
| val getFrequencyItems = itemClass.getDeclaredMethod("getFrequencyItems", ItemStack::class.java) | ||
|
|
||
| val frequencyItems = getFrequencyItems.invoke(null, controller) | ||
|
|
||
| val type: BehaviourType<DualLinkBehaviour> = | ||
| if (face == getFrontFacing(player.level().getBlockState(linkLocation))) DualLinkBehaviour.FRONT_TYPE | ||
| else DualLinkBehaviour.BACK_TYPE | ||
|
|
||
| val linkBehaviour = BlockEntityBehaviour.get(player.level(), linkLocation, type) ?: return@enqueueWork | ||
|
|
||
| // For some BRAIN DEAD REASON, the package for ItemHandler in porting lib and ItemHandler in forge are DIFFERENT | ||
| // They don't even extend a common class, so I'm just using reflection because fuck it. | ||
| val setStackInSlot = frequencyItems::class.java.getDeclaredMethod("setStackInSlot", Integer::class.javaPrimitiveType, ItemStack::class.java) | ||
| val serializeNbt = frequencyItems::class.java.getDeclaredMethod("serializeNBT") | ||
|
|
||
| linkBehaviour.networkKey | ||
| .forEachWithContext { f: RedstoneLinkNetworkHandler.Frequency, first: Boolean -> | ||
| setStackInSlot.invoke(frequencyItems, button * 2 + (if (first) 0 else 1), f.stack | ||
| .copy()) | ||
| } | ||
|
|
||
| controller.tag!! | ||
| .put("Items", serializeNbt.invoke(frequencyItems) as Tag) | ||
| } | ||
| context.setPacketHandled(true) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -64,6 +64,18 @@ repositories { | |
| } | ||
| } | ||
| maven { url = "https://jitpack.io/" } | ||
|
|
||
| exclusiveContent { | ||
| forRepository { | ||
| maven { | ||
| name = "Modrinth" | ||
| url = "https://api.modrinth.com/maven" | ||
| } | ||
| } | ||
| filter { | ||
| includeGroup "maven.modrinth" | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -109,6 +121,8 @@ dependencies { | |
| modCompileOnly("dev.engine-room.flywheel:flywheel-forge-api-${minecraft_version}:${flywheel_forge_version}") | ||
| modRuntimeOnly("dev.engine-room.flywheel:flywheel-forge-${minecraft_version}:${flywheel_forge_version}") | ||
|
|
||
| modCompileOnly("maven.modrinth:valkyrien-warium:0.3.1") | ||
|
||
|
|
||
| // JOML | ||
| compileOnly("org.joml:joml:1.10.4") | ||
| compileOnly("org.joml:joml-primitives:1.10.0") | ||
|
|
@@ -121,6 +135,9 @@ dependencies { | |
| modCompileOnly("cc.tweaked:cc-tweaked-$minecraft_version-forge-api:$cc_version") | ||
| modRuntimeOnly("cc.tweaked:cc-tweaked-$minecraft_version-forge:$cc_version") | ||
|
|
||
| //modRuntimeOnly("maven.modrinth:create-connected:saiZ8AwJ") | ||
| modImplementation ("maven.modrinth:create-tweaked-controllers:aHqvR8LC") | ||
|
|
||
| //Spark profiler | ||
| //modRuntimeOnly("maven.modrinth:spark:1.10.53-forge") | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my attempts at fixing CC, not what caused it to break