Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ dependencies {

implementation fg.deobf("maven.modrinth:create-tweaked-controllers:1.20.1-1.2.5")

implementation fg.deobf("maven.modrinth:create-tfmg:1.0.2f-forge")

//runtimeOnly fg.deobf("maven.modrinth:eureka:9gbnRz82")
runtimeOnly fg.deobf("maven.modrinth:vlib:1.20.1-0.1.0+forge")
runtimeOnly fg.deobf("org.valkyrienskies:clockwork-forge:1.20.1-0.4.0-forge-a764c6701b") {transitive = false }
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/edn/stratodonut/drivebywire/WireBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.simibubi.create.foundation.data.SharedProperties;
import com.tterrag.registrate.util.entry.BlockEntry;
import edn.stratodonut.drivebywire.blocks.ControllerHubBlock;
import edn.stratodonut.drivebywire.blocks.TFMGEngineControllerHubBlock;
import edn.stratodonut.drivebywire.blocks.TweakedControllerHubBlock;
import edn.stratodonut.drivebywire.blocks.WireNetworkBackupBlock;
import net.minecraft.resources.ResourceLocation;
Expand Down Expand Up @@ -40,5 +41,12 @@ public class WireBlocks {
.simpleItem()
.register();

public static final BlockEntry<TFMGEngineControllerHubBlock> TFMG_ENGINE_CONTROLLER_HUB =
REGISTRATE.block("tfmg_engine_controller_hub", TFMGEngineControllerHubBlock::new)
.initialProperties(SharedProperties::copperMetal)
.transform(axeOrPickaxe())
.simpleItem()
.register();

public static void register() {}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package edn.stratodonut.drivebywire;

import com.tterrag.registrate.util.entry.RegistryEntry;
import edn.stratodonut.drivebywire.blocks.TFMGEngineControllerHubBlock;
import edn.stratodonut.drivebywire.blocks.TweakedControllerHubBlock;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
Expand Down Expand Up @@ -39,6 +40,9 @@ public static boolean include(Object thing) {
if (!ModList.get().isLoaded("create_tweaked_controllers")) {
if (thing instanceof TweakedControllerHubBlock) return false;
}
if (!ModList.get().isLoaded("tfmg")) {
if (thing instanceof TFMGEngineControllerHubBlock) return false;
}
return true;
}

Expand Down
6 changes: 5 additions & 1 deletion src/main/java/edn/stratodonut/drivebywire/WirePackets.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ public enum WirePackets {
CREATE_CONNECTION(WireAddConnectionPacket.class, WireAddConnectionPacket::new, PLAY_TO_SERVER),
REMOVE_CONNECTION(WireRemoveConnectionPacket.class, WireRemoveConnectionPacket::new, PLAY_TO_SERVER),
REQUEST_SYNC(WireNetworkRequestSyncPacket.class, WireNetworkRequestSyncPacket::new, PLAY_TO_SERVER),
LINK_NETWORKS(WireLinkNetworksPacket .class, WireLinkNetworksPacket::new, PLAY_TO_SERVER);
LINK_NETWORKS(WireLinkNetworksPacket .class, WireLinkNetworksPacket::new, PLAY_TO_SERVER),

TFMG_STEERING_INPUT(TFMGSteeringInputPacket.class, TFMGSteeringInputPacket::new, PLAY_TO_SERVER);

// DO NOT TOUCH ANYTHING BELOW THIS LINE, THANKS CREATE

Expand All @@ -47,6 +49,8 @@ public static void registerPackets() {

for (WirePackets packet : values())
packet.packetType.register();

System.out.println("[DBW] Packets registered");
}

public static SimpleChannel getChannel() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package edn.stratodonut.drivebywire.blocks;

import edn.stratodonut.drivebywire.WireSounds;
import edn.stratodonut.drivebywire.compat.TFMGEngineControllerWireServerHandler;
import edn.stratodonut.drivebywire.mixinducks.TFMGControllerDuck;
import edn.stratodonut.drivebywire.util.HubItem;
import edn.stratodonut.drivebywire.wire.MultiChannelWireSource;
import net.minecraft.core.BlockPos;
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.ItemStack;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class TFMGEngineControllerHubBlock extends Block implements MultiChannelWireSource{
private static final List<String> channels = List.of(
TFMGEngineControllerWireServerHandler.STEER_LEFT,
TFMGEngineControllerWireServerHandler.STEER_RIGHT,

TFMGEngineControllerWireServerHandler.ENGINE_STARTED,

TFMGEngineControllerWireServerHandler.SHIFT_REVERSE,
TFMGEngineControllerWireServerHandler.SHIFT_NEUTRAL,
TFMGEngineControllerWireServerHandler.SHIFT_1,
TFMGEngineControllerWireServerHandler.SHIFT_2,
TFMGEngineControllerWireServerHandler.SHIFT_3,
TFMGEngineControllerWireServerHandler.SHIFT_4,
TFMGEngineControllerWireServerHandler.SHIFT_5,
TFMGEngineControllerWireServerHandler.SHIFT_6,

TFMGEngineControllerWireServerHandler.PEDAL_CLUTCH,
TFMGEngineControllerWireServerHandler.PEDAL_BRAKE,
TFMGEngineControllerWireServerHandler.PEDAL_GAS
);

public TFMGEngineControllerHubBlock(Properties props) {
super(props);
}

@Override
public @NotNull InteractionResult use(
@NotNull BlockState state,
@NotNull Level level,
@NotNull BlockPos pos,
@NotNull Player player,
@NotNull InteractionHand hand,
@NotNull BlockHitResult hit
) {
ItemStack stack = player.getItemInHand(hand);

if (stack.getItem() instanceof TFMGControllerDuck) {
HubItem.putHub(stack, pos);
if (!level.isClientSide) {
level.playSound(null, pos, WireSounds.PLUG_IN.get(), SoundSource.BLOCKS, 1, 1);
player.displayClientMessage(
Component.literal("TFMG Controller connected!"), true
);
}
return InteractionResult.SUCCESS;
}

return super.use(state, level, pos, player, hand, hit);
}

@Override
public VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext ctx) {
return ControllerHubBlock.BOTTOM_AABB;
}

@Override
public List<String> wire$getChannels() {
return channels;
}

@Override
public @NotNull String wire$nextChannel(String current, boolean forward) {
int idx = channels.indexOf(current);
if (idx == -1) return channels.get(0);
return channels.get(Math.floorMod(idx + (forward ? 1 : -1), channels.size()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package edn.stratodonut.drivebywire.client;

import net.minecraft.core.BlockPos;

public final class TFMGClientControllerTracker {

private static BlockPos current;

public static void set(BlockPos pos) {
current = pos;
}

public static void clear() {
current = null;
}

public static BlockPos get() {
return current;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package edn.stratodonut.drivebywire.compat;

import edn.stratodonut.drivebywire.wire.ShipWireNetworkManager;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;

public class TFMGEngineControllerWireServerHandler {

public static final String STEER_LEFT = "tfmg_steer_left";
public static final String STEER_RIGHT = "tfmg_steer_right";

public static final String ENGINE_STARTED = "tfmg_engine_started";

public static final String SHIFT_REVERSE = "tfmg_shift_reverse";
public static final String SHIFT_NEUTRAL = "tfmg_shift_neutral";
public static final String SHIFT_1 = "tfmg_shift_1";
public static final String SHIFT_2 = "tfmg_shift_2";
public static final String SHIFT_3 = "tfmg_shift_3";
public static final String SHIFT_4 = "tfmg_shift_4";
public static final String SHIFT_5 = "tfmg_shift_5";
public static final String SHIFT_6 = "tfmg_shift_6";

public static final String PEDAL_CLUTCH = "tfmg_pedal_clutch";
public static final String PEDAL_BRAKE = "tfmg_pedal_brake";
public static final String PEDAL_GAS = "tfmg_pedal_gas";

public static void set(Level level, BlockPos pos, String channel, boolean active) {
ShipWireNetworkManager.trySetSignalAt(
level,
pos,
channel,
active ? 15 : 0
);
}

public static void setAnalog(Level level, BlockPos pos, String channel, float value) {
// clamp between 0 and 1
float clamped = Math.max(0f, Math.min(1f, value));

int strength = (int)(clamped * 15f);

ShipWireNetworkManager.trySetSignalAt(
level,
pos,
channel,
strength
);
}

public static void reset(Level level, BlockPos pos) {
set(level, pos, STEER_LEFT, false);
set(level, pos, STEER_RIGHT, false);

set(level, pos, ENGINE_STARTED, false);

set(level, pos, SHIFT_REVERSE, false);
set(level, pos, SHIFT_NEUTRAL, false);
set(level, pos, SHIFT_1, false);
set(level, pos, SHIFT_2, false);
set(level, pos, SHIFT_3, false);
set(level, pos, SHIFT_4, false);
set(level, pos, SHIFT_5, false);
set(level, pos, SHIFT_6, false);

set(level, pos, PEDAL_CLUTCH, false);
set(level, pos, PEDAL_BRAKE, false);
set(level, pos, PEDAL_GAS, false);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package edn.stratodonut.drivebywire.compat;

import net.minecraft.core.BlockPos;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class TFMGSteeringState {

private static final Map<BlockPos, Float> STEER = new ConcurrentHashMap<>();

public static void set(BlockPos pos, float steer) {
STEER.put(pos, steer);
}

public static float getSteer(BlockPos pos) {
return STEER.getOrDefault(pos, 0f);
}

public static void clear(BlockPos pos) {
STEER.remove(pos);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package edn.stratodonut.drivebywire.mixin.client;

import com.drmangotea.tfmg.content.engines.engine_controller.EngineControllerClientHandler;
import edn.stratodonut.drivebywire.client.TFMGClientControllerTracker;
import net.minecraft.core.BlockPos;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@OnlyIn(Dist.CLIENT)
@Mixin(EngineControllerClientHandler.class)
public class TFMGEngineControllerClientHandlerMixin {

static {
System.out.println("[DBW] TFMG EngineControllerClientHandler mixin loaded");
}

@Inject(method = "activateInLectern", at = @At("HEAD"), remap = false)
private static void drivebywire$onActivate(BlockPos pos, CallbackInfo ci) {
TFMGClientControllerTracker.set(pos);
}

@Inject(method = "deactivateInLectern", at = @At("HEAD"), remap = false)
private static void drivebywire$onDeactivate(CallbackInfo ci) {
TFMGClientControllerTracker.clear();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package edn.stratodonut.drivebywire.mixin.client;

import com.drmangotea.tfmg.content.engines.engine_controller.EngineControllerBlockEntity;
import edn.stratodonut.drivebywire.WirePackets;
import edn.stratodonut.drivebywire.client.TFMGClientControllerTracker;
import edn.stratodonut.drivebywire.network.TFMGSteeringInputPacket;
import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Pseudo;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@OnlyIn(Dist.CLIENT)
@Pseudo
@Mixin(EngineControllerBlockEntity.class)
public abstract class TFMGEngineControllerClientSteeringMixin {

private float drivebywire$lastSent = 0f;

@Inject(method = "tick", at = @At("TAIL"), remap = false)
private void drivebywire$captureSteering(CallbackInfo ci) {
EngineControllerBlockEntity self = (EngineControllerBlockEntity)(Object)this;
Level level = self.getLevel();

if (level == null || !level.isClientSide) return;

BlockPos active = TFMGClientControllerTracker.get();
if (active == null || !active.equals(self.getBlockPos())) return;

float angle = self.steeringWheelAngle.getValue();

final float DEADZONE = 4.0f;
if (Math.abs(angle) < DEADZONE) {
angle = 0f;
}

final float MAX_STEER = 38f;
float normalized = angle / MAX_STEER;
normalized = Math.max(-1f, Math.min(1f, normalized));

float delta = Math.abs(normalized - drivebywire$lastSent);

if (normalized == 0f && drivebywire$lastSent != 0f) {
drivebywire$lastSent = 0f;

WirePackets.getChannel().sendToServer(
new TFMGSteeringInputPacket(self.getBlockPos(), 0f)
);
return;
}

if (delta < 0.02f) {
return;
}

drivebywire$lastSent = normalized;

WirePackets.getChannel().sendToServer(
new TFMGSteeringInputPacket(self.getBlockPos(), normalized)
);
}
}
Loading