Skip to content

Commit

Permalink
Progress? im not sure...
Browse files Browse the repository at this point in the history
  • Loading branch information
enjarai committed Apr 1, 2024
1 parent 84df973 commit 95c2b3f
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class ToastUtil {
public static void toasty(String key) {
MinecraftClient.getInstance().getToastManager().add(SystemToast.create(
MinecraftClient.getInstance(),
SystemToast.Type.TUTORIAL_HINT,
SystemToast.Type.PERIODIC_NOTIFICATION,
Text.translatable("toast.do_a_barrel_roll"),
Text.translatable("toast.do_a_barrel_roll." + key)
));
Expand Down
2 changes: 1 addition & 1 deletion fabric/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ dependencies {
exclude group: "net.fabricmc"
}

modCompileOnly("dev.isxander:controlify:${project.controlify_version}") {
modImplementation("dev.isxander:controlify:${project.controlify_version}") {
exclude group: "net.fabricmc"
exclude group: "maven.modrinth"
exclude group: "com.github.CaffeineMC"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import dev.isxander.controlify.api.bind.BindingSupplier;
import dev.isxander.controlify.api.bind.ControlifyBindingsApi;
import dev.isxander.controlify.api.entrypoint.ControlifyEntrypoint;
import dev.isxander.controlify.bindings.GamepadBinds;
import dev.isxander.controlify.controller.input.GamepadInputs;
import net.minecraft.text.Text;
import net.minecraft.util.math.MathHelper;
import nl.enjarai.doabarrelroll.DoABarrelRoll;
Expand All @@ -15,6 +15,8 @@
import nl.enjarai.doabarrelroll.api.rotation.RotationInstant;
import nl.enjarai.doabarrelroll.config.ModConfig;

import static dev.isxander.controlify.controller.input.GamepadInputs.*;

public class ControlifyCompat implements ControlifyEntrypoint {
public static BindingSupplier PITCH_UP;
public static BindingSupplier PITCH_DOWN;
Expand All @@ -26,22 +28,27 @@ public class ControlifyCompat implements ControlifyEntrypoint {
public static BindingSupplier THRUST_BACKWARD;

private RotationInstant applyToRotation(RotationInstant rotationDelta, RollContext context) {
var controller = ControlifyApi.get().currentController();
var sensitivity = ModConfig.INSTANCE.getControllerSensitivity();
var perhapsController = ControlifyApi.get().getCurrentController();
if (perhapsController.isPresent()) {
var controller = perhapsController.get();
var sensitivity = ModConfig.INSTANCE.getControllerSensitivity();

if (PITCH_UP.onController(controller) == null) return rotationDelta;

if (PITCH_UP.onController(controller) == null) return rotationDelta;
double multiplier = context.getRenderDelta() * 1200;

double multiplier = context.getRenderDelta() * 1200;
double pitchAxis = PITCH_DOWN.onController(controller).state() - PITCH_UP.onController(controller).state();
double yawAxis = YAW_RIGHT.onController(controller).state() - YAW_LEFT.onController(controller).state();
double rollAxis = ROLL_RIGHT.onController(controller).state() - ROLL_LEFT.onController(controller).state();

float pitchAxis = PITCH_DOWN.onController(controller).state() - PITCH_UP.onController(controller).state();
float yawAxis = YAW_RIGHT.onController(controller).state() - YAW_LEFT.onController(controller).state();
float rollAxis = ROLL_RIGHT.onController(controller).state() - ROLL_LEFT.onController(controller).state();
pitchAxis *= multiplier * sensitivity.pitch;
yawAxis *= multiplier * sensitivity.yaw;
rollAxis *= multiplier * sensitivity.roll;

pitchAxis *= multiplier * sensitivity.pitch;
yawAxis *= multiplier * sensitivity.yaw;
rollAxis *= multiplier * sensitivity.roll;
return rotationDelta.add(pitchAxis, yawAxis, rollAxis);
}

return rotationDelta.add(pitchAxis, yawAxis, rollAxis);
return rotationDelta;
}

public static double getThrustModifier() {
Expand All @@ -68,42 +75,42 @@ public static RotationInstant manageThrottle(RotationInstant rotationInstant, Ro
public void onControlifyPreInit(ControlifyApi controlifyApi) {
var bindings = ControlifyBindingsApi.get();
PITCH_UP = bindings.registerBind(DoABarrelRoll.id("pitch_up"), builder -> builder
.defaultBind(GamepadBinds.RIGHT_STICK_FORWARD)
.defaultBind(getBind(RIGHT_STICK_AXIS_UP))
.category(Text.translatable("controlify.category.do_a_barrel_roll.do_a_barrel_roll"))
.name(Text.translatable("controlify.bind.do_a_barrel_roll.pitch_up"))
);
PITCH_DOWN = bindings.registerBind(DoABarrelRoll.id("pitch_down"), builder -> builder
.defaultBind(GamepadBinds.RIGHT_STICK_BACKWARD)
.defaultBind(getBind(RIGHT_STICK_AXIS_DOWN))
.category(Text.translatable("controlify.category.do_a_barrel_roll.do_a_barrel_roll"))
.name(Text.translatable("controlify.bind.do_a_barrel_roll.pitch_down"))
);
ROLL_LEFT = bindings.registerBind(DoABarrelRoll.id("roll_left"), builder -> builder
.defaultBind(GamepadBinds.RIGHT_STICK_LEFT)
.defaultBind(getBind(RIGHT_STICK_AXIS_LEFT))
.category(Text.translatable("controlify.category.do_a_barrel_roll.do_a_barrel_roll"))
.name(Text.translatable("controlify.bind.do_a_barrel_roll.roll_left"))
);
ROLL_RIGHT = bindings.registerBind(DoABarrelRoll.id("roll_right"), builder -> builder
.defaultBind(GamepadBinds.RIGHT_STICK_RIGHT)
.defaultBind(getBind(RIGHT_STICK_AXIS_RIGHT))
.category(Text.translatable("controlify.category.do_a_barrel_roll.do_a_barrel_roll"))
.name(Text.translatable("controlify.bind.do_a_barrel_roll.roll_right"))
);
YAW_LEFT = bindings.registerBind(DoABarrelRoll.id("yaw_left"), builder -> builder
.defaultBind(GamepadBinds.LEFT_STICK_LEFT)
.defaultBind(getBind(LEFT_STICK_AXIS_LEFT))
.category(Text.translatable("controlify.category.do_a_barrel_roll.do_a_barrel_roll"))
.name(Text.translatable("controlify.bind.do_a_barrel_roll.yaw_left"))
);
YAW_RIGHT = bindings.registerBind(DoABarrelRoll.id("yaw_right"), builder -> builder
.defaultBind(GamepadBinds.LEFT_STICK_RIGHT)
.defaultBind(getBind(LEFT_STICK_AXIS_RIGHT))
.category(Text.translatable("controlify.category.do_a_barrel_roll.do_a_barrel_roll"))
.name(Text.translatable("controlify.bind.do_a_barrel_roll.yaw_right"))
);
THRUST_FORWARD = bindings.registerBind(DoABarrelRoll.id("thrust_forward"), builder -> builder
.defaultBind(GamepadBinds.LEFT_STICK_FORWARD)
.defaultBind(getBind(LEFT_STICK_AXIS_UP))
.category(Text.translatable("controlify.category.do_a_barrel_roll.do_a_barrel_roll"))
.name(Text.translatable("controlify.bind.do_a_barrel_roll.thrust_forward"))
);
THRUST_BACKWARD = bindings.registerBind(DoABarrelRoll.id("thrust_backward"), builder -> builder
.defaultBind(GamepadBinds.LEFT_STICK_BACKWARD)
.defaultBind(getBind(LEFT_STICK_AXIS_DOWN))
.category(Text.translatable("controlify.category.do_a_barrel_roll.do_a_barrel_roll"))
.name(Text.translatable("controlify.bind.do_a_barrel_roll.thrust_backward"))
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package nl.enjarai.doabarrelroll;

import net.minecraft.util.Identifier;
import net.minecraftforge.client.ConfigScreenHandler;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.loading.FMLLoader;
import net.minecraftforge.network.simple.SimpleChannel;
import net.neoforged.fml.ModLoadingContext;
import net.neoforged.fml.common.Mod;
import net.neoforged.fml.loading.FMLLoader;
import net.neoforged.neoforge.client.ConfigScreenHandler;
import nl.enjarai.doabarrelroll.config.ModConfigScreen;
import nl.enjarai.doabarrelroll.platform.services.ClientNetworkUtils;
import nl.enjarai.doabarrelroll.platform.services.ServerNetworkUtils;
Expand All @@ -15,7 +14,6 @@

@Mod(DoABarrelRoll.MODID)
public class DoABarrelRollForge {
public static final HashMap<Identifier, SimpleChannel> NETWORK_CHANNELS = new HashMap<>();
public static final HashMap<Identifier, List<ClientNetworkUtils.PacketListener>> CLIENT_LISTENERS = new HashMap<>();
public static final HashMap<Identifier, List<ServerNetworkUtils.PacketListener>> SERVER_LISTENERS = new HashMap<>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package nl.enjarai.doabarrelroll;

import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraftforge.server.permission.PermissionAPI;
import net.minecraftforge.server.permission.nodes.PermissionDynamicContext;
import net.minecraftforge.server.permission.nodes.PermissionDynamicContextKey;
import net.minecraftforge.server.permission.nodes.PermissionNode;
import net.minecraftforge.server.permission.nodes.PermissionTypes;
import net.neoforged.neoforge.server.permission.PermissionAPI;
import net.neoforged.neoforge.server.permission.nodes.PermissionDynamicContext;
import net.neoforged.neoforge.server.permission.nodes.PermissionDynamicContextKey;
import net.neoforged.neoforge.server.permission.nodes.PermissionNode;
import net.neoforged.neoforge.server.permission.nodes.PermissionTypes;
import org.jetbrains.annotations.Nullable;

import java.util.List;
Expand Down
12 changes: 12 additions & 0 deletions neoforge/src/main/java/nl/enjarai/doabarrelroll/SillyPayload.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package nl.enjarai.doabarrelroll;

import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.packet.CustomPayload;
import net.minecraft.util.Identifier;

public record SillyPayload(PacketByteBuf buf, Identifier id) implements CustomPayload {
@Override
public void write(PacketByteBuf buf) {
buf.writeBytes(buf);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package nl.enjarai.doabarrelroll;

import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.server.permission.events.PermissionGatherEvent;
import net.minecraftforge.server.permission.nodes.PermissionNode;
import net.minecraft.util.Identifier;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.Mod;
import net.neoforged.neoforge.event.TickEvent;
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
import net.neoforged.neoforge.network.event.RegisterPayloadHandlerEvent;
import net.neoforged.neoforge.network.registration.IPayloadRegistrar;
import net.neoforged.neoforge.server.permission.events.PermissionGatherEvent;
import net.neoforged.neoforge.server.permission.nodes.PermissionNode;

import java.util.Objects;

@Mod.EventBusSubscriber
public class WhyIsTherePublicTransportationInThisModLoader {
Expand All @@ -28,4 +33,24 @@ public static void serverTick(TickEvent.ServerTickEvent event) {
EventCallbacks.serverTick(event.getServer());
}
}

@SubscribeEvent
public static void register(final RegisterPayloadHandlerEvent event) {
final IPayloadRegistrar registrar = event.registrar(DoABarrelRoll.MODID);
registerChannel(registrar, DoABarrelRoll.HANDSHAKE_CHANNEL);
registerChannel(registrar, DoABarrelRoll.ROLL_CHANNEL);
}

private static void registerChannel(IPayloadRegistrar registrar, Identifier id) {
registrar.play(id, buf -> new SillyPayload(buf, id), (payload, ctx) -> {
if (ctx.flow().getReceptionSide().isServer()) {
DoABarrelRollForge.SERVER_LISTENERS.get(id).forEach(
listener -> listener.accept(((ServerPlayerEntity) ctx.player().orElseThrow()).networkHandler, payload.buf(),
replyBuf -> ctx.replyHandler().send(new SillyPayload(replyBuf, id))));
} else {
DoABarrelRollForge.CLIENT_LISTENERS.get(id).forEach(
listener -> listener.accept(payload.buf(), replyBuf -> ctx.replyHandler().send(new SillyPayload(replyBuf, id))));
}
});
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package nl.enjarai.doabarrelroll;

import net.minecraft.client.MinecraftClient;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.ClientPlayerNetworkEvent;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.Mod;
import net.neoforged.neoforge.client.event.ClientPlayerNetworkEvent;
import net.neoforged.neoforge.event.TickEvent;

@Mod.EventBusSubscriber(Dist.CLIENT)
public class WhyIsTherePublicTransportationInThisModLoaderClient {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package nl.enjarai.doabarrelroll;

import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.RegisterKeyMappingsEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.Mod;
import net.neoforged.neoforge.client.event.RegisterKeyMappingsEvent;

@Mod.EventBusSubscriber(value = Dist.CLIENT, bus = Mod.EventBusSubscriber.Bus.MOD)
public class WhyIsTherePublicTransportationInThisModLoaderClientMod {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package nl.enjarai.doabarrelroll.platform;

import net.minecraft.client.MinecraftClient;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.util.Identifier;
import net.minecraftforge.network.PacketDistributor;
import nl.enjarai.doabarrelroll.DoABarrelRollForge;
import nl.enjarai.doabarrelroll.SillyPayload;
import nl.enjarai.doabarrelroll.platform.services.ClientNetworkUtils;

import java.util.ArrayList;

public class ForgeClientNetworkUtils implements ClientNetworkUtils {
@Override
public void sendPacket(Identifier channel, PacketByteBuf buf) {
DoABarrelRollForge.NETWORK_CHANNELS.get(channel).send(PacketDistributor.SERVER.noArg(), buf);
var handler = MinecraftClient.getInstance().getNetworkHandler();
if (handler != null) {
handler.send(new SillyPayload(buf, channel));
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
package nl.enjarai.doabarrelroll.platform;

import net.minecraft.client.gui.screen.Screen;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.util.Identifier;
import net.minecraftforge.fml.loading.LoadingModList;
import net.minecraftforge.network.NetworkRegistry;
import net.neoforged.fml.loading.LoadingModList;
import nl.enjarai.doabarrelroll.DoABarrelRoll;
import nl.enjarai.doabarrelroll.DoABarrelRollForge;
import nl.enjarai.doabarrelroll.ModPermissions;
import nl.enjarai.doabarrelroll.platform.services.PlatformHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Objects;

public class ForgePlatformHelper implements PlatformHelper {
@Override
public String getPlatformName() {
Expand All @@ -33,27 +28,7 @@ public boolean checkPermission(ServerPlayNetworkHandler source, String permissio

@Override
public void registerNetworkChannels(Identifier... channels) {
for (var channel : channels) {
var frogeChannel = NetworkRegistry.newSimpleChannel(channel, () -> "", s -> true, s -> true);
DoABarrelRollForge.NETWORK_CHANNELS.put(channel, frogeChannel);
frogeChannel.registerMessage(0, PacketByteBuf.class, (bufSource, bufDest) -> bufDest.writeBytes(bufSource), buf -> {
buf.retain();
return buf;
}, (buf, contextSupplier) -> {
var ctx = contextSupplier.get();
ctx.enqueueWork(() -> {
if (ctx.getDirection().getReceptionSide().isServer()) {
DoABarrelRollForge.SERVER_LISTENERS.get(channel).forEach(
listener -> listener.accept(Objects.requireNonNull(ctx.getSender()).networkHandler, buf,
replyBuf -> frogeChannel.sendTo(replyBuf, ctx.getSender().networkHandler.connection, ctx.getDirection().reply())));
} else {
DoABarrelRollForge.CLIENT_LISTENERS.get(channel).forEach(
listener -> listener.accept(buf, frogeChannel::sendToServer));
}
});
ctx.setPacketHandled(true);
});
}
// Network channels on NeoForge are registered in an event, so we'll have to do it manually there...
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Identifier;
import net.minecraftforge.network.PacketDistributor;
import nl.enjarai.doabarrelroll.DoABarrelRollForge;
import nl.enjarai.doabarrelroll.SillyPayload;
import nl.enjarai.doabarrelroll.platform.services.ServerNetworkUtils;

import java.util.ArrayList;
Expand All @@ -15,13 +15,17 @@
public class ForgeServerNetworkUtils implements ServerNetworkUtils {
@Override
public void sendPacket(ServerPlayNetworkHandler handler, Identifier channel, PacketByteBuf buf) {
DoABarrelRollForge.NETWORK_CHANNELS.get(channel).send(PacketDistributor.PLAYER.with(handler::getPlayer), buf);
handler.send(new SillyPayload(buf, channel));
}

@Override
public void sendPacketsToTracking(Entity entity, Predicate<ServerPlayerEntity> predicate, Identifier channel, PacketByteBuf buf) {
// Cant do predicates here apparently? Well fuck me.
DoABarrelRollForge.NETWORK_CHANNELS.get(channel).send(PacketDistributor.TRACKING_ENTITY.with(() -> entity), buf);
entity.getWorld().getPlayers().stream()
.filter(player -> player != entity)
// Temporary solution until I can figure out how to find tracked players on NeoForge.
.filter(player -> entity.getEyePos().squaredDistanceTo(player.getPos()) <= 16 * 10)
.filter(player -> predicate.test((ServerPlayerEntity) player))
.forEach(player -> ((ServerPlayerEntity) player).networkHandler.send(new SillyPayload(buf, channel)));
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions neoforge/src/main/resources/META-INF/mods.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ description='''${mod_description}'''
# the modid of the dependency
modId = "forge" #mandatory
# Does this dependency have to exist - if not, ordering below must be specified
mandatory = true #mandatory
type = "required" #mandatory
# The version range of the dependency
versionRange = "${forge_version_range}" #mandatory
# An ordering relationship for the dependency - BEFORE or AFTER required if the dependency is not mandatory
Expand All @@ -62,7 +62,7 @@ description='''${mod_description}'''
side = "BOTH" # Side this dependency is applied on - 'BOTH', 'CLIENT' or 'SERVER'
[[dependencies.'${mod_id}']]
modId = "minecraft"
mandatory = true
type = "required"
# This version range declares a minimum of the current minecraft version up to but not including the next major version
versionRange = "${forge_minecraft_version_range}"
ordering = "NONE"
Expand Down

0 comments on commit 95c2b3f

Please sign in to comment.