Skip to content

Commit

Permalink
pull config values into a data class
Browse files Browse the repository at this point in the history
  • Loading branch information
UpcraftLP committed Apr 23, 2024
1 parent a634ab2 commit 86815d2
Show file tree
Hide file tree
Showing 14 changed files with 164 additions and 23 deletions.
7 changes: 7 additions & 0 deletions Common/src/main/java/dev/cammiescorner/icarus/Icarus.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import com.teamresourceful.resourcefulconfig.common.config.Configurator;
import commonnetwork.api.Network;
import dev.cammiescorner.icarus.network.s2c.SyncConfigValuesPacket;
import dev.cammiescorner.icarus.util.IcarusHelper;
import dev.cammiescorner.icarus.util.ServerPlayerFallbackValues;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;

public class Icarus {

Expand All @@ -19,4 +22,8 @@ public static void init() {
public static ResourceLocation id(String path) {
return new ResourceLocation(MODID, path);
}

public static void onServerStart(MinecraftServer server) {
IcarusHelper.fallbackValues = new ServerPlayerFallbackValues();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.cammiescorner.icarus.api;

public interface IcarusPlayerValues {

float wingsSpeed();

float maxSlowedMultiplier();

boolean armorSlows();

boolean canLoopDeLoop();

boolean canSlowFall();

float exhaustionAmount();

int maxHeightAboveWorld();

boolean maxHeightEnabled();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dev.cammiescorner.icarus.client.IcarusClient;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ItemStack;

import java.util.function.Predicate;

Expand All @@ -10,4 +11,8 @@ public class IcarusAPIClient {
public static void addRenderPredicate(Predicate<LivingEntity> predicate) {
IcarusClient.addRenderPredicate(predicate);
}

public static ItemStack getWingsForRendering(LivingEntity entity) {
return IcarusClient.getWingsForRendering(entity);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package dev.cammiescorner.icarus.client;

import dev.cammiescorner.icarus.api.IcarusPlayerValues;
import dev.cammiescorner.icarus.util.ServerPlayerFallbackValues;

public class ClientPlayerFallbackValues extends ServerPlayerFallbackValues implements IcarusPlayerValues {

private final float wingsSpeed;
private final boolean armorSlows;
private final float maxSlowedMultiplier;
private final boolean canLoopDeLoop;

public ClientPlayerFallbackValues(float wingsSpeed, float maxSlowedMultiplier, boolean armorSlows, boolean canLoopDeLoop) {
this.wingsSpeed = wingsSpeed;
this.armorSlows = armorSlows;
this.maxSlowedMultiplier = maxSlowedMultiplier;
this.canLoopDeLoop = canLoopDeLoop;
}

@Override
public float wingsSpeed() {
return wingsSpeed;
}

@Override
public float maxSlowedMultiplier() {
return maxSlowedMultiplier;
}

@Override
public boolean armorSlows() {
return armorSlows;
}

@Override
public boolean canLoopDeLoop() {
return canLoopDeLoop;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,36 @@
import dev.cammiescorner.icarus.util.IcarusHelper;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.ApiStatus;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

public class IcarusClient {
public static float wingSpeed;
public static float maxSlowedMultiplier;
public static boolean armorSlows;
public static boolean canLoopDeLoop;

private static final List<Predicate<LivingEntity>> renderPredicates = new ArrayList<>();

public static void onPlayerTick(Player player) {
if (player.isFallFlying() && IcarusHelper.hasWings.test(player) && player.zza > 0) {
var cfg = IcarusHelper.getConfigValues(player);
var rotation = player.getLookAngle();
var velocity = player.getDeltaMovement();
float modifier = armorSlows ? Math.max(1F, (player.getArmorValue() / 30F) * maxSlowedMultiplier) : 1F;
float speed = (wingSpeed * (player.getXRot() < -75 && player.getXRot() > -105 ? 2.75F : 1F)) / modifier;
float modifier = cfg.armorSlows() ? Math.max(1F, (player.getArmorValue() / 30F) * cfg.maxSlowedMultiplier()) : 1F;
float speed = (cfg.wingsSpeed() * (player.getXRot() < -75 && player.getXRot() > -105 ? 2.75F : 1F)) / modifier;

player.setDeltaMovement(velocity.add(rotation.x * speed + (rotation.x * 1.5D - velocity.x) * speed,
rotation.y * speed + (rotation.y * 1.5D - velocity.y) * speed,
rotation.z * speed + (rotation.z * 1.5D - velocity.z) * speed));
}
}

@ApiStatus.Internal
public static ItemStack getWingsForRendering(LivingEntity entity) {
return IcarusHelper.getEquippedWings.apply(entity);
}

@ApiStatus.Internal
public static void addRenderPredicate(Predicate<LivingEntity> predicate) {
renderPredicates.add(predicate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import dev.cammiescorner.icarus.api.client.IcarusAPIClient;
import dev.cammiescorner.icarus.client.IcarusClient;
import dev.cammiescorner.icarus.client.IcarusModels;
import dev.cammiescorner.icarus.client.models.*;
import dev.cammiescorner.icarus.init.IcarusItems;
import dev.cammiescorner.icarus.item.WingItem;
import dev.cammiescorner.icarus.util.IcarusHelper;
import net.minecraft.client.model.EntityModel;
import net.minecraft.client.model.geom.EntityModelSet;
import net.minecraft.client.renderer.MultiBufferSource;
Expand Down Expand Up @@ -42,7 +42,7 @@ public WingsLayer(RenderLayerParent<T, M> context, EntityModelSet loader) {

@Override
public void render(PoseStack matrices, MultiBufferSource vertexConsumers, int light, T entity, float limbAngle, float limbDistance, float tickDelta, float animationProgress, float headYaw, float headPitch) {
var stack = IcarusHelper.getEquippedWings.apply(entity);
var stack = IcarusAPIClient.getWingsForRendering(entity);

if (stack.getItem() instanceof WingItem wingItem && IcarusClient.shouldRenderWings(entity)) {
float[] primaryColour = wingItem.getPrimaryColor(stack).getTextureDiffuseColors();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public boolean isUsable(LivingEntity entity, ItemStack stack) {

public boolean onFlightTick(LivingEntity entity, ItemStack wings, int ticks) {
if (IcarusConfig.wingsDurability > 0 && wings.is(IcarusItemTags.MELTS) && !(entity instanceof Player player && player.isCreative())) {
if (ticks % 20 == 0 || (IcarusConfig.maxHeightEnabled && entity.getY() > entity.level().getHeight() + IcarusConfig.maxHeightAboveWorld && ticks % 2 == 0)) {
var cfg = IcarusHelper.getConfigValues(entity);
if (ticks % 20 == 0 || (cfg.maxHeightEnabled() && entity.getY() > entity.level().getHeight() + cfg.maxHeightAboveWorld() && ticks % 2 == 0)) {
wings.hurtAndBreak(1, entity, e -> e.broadcastBreakEvent(EquipmentSlot.CHEST));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.cammiescorner.icarus.mixin.client;

import dev.cammiescorner.icarus.client.IcarusClient;
import dev.cammiescorner.icarus.util.IcarusHelper;
import net.minecraft.client.Minecraft;
import net.minecraft.client.MouseHandler;
Expand All @@ -19,7 +18,7 @@ public class MouseHandlerMixin {

@ModifyArg(method = "turnPlayer", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/player/LocalPlayer;turn(DD)V"), index = 0)
public double changeLookDirectionX(double x) {
if (minecraft.player != null && IcarusClient.canLoopDeLoop && minecraft.player.isFallFlying() && IcarusHelper.hasWings.test(minecraft.player) && (minecraft.player.getXRot() > 90 || minecraft.player.getXRot() < -90))
if (minecraft.player != null && minecraft.player.isFallFlying() && IcarusHelper.hasWings.test(minecraft.player) && IcarusHelper.getConfigValues(minecraft.player).canLoopDeLoop() && (minecraft.player.getXRot() > 90 || minecraft.player.getXRot() < -90))
return -x;

return x;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import commonnetwork.api.Dispatcher;
import commonnetwork.networking.data.PacketContext;
import dev.cammiescorner.icarus.Icarus;
import dev.cammiescorner.icarus.IcarusConfig;
import dev.cammiescorner.icarus.client.IcarusClient;
import dev.cammiescorner.icarus.client.ClientPlayerFallbackValues;
import dev.cammiescorner.icarus.util.IcarusHelper;
import dev.cammiescorner.icarus.util.ServerPlayerFallbackValues;
import net.minecraft.client.Minecraft;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
Expand All @@ -21,7 +22,8 @@ public void encode(FriendlyByteBuf buf) {
}

public static void send(ServerPlayer player) {
var packet = new SyncConfigValuesPacket(IcarusConfig.wingsSpeed, IcarusConfig.maxSlowedMultiplier, IcarusConfig.armorSlows, IcarusConfig.canLoopDeLoop);
var cfg = new ServerPlayerFallbackValues();
var packet = new SyncConfigValuesPacket(cfg.wingsSpeed(), cfg.maxSlowedMultiplier(), cfg.armorSlows(), cfg.canLoopDeLoop());
Dispatcher.sendToClient(packet, player);
}

Expand All @@ -35,11 +37,11 @@ public static SyncConfigValuesPacket decode(FriendlyByteBuf buf) {
}

public static void handle(PacketContext<SyncConfigValuesPacket> ctx) {
Minecraft.getInstance().execute(() -> {
IcarusClient.wingSpeed = ctx.message().wingsSpeed();
IcarusClient.maxSlowedMultiplier = ctx.message().maxSlowedMultiplier();
IcarusClient.armorSlows = ctx.message().armorSlows();
IcarusClient.canLoopDeLoop = ctx.message().canLoopDeLoop();
});
Minecraft.getInstance().execute(() -> IcarusHelper.fallbackValues = new ClientPlayerFallbackValues(
ctx.message().wingsSpeed(),
ctx.message().maxSlowedMultiplier(),
ctx.message().armorSlows(),
ctx.message().canLoopDeLoop()
));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.cammiescorner.icarus.util;

import dev.cammiescorner.icarus.IcarusConfig;
import dev.cammiescorner.icarus.api.IcarusPlayerValues;
import dev.cammiescorner.icarus.api.SlowFallingEntity;
import dev.cammiescorner.icarus.init.IcarusDimensionTags;
import dev.cammiescorner.icarus.init.IcarusItemTags;
Expand All @@ -24,6 +24,7 @@ public class IcarusHelper {
public static Predicate<LivingEntity> hasWings = entity -> false;
public static Function<LivingEntity, ItemStack> getEquippedWings = entity -> ItemStack.EMPTY;
public static BiPredicate<LivingEntity, ItemStack> equipFunc = (entity, stack) -> false;
public static IcarusPlayerValues fallbackValues = new ServerPlayerFallbackValues();

public static boolean onFallFlyingTick(LivingEntity entity, @Nullable ItemStack wings, boolean tick) {
if(!entity.level().isClientSide() && entity.level().registryAccess().registryOrThrow(Registries.DIMENSION).getHolderOrThrow(entity.level().dimension()).is(IcarusDimensionTags.NO_FLYING_ALLOWED)) {
Expand All @@ -43,15 +44,16 @@ public static boolean onFallFlyingTick(LivingEntity entity, @Nullable ItemStack
}

if (tick) {
if ((IcarusConfig.canSlowFall && entity.isShiftKeyDown()) || entity.isUnderWater()) {
var cfg = IcarusHelper.getConfigValues(entity);
if ((cfg.canSlowFall() && entity.isShiftKeyDown()) || entity.isUnderWater()) {
if (entity instanceof Player player) {
stopFlying(player);
}
return false;
}

if (!wings.is(IcarusItemTags.FREE_FLIGHT) && entity instanceof Player player && !player.isCreative()) {
player.getFoodData().addExhaustion(IcarusConfig.exhaustionAmount);
player.getFoodData().addExhaustion(cfg.exhaustionAmount());
if (player.getFoodData().getFoodLevel() <= 6) {
stopFlying(player);
return false;
Expand All @@ -71,6 +73,10 @@ public static boolean onFallFlyingTick(LivingEntity entity, @Nullable ItemStack
return true;
}

public static IcarusPlayerValues getConfigValues(LivingEntity entity) {
return fallbackValues;
}

public static void onPlayerTick(Player player) {
if (((SlowFallingEntity) player).icarus$isSlowFalling()) {
player.fallDistance = 0F;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package dev.cammiescorner.icarus.util;

import dev.cammiescorner.icarus.IcarusConfig;
import dev.cammiescorner.icarus.api.IcarusPlayerValues;

public class ServerPlayerFallbackValues implements IcarusPlayerValues {

@Override
public float wingsSpeed() {
return IcarusConfig.wingsSpeed;
}

@Override
public float maxSlowedMultiplier() {
return IcarusConfig.maxSlowedMultiplier;
}

@Override
public boolean armorSlows() {
return IcarusConfig.armorSlows;
}

@Override
public boolean canLoopDeLoop() {
return IcarusConfig.canLoopDeLoop;
}

@Override
public boolean canSlowFall() {
return IcarusConfig.canSlowFall;
}

@Override
public float exhaustionAmount() {
return IcarusConfig.exhaustionAmount;
}

@Override
public boolean maxHeightEnabled() {
return IcarusConfig.maxHeightEnabled;
}

@Override
public int maxHeightAboveWorld() {
return IcarusConfig.maxHeightAboveWorld;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import dev.emi.trinkets.api.TrinketsApi;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.entity.event.v1.EntityElytraEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.itemgroup.v1.FabricItemGroup;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.minecraft.core.Registry;
Expand Down Expand Up @@ -98,6 +99,8 @@ public void onInitialize() {
return false;
});

ServerLifecycleEvents.SERVER_STARTING.register(Icarus::onServerStart);

IcarusItems.register(new Registrar<>() {
@Override
public <T extends Item> Supplier<T> register(String name, Supplier<? extends T> factory) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.event.server.ServerStartingEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import top.theillusivec4.caelus.api.CaelusApi;
Expand Down Expand Up @@ -57,4 +58,9 @@ public static void onPlayerTick(TickEvent.PlayerTickEvent event) {
public static void onPlayerJoin(PlayerEvent.PlayerLoggedInEvent event) {
IcarusHelper.onServerPlayerJoin((ServerPlayer) event.getEntity());
}

@SubscribeEvent
public static void onServerStart(ServerStartingEvent event) {
Icarus.onServerStart(event.getServer());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.quiltmc.loader.api.ModContainer;
import org.quiltmc.qsl.base.api.entrypoint.ModInitializer;
import org.quiltmc.qsl.entity.event.api.ServerEntityTickCallback;
import org.quiltmc.qsl.lifecycle.api.event.ServerLifecycleEvents;
import org.quiltmc.qsl.networking.api.ServerPlayConnectionEvents;

import java.util.function.Supplier;
Expand Down Expand Up @@ -107,6 +108,8 @@ public void onInitialize(ModContainer mod) {
}
});

ServerLifecycleEvents.STARTING.register(Icarus::onServerStart);

IcarusItems.register(new Registrar<>() {
@Override
public <T extends Item> Supplier<T> register(String name, Supplier<? extends T> factory) {
Expand Down

0 comments on commit 86815d2

Please sign in to comment.