all the Common kotlin stuff is now compiling apparently

This commit is contained in:
Talia-12 2023-06-20 22:04:49 +10:00
parent 7d611c0220
commit a97545246f
9 changed files with 129 additions and 79 deletions

View file

@ -2,21 +2,23 @@ package at.petrak.hexcasting.api.utils
import org.joml.Quaternionf
import org.joml.Vector3f
import kotlin.math.cos
import kotlin.math.sin
import kotlin.math.*
val Vector3fXP
get() = Vector3f(1f, 0f, 0f)
val Vector3fXM
get() = Vector3f(-1f, 0f, 0f)
val Vector3fYP
get() = Vector3f(0f, 1f, 0f)
val Vector3fYM
get() = Vector3f(0f, -1f, 0f)
val Vector3fZP
get() = Vector3f(0f, 0f, 1f)
val Vector3fZM
get() = Vector3f(0f, 0f, -1f)
object Vector3fUtils {
val XP
get() = Vector3f(1f, 0f, 0f)
val XM
get() = Vector3f(-1f, 0f, 0f)
val YP
get() = Vector3f(0f, 1f, 0f)
val YM
get() = Vector3f(0f, -1f, 0f)
val ZP
get() = Vector3f(0f, 0f, 1f)
val ZM
get() = Vector3f(0f, 0f, -1f)
}
fun Vector3f.rotationDegrees(degrees: Float): Quaternionf {
val rads = degrees * 0.017453292f
@ -28,4 +30,46 @@ fun Vector3f.rotationDegrees(degrees: Float): Quaternionf {
val r = cos(rads / 2.0f)
return Quaternionf(i, j, k, r)
}
object QuaternionfUtils {
@JvmStatic
val ONE: Quaternionf
get() = Quaternionf(0.0f, 0.0f, 0.0f, 1.0f)
@JvmStatic
fun fromXYZDegrees(vector3f: Vector3f): Quaternionf {
return fromXYZ(Math.toRadians(vector3f.x().toDouble()).toFloat(), Math.toRadians(vector3f.y().toDouble()).toFloat(), Math.toRadians(vector3f.z().toDouble()).toFloat())
}
@JvmStatic
fun fromXYZ(vector3f: Vector3f): Quaternionf {
return fromXYZ(vector3f.x(), vector3f.y(), vector3f.z())
}
@JvmStatic
fun fromXYZ(f: Float, g: Float, h: Float): Quaternionf {
val quaternion = ONE
quaternion.mul(Quaternionf(sin((f / 2.0f)), 0.0f, 0.0f, cos((f / 2.0f))))
quaternion.mul(Quaternionf(0.0f, sin((g / 2.0f)), 0.0f, cos((g / 2.0f))))
quaternion.mul(Quaternionf(0.0f, 0.0f, sin((h / 2.0f)), cos((h / 2.0f))))
return quaternion
}
}
fun Quaternionf.toXYZ(): Vector3f {
val f: Float = this.w() * this.w()
val g: Float = this.x() * this.x()
val h: Float = this.y() * this.y()
val i: Float = this.z() * this.z()
val j = f + g + h + i
val k: Float = 2.0f * this.w() * this.x() - 2.0f * this.y() * this.z()
val l = asin(k / j)
return if (abs(k) > 0.999f * j)
Vector3f(2.0f * atan2(this.x(), this.w()), l, 0.0f)
else Vector3f(
atan2(2.0f * this.y() * this.z() + 2.0f * this.x() * this.w(), f - g - h + i),
l,
atan2(2.0f * this.x() * this.y() + 2.0f * this.w() * this.z(), f + g - h - i))
}

View file

@ -2,6 +2,7 @@ package at.petrak.hexcasting.client.render;
import at.petrak.hexcasting.api.client.ScryingLensOverlayRegistry;
import at.petrak.hexcasting.api.player.Sentinel;
import at.petrak.hexcasting.api.utils.QuaternionfUtils;
import at.petrak.hexcasting.client.ClientTickCounter;
import at.petrak.hexcasting.common.lib.HexAttributes;
import at.petrak.hexcasting.xplat.IXplatAbstractions;
@ -13,9 +14,8 @@ import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.Tesselator;
import com.mojang.blaze3d.vertex.VertexFormat;
import com.mojang.datafixers.util.Pair;
import com.mojang.math.Quaternion;
import com.mojang.math.Vector3f;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.client.renderer.GameRenderer;
@ -27,6 +27,8 @@ import net.minecraft.world.item.ItemStack;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.phys.Vec3;
import org.joml.Quaternionf;
import org.joml.Vector3f;
import java.util.List;
import java.util.function.BiConsumer;
@ -36,14 +38,14 @@ public class HexAdditionalRenderers {
var player = Minecraft.getInstance().player;
if (player != null) {
var sentinel = IXplatAbstractions.INSTANCE.getSentinel(player);
if (sentinel != null && player.getLevel().dimension().equals(sentinel.dimension())) {
if (sentinel != null && player.level().dimension().equals(sentinel.dimension())) {
renderSentinel(sentinel, player, ps, partialTick);
}
}
}
public static void overlayGui(PoseStack ps, float partialTicks) {
tryRenderScryingLensOverlay(ps, partialTicks);
public static void overlayGui(GuiGraphics graphics, float partialTicks) {
tryRenderScryingLensOverlay(graphics, partialTicks);
}
private static void renderSentinel(Sentinel sentinel, LocalPlayer owner,
@ -64,9 +66,9 @@ public class HexAdditionalRenderers {
var magnitude = 0.1f;
ps.translate(0, Mth.sin(bobSpeed * time) * magnitude, 0);
var spinSpeed = 1f / 30;
ps.mulPose(Quaternion.fromXYZ(new Vector3f(0, spinSpeed * time, 0)));
ps.mulPose(QuaternionfUtils.fromXYZ(new Vector3f(0, spinSpeed * time, 0)));
if (sentinel.extendsRange()) {
ps.mulPose(Quaternion.fromXYZ(new Vector3f(spinSpeed * time / 8f, 0, 0)));
ps.mulPose(QuaternionfUtils.fromXYZ(new Vector3f(spinSpeed * time / 8f, 0, 0)));
}
float scale = 0.5f;
@ -148,8 +150,9 @@ public class HexAdditionalRenderers {
}
}
private static void tryRenderScryingLensOverlay(PoseStack ps, float partialTicks) {
private static void tryRenderScryingLensOverlay(GuiGraphics graphics, float partialTicks) {
var mc = Minecraft.getInstance();
var ps = graphics.pose();
LocalPlayer player = mc.player;
ClientLevel level = mc.level;
@ -196,16 +199,16 @@ public class HexAdditionalRenderers {
var stack = pair.getFirst();
if (!stack.isEmpty()) {
// this draws centered in the Y ...
RenderLib.renderItemStackInGui(ps, pair.getFirst(), 0, 0);
graphics.renderItem(pair.getFirst(), 0, 0);
}
float tx = stack.isEmpty() ? 0 : 18;
float ty = 5;
int tx = stack.isEmpty() ? 0 : 18;
int ty = 5;
// but this draws where y=0 is the baseline
var text = pair.getSecond();
for (var line : text) {
var actualLine = Language.getInstance().getVisualOrder(line);
mc.font.drawShadow(ps, actualLine, tx, ty, 0xffffffff);
graphics.drawString(mc.font, actualLine, tx, ty, 0xffffffff);
ps.translate(0, mc.font.lineHeight, 0);
}
if (text.isEmpty()) {

View file

@ -4,8 +4,7 @@ package at.petrak.hexcasting.client.render
import at.petrak.hexcasting.api.casting.math.HexPattern
import at.petrak.hexcasting.api.mod.HexConfig
import at.petrak.hexcasting.api.utils.TAU
import at.petrak.hexcasting.api.utils.Vector3fYP
import at.petrak.hexcasting.api.utils.*
import at.petrak.hexcasting.client.ClientTickCounter
import com.mojang.blaze3d.systems.RenderSystem
import com.mojang.blaze3d.vertex.DefaultVertexFormat
@ -30,8 +29,6 @@ import kotlin.math.abs
import kotlin.math.min
import kotlin.math.roundToInt
import kotlin.math.sin
import at.petrak.hexcasting.api.utils.Vector3fZP
import at.petrak.hexcasting.api.utils.rotationDegrees
val NOISE: SimplexNoise = SimplexNoise(SingleThreadedRandomSource(9001L))
@ -405,20 +402,6 @@ fun getCenteredPattern(pattern: HexPattern, width: Float, height: Float, minSize
return scale to lines2
}
fun renderItemStackInGui(ms: PoseStack, stack: ItemStack, x: Int, y: Int) {
transferMsToGl(ms) { Minecraft.getInstance().itemRenderer.renderAndDecorateItem(stack, x, y) }
}
fun transferMsToGl(ms: PoseStack, toRun: Runnable) {
val mvs = RenderSystem.getModelViewStack()
mvs.pushPose()
mvs.mulPoseMatrix(ms.last().pose())
RenderSystem.applyModelViewMatrix()
toRun.run()
mvs.popPose()
RenderSystem.applyModelViewMatrix()
}
@JvmOverloads
fun renderEntity(
ms: PoseStack, entity: Entity, world: Level, x: Float, y: Float, rotation: Float,
@ -428,13 +411,13 @@ fun renderEntity(
val rotation = if (Screen.hasShiftDown()) 0.0f else rotation
// TODO: Figure out why this is here and whether removing it will break things
entity.level = world
// entity.level = world
ms.pushPose()
ms.translate(x.toDouble(), y.toDouble(), 50.0)
ms.scale(renderScale, renderScale, renderScale)
ms.translate(0.0, offset.toDouble(), 0.0)
ms.mulPose(Vector3fZP.rotationDegrees(180.0f))
ms.mulPose(Vector3fYP.rotationDegrees(rotation))
ms.mulPose(Vector3fUtils.ZP.rotationDegrees(180.0f))
ms.mulPose(Vector3fUtils.YP.rotationDegrees(rotation))
val erd = Minecraft.getInstance().entityRenderDispatcher
val immediate = Minecraft.getInstance().renderBuffers().bufferSource()
erd.setRenderShadow(false)

View file

@ -4,6 +4,7 @@ import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.datafixers.util.Pair;
import net.minecraft.client.renderer.ShaderInstance;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.server.packs.resources.ResourceProvider;
import java.io.IOException;
import java.util.function.Consumer;
@ -12,10 +13,10 @@ import java.util.function.Consumer;
public class HexShaders {
private static ShaderInstance grayscale;
public static void init(ResourceManager resourceManager,
Consumer<Pair<ShaderInstance, Consumer<ShaderInstance>>> registrations) throws IOException {
public static void init(ResourceProvider resourceProvider,
Consumer<Pair<ShaderInstance, Consumer<ShaderInstance>>> registrations) throws IOException {
registrations.accept(Pair.of(
new ShaderInstance(resourceManager, "hexcasting__grayscale", DefaultVertexFormat.NEW_ENTITY),
new ShaderInstance(resourceProvider, "hexcasting__grayscale", DefaultVertexFormat.NEW_ENTITY),
inst -> grayscale = inst)
);
}

View file

@ -6,22 +6,20 @@ import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import net.minecraft.core.BlockSource;
import net.minecraft.core.dispenser.OptionalDispenseItemBehavior;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.item.ArmorItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Wearable;
//import net.minecraft.world.item.Wearable;
import net.minecraft.world.level.block.DispenserBlock;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.UUID;
public class ItemLens extends Item implements Wearable, HexBaubleItem {
public class ItemLens extends Item implements HexBaubleItem { // Wearable,
// The 0.1 is *additive*
@ -69,10 +67,10 @@ public class ItemLens extends Item implements Wearable, HexBaubleItem {
return EquipmentSlot.HEAD;
}
@Nullable
@Override
public SoundEvent getEquipSound() {
return SoundEvents.AMETHYST_BLOCK_CHIME;
}
// @Nullable
// @Override
// public SoundEvent getEquipSound() {
// return SoundEvents.AMETHYST_BLOCK_CHIME;
// }
}

View file

@ -55,8 +55,7 @@ public class HexItems {
public static final ItemLens SCRYING_LENS = make("lens", new ItemLens(
IXplatAbstractions.INSTANCE.addEquipSlotFabric(EquipmentSlot.HEAD)
.stacksTo(1)
.tab(IXplatAbstractions.INSTANCE.getTab())));
.stacksTo(1)));
public static final ItemAbacus ABACUS = make("abacus", new ItemAbacus(unstackable()));
public static final ItemThoughtKnot THOUGHT_KNOT = make("thought_knot", new ItemThoughtKnot(unstackable()));
@ -115,7 +114,7 @@ public class HexItems {
//
public static Item.Properties props() {
return new Item.Properties().tab(IXplatAbstractions.INSTANCE.getTab());
return new Item.Properties();
}
public static Item.Properties unstackable() {

View file

@ -0,0 +1,20 @@
package at.petrak.hexcasting.api.utils
import org.joml.Vector3f
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test
import kotlin.random.Random
class QuaternionfUtilsTest {
@Test
fun testXYZ() {
val random = Random(System.currentTimeMillis())
repeat(500) {
val expected = Vector3f(random.nextFloat(), random.nextFloat(), random.nextFloat())
val output = QuaternionfUtils.fromXYZ(expected).toXYZ()
assertTrue(expected.distanceSquared(output) < 0.000001)
{ "Expected distance between $expected and $output to be less than 0.000001, was ${expected.distanceSquared(output)}." }
}
}
}

View file

@ -55,7 +55,7 @@ public class ForgeHexClientInitializer {
});
evBus.addListener((RenderGuiEvent.Post e) -> {
HexAdditionalRenderers.overlayGui(e.getPoseStack(), e.getPartialTick());
HexAdditionalRenderers.overlayGui(e.getGuiGraphics(), e.getPartialTick());
});
@ -82,7 +82,7 @@ public class ForgeHexClientInitializer {
@SubscribeEvent
public static void registerShaders(RegisterShadersEvent evt) throws IOException {
HexShaders.init(evt.getResourceManager(), p -> evt.registerShader(p.getFirst(), p.getSecond()));
HexShaders.init(evt.getResourceProvider(), p -> evt.registerShader(p.getFirst(), p.getSecond()));
}
// https://github.com/VazkiiMods/Botania/blob/1.19.x/Forge/src/main/java/vazkii/botania/forge/client/ForgeClientInitializer.java#L225
@ -92,7 +92,7 @@ public class ForgeHexClientInitializer {
@Override
public <T extends ParticleOptions> void register(ParticleType<T> type, Function<SpriteSet,
ParticleProvider<T>> constructor) {
evt.register(type, constructor::apply);
evt.registerSpriteSet(type, constructor::apply);
}
});
}

View file

@ -32,6 +32,8 @@ import at.petrak.hexcasting.forge.recipe.ForgeUnsealedIngredient;
import at.petrak.hexcasting.interop.HexInterop;
import at.petrak.hexcasting.xplat.IXplatAbstractions;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
@ -92,22 +94,22 @@ public class ForgeHexInitializer {
}
private static void initRegistry() {
bind(Registry.SOUND_EVENT_REGISTRY, HexSounds::registerSounds);
bind(Registry.BLOCK_REGISTRY, HexBlocks::registerBlocks);
bind(Registry.ITEM_REGISTRY, HexBlocks::registerBlockItems);
bind(Registry.BLOCK_ENTITY_TYPE_REGISTRY, HexBlockEntities::registerTiles);
bind(Registry.ITEM_REGISTRY, HexItems::registerItems);
bind(Registries.SOUND_EVENT, HexSounds::registerSounds);
bind(Registries.BLOCK, HexBlocks::registerBlocks);
bind(Registries.ITEM, HexBlocks::registerBlockItems);
bind(Registries.BLOCK_ENTITY_TYPE, HexBlockEntities::registerTiles);
bind(Registries.ITEM, HexItems::registerItems);
bind(Registry.RECIPE_SERIALIZER_REGISTRY, HexRecipeStuffRegistry::registerSerializers);
bind(Registry.RECIPE_TYPE_REGISTRY, HexRecipeStuffRegistry::registerTypes);
bind(Registries.RECIPE_SERIALIZER, HexRecipeStuffRegistry::registerSerializers);
bind(Registries.RECIPE_TYPE, HexRecipeStuffRegistry::registerTypes);
bind(Registry.ENTITY_TYPE_REGISTRY, HexEntities::registerEntities);
bind(Registry.ATTRIBUTE_REGISTRY, HexAttributes::register);
bind(Registry.MOB_EFFECT_REGISTRY, HexMobEffects::register);
bind(Registry.POTION_REGISTRY, HexPotions::register);
bind(Registries.ENTITY_TYPE, HexEntities::registerEntities);
bind(Registries.ATTRIBUTE, HexAttributes::register);
bind(Registries.MOB_EFFECT, HexMobEffects::register);
bind(Registries.POTION, HexPotions::register);
HexPotions.addRecipes();
bind(Registry.PARTICLE_TYPE_REGISTRY, HexParticles::registerParticles);
bind(Registries.PARTICLE_TYPE, HexParticles::registerParticles);
bind(IXplatAbstractions.INSTANCE.getIotaTypeRegistry().key(), HexIotaTypes::registerTypes);
bind(IXplatAbstractions.INSTANCE.getActionRegistry().key(), HexActions::register);
@ -158,13 +160,13 @@ public class ForgeHexInitializer {
// We have to do these at some point when the registries are still open
modBus.addListener((RegisterEvent evt) -> {
if (evt.getRegistryKey().equals(Registry.ITEM_REGISTRY)) {
if (evt.getRegistryKey().equals(Registries.ITEM)) {
CraftingHelper.register(ForgeUnsealedIngredient.ID, ForgeUnsealedIngredient.Serializer.INSTANCE);
CraftingHelper.register(ForgeModConditionalIngredient.ID,
ForgeModConditionalIngredient.Serializer.INSTANCE);
HexStatistics.register();
HexLootFunctions.registerSerializers((lift, id) ->
Registry.register(Registry.LOOT_FUNCTION_TYPE, id, lift));
Registry.register(BuiltInRegistries.LOOT_FUNCTION_TYPE, id, lift));
}
});