From b1f34389b73621262113a4fab73bd5d0bd9ae5c4 Mon Sep 17 00:00:00 2001 From: PepperBell <44146161+PepperCode1@users.noreply.github.com> Date: Wed, 24 Nov 2021 18:43:05 -0800 Subject: [PATCH] Improve StitchedSprite functionality - Make StitchedSprite work like PartialModel - Allow StitchedSprite instances to be created directly - Allow StitchSprites to be created for any atlas - Use texture stitch post event instead of lazy computation for filling StitchedSprites - Remove legacy method from AngleHelper - Change artifact name from flywheel to flywheel-forge - Organize imports --- build.gradle | 2 +- gradle.properties | 2 +- .../com/jozufozu/flywheel/FlywheelClient.java | 6 +- .../jozufozu/flywheel/core/AtlasStitcher.java | 40 ----------- .../flywheel/core/StitchedSprite.java | 66 ++++++++++++++----- .../core/materials/oriented/OrientedData.java | 2 - .../jozufozu/flywheel/event/ForgeEvents.java | 2 +- .../jozufozu/flywheel/util/AngleHelper.java | 12 ---- .../flywheel/vanilla/ChestInstance.java | 1 - 9 files changed, 55 insertions(+), 78 deletions(-) delete mode 100644 src/main/java/com/jozufozu/flywheel/core/AtlasStitcher.java diff --git a/build.gradle b/build.gradle index 0acdddfce..5ae2aba04 100644 --- a/build.gradle +++ b/build.gradle @@ -29,7 +29,7 @@ project.buildnumber = System.getenv('BUILD_NUMBER') != null ? System.getenv('BUI version = "${mc_update_version}-${mod_version}" + (dev ? ".${buildnumber}" : '') group = 'com.jozufozu.flywheel' -archivesBaseName = 'flywheel' +archivesBaseName = 'flywheel-forge' java.toolchain.languageVersion = JavaLanguageVersion.of(16) diff --git a/gradle.properties b/gradle.properties index 8271bf4d2..89a19eba4 100644 --- a/gradle.properties +++ b/gradle.properties @@ -5,7 +5,7 @@ org.gradle.daemon = false mod_version = 0.3.0 mc_update_version = 1.17 minecraft_version = 1.17.1 -forge_version = 37.0.108 +forge_version = 37.0.117 # build dependency versions forgegradle_version = 5.1.+ diff --git a/src/main/java/com/jozufozu/flywheel/FlywheelClient.java b/src/main/java/com/jozufozu/flywheel/FlywheelClient.java index 9cf2e6f79..a8b7b19ae 100644 --- a/src/main/java/com/jozufozu/flywheel/FlywheelClient.java +++ b/src/main/java/com/jozufozu/flywheel/FlywheelClient.java @@ -1,10 +1,10 @@ package com.jozufozu.flywheel; import com.jozufozu.flywheel.backend.Backend; -import com.jozufozu.flywheel.core.AtlasStitcher; import com.jozufozu.flywheel.core.Contexts; import com.jozufozu.flywheel.core.Materials; import com.jozufozu.flywheel.core.PartialModel; +import com.jozufozu.flywheel.core.StitchedSprite; import com.jozufozu.flywheel.vanilla.VanillaInstances; import net.minecraftforge.eventbus.api.IEventBus; @@ -18,12 +18,12 @@ public class FlywheelClient { IEventBus modEventBus = FMLJavaModLoadingContext.get() .getModEventBus(); - modEventBus.addListener(AtlasStitcher.getInstance()::onTextureStitch); - modEventBus.addListener(Contexts::flwInit); modEventBus.addListener(Materials::flwInit); modEventBus.addListener(PartialModel::onModelRegistry); modEventBus.addListener(PartialModel::onModelBake); + modEventBus.addListener(StitchedSprite::onTextureStitchPre); + modEventBus.addListener(StitchedSprite::onTextureStitchPost); VanillaInstances.init(); } diff --git a/src/main/java/com/jozufozu/flywheel/core/AtlasStitcher.java b/src/main/java/com/jozufozu/flywheel/core/AtlasStitcher.java deleted file mode 100644 index 3e4ac15fa..000000000 --- a/src/main/java/com/jozufozu/flywheel/core/AtlasStitcher.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.jozufozu.flywheel.core; - -import java.util.ArrayList; -import java.util.List; - -import net.minecraft.resources.ResourceLocation; -import net.minecraft.world.inventory.InventoryMenu; -import net.minecraftforge.client.event.TextureStitchEvent; - -/** - * This is primarily for hacking entity textures into the block atlas. - */ -public class AtlasStitcher { - protected static final AtlasStitcher INSTANCE = new AtlasStitcher(); - - public static AtlasStitcher getInstance() { - return INSTANCE; - } - - private final List sprites = new ArrayList<>(); - - public StitchedSprite get(ResourceLocation loc) { - StitchedSprite sprite = new StitchedSprite(loc); - - sprites.add(sprite); - - return sprite; - } - - public void onTextureStitch(TextureStitchEvent.Pre event) { - if (!event.getMap() - .location() - .equals(InventoryMenu.BLOCK_ATLAS)) return; - - sprites.forEach(StitchedSprite::reset); - sprites.stream() - .map(StitchedSprite::getLoc) - .forEach(event::addSprite); - } -} diff --git a/src/main/java/com/jozufozu/flywheel/core/StitchedSprite.java b/src/main/java/com/jozufozu/flywheel/core/StitchedSprite.java index c43f045a6..6f6157799 100644 --- a/src/main/java/com/jozufozu/flywheel/core/StitchedSprite.java +++ b/src/main/java/com/jozufozu/flywheel/core/StitchedSprite.java @@ -1,35 +1,67 @@ package com.jozufozu.flywheel.core; -import net.minecraft.client.Minecraft; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import net.minecraft.client.renderer.texture.TextureAtlas; import net.minecraft.client.renderer.texture.TextureAtlasSprite; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.inventory.InventoryMenu; +import net.minecraftforge.client.event.TextureStitchEvent; public class StitchedSprite { + private static final Map> ALL = new HashMap<>(); - private final ResourceLocation loc; + protected final ResourceLocation atlasLocation; + protected final ResourceLocation location; + protected TextureAtlasSprite sprite; - TextureAtlasSprite sprite; - - StitchedSprite(ResourceLocation loc) { - this.loc = loc; + public StitchedSprite(ResourceLocation atlas, ResourceLocation location) { + atlasLocation = atlas; + this.location = location; + ALL.computeIfAbsent(atlasLocation, $ -> new ArrayList<>()).add(this); } - public ResourceLocation getLoc() { - return loc; + public StitchedSprite(ResourceLocation location) { + this(InventoryMenu.BLOCK_ATLAS, location); } - public TextureAtlasSprite getSprite() { - if (sprite == null) { - sprite = Minecraft.getInstance() - .getTextureAtlas(InventoryMenu.BLOCK_ATLAS) - .apply(loc); + public static void onTextureStitchPre(TextureStitchEvent.Pre event) { + ResourceLocation atlasLocation = event.getMap().location(); + List sprites = ALL.get(atlasLocation); + if (sprites != null) { + for (StitchedSprite sprite : sprites) { + event.addSprite(sprite.getLocation()); + } } + } + public static void onTextureStitchPost(TextureStitchEvent.Post event) { + TextureAtlas atlas = event.getMap(); + ResourceLocation atlasLocation = atlas.location(); + List sprites = ALL.get(atlasLocation); + if (sprites != null) { + for (StitchedSprite sprite : sprites) { + sprite.loadSprite(atlas); + } + } + } + + protected void loadSprite(TextureAtlas atlas) { + sprite = atlas.getSprite(location); + } + + public ResourceLocation getAtlasLocation() { + return atlasLocation; + } + + public ResourceLocation getLocation() { + return location; + } + + public TextureAtlasSprite get() { return sprite; } - - void reset() { - sprite = null; - } } diff --git a/src/main/java/com/jozufozu/flywheel/core/materials/oriented/OrientedData.java b/src/main/java/com/jozufozu/flywheel/core/materials/oriented/OrientedData.java index 15a45882a..850b426a3 100644 --- a/src/main/java/com/jozufozu/flywheel/core/materials/oriented/OrientedData.java +++ b/src/main/java/com/jozufozu/flywheel/core/materials/oriented/OrientedData.java @@ -2,8 +2,6 @@ package com.jozufozu.flywheel.core.materials.oriented; import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer; import com.jozufozu.flywheel.core.materials.BasicData; -import com.jozufozu.flywheel.util.transform.Rotate; -import com.jozufozu.flywheel.util.transform.Translate; import com.jozufozu.flywheel.util.vec.Vec3; import com.mojang.math.Quaternion; import com.mojang.math.Vector3f; diff --git a/src/main/java/com/jozufozu/flywheel/event/ForgeEvents.java b/src/main/java/com/jozufozu/flywheel/event/ForgeEvents.java index 5ca34fa40..72900ab01 100644 --- a/src/main/java/com/jozufozu/flywheel/event/ForgeEvents.java +++ b/src/main/java/com/jozufozu/flywheel/event/ForgeEvents.java @@ -4,9 +4,9 @@ import java.util.ArrayList; import com.jozufozu.flywheel.backend.Backend; import com.jozufozu.flywheel.backend.instancing.InstancedRenderDispatcher; -import com.jozufozu.flywheel.util.WorldAttached; import com.jozufozu.flywheel.light.LightUpdater; import com.jozufozu.flywheel.util.ChunkIter; +import com.jozufozu.flywheel.util.WorldAttached; import net.minecraft.client.Minecraft; import net.minecraft.client.multiplayer.ClientLevel; diff --git a/src/main/java/com/jozufozu/flywheel/util/AngleHelper.java b/src/main/java/com/jozufozu/flywheel/util/AngleHelper.java index 7dbddb193..a89f8bf87 100644 --- a/src/main/java/com/jozufozu/flywheel/util/AngleHelper.java +++ b/src/main/java/com/jozufozu/flywheel/util/AngleHelper.java @@ -5,19 +5,7 @@ import net.minecraft.core.Direction.Axis; public class AngleHelper { - /** - * Legacy method. See {@link #horizontalAngleNew(Direction)} for new method. - */ public static float horizontalAngle(Direction facing) { - float angle = facing.toYRot(); - if (facing.getAxis() == Axis.X) angle = -angle; - return angle; - } - - /** - * Same as {@link #horizontalAngle(Direction)}, but returns 0 instead of -90 for vertical directions. - */ - public static float horizontalAngleNew(Direction facing) { if (facing.getAxis() .isVertical()) { return 0; diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/ChestInstance.java b/src/main/java/com/jozufozu/flywheel/vanilla/ChestInstance.java index b93961253..a11867494 100644 --- a/src/main/java/com/jozufozu/flywheel/vanilla/ChestInstance.java +++ b/src/main/java/com/jozufozu/flywheel/vanilla/ChestInstance.java @@ -13,7 +13,6 @@ import com.jozufozu.flywheel.core.materials.model.ModelData; import com.jozufozu.flywheel.core.materials.oriented.OrientedData; import com.jozufozu.flywheel.core.model.ModelPart; import com.jozufozu.flywheel.util.AnimationTickHolder; -import com.jozufozu.flywheel.util.transform.MatrixTransformStack; import com.mojang.math.Quaternion; import com.mojang.math.Vector3f;