-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
389a5fe
commit 723a704
Showing
8 changed files
with
229 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,5 @@ pluginManagement { | |
gradlePluginPortal() | ||
} | ||
} | ||
|
||
rootProject.name = "mmodding-library" |
99 changes: 99 additions & 0 deletions
99
...modding/mmodding_lib/library/client/render/model/json/NotCollidingItemModelGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package com.mmodding.mmodding_lib.library.client.render.model.json; | ||
|
||
import com.mojang.datafixers.util.Either; | ||
import net.minecraft.client.render.model.json.ItemModelGenerator; | ||
import net.minecraft.client.render.model.json.JsonUnbakedModel; | ||
import net.minecraft.client.render.model.json.ModelElement; | ||
import net.minecraft.client.texture.Sprite; | ||
import net.minecraft.client.util.SpriteIdentifier; | ||
import net.minecraft.util.Util; | ||
import org.jetbrains.annotations.ApiStatus; | ||
import org.quiltmc.loader.api.minecraft.ClientOnly; | ||
|
||
import java.util.*; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* A no-pixel-collision version of the {@link ItemModelGenerator}. | ||
* <br> | ||
* Builtin Model Identifier is <code>mmodding:builtin/not_colliding_generated</code>. | ||
* <br> | ||
* Item Model Identifier is <code>mmodding:item/not_colliding_generated`</code>. | ||
*/ | ||
@ClientOnly | ||
public class NotCollidingItemModelGenerator extends ItemModelGenerator { | ||
|
||
public static final NotCollidingItemModelGenerator INSTANCE = new NotCollidingItemModelGenerator(); | ||
|
||
public static final JsonUnbakedModel MARKER = Util.make( | ||
JsonUnbakedModel.deserialize("{\"gui_light\": \"front\"}"), | ||
blockModel -> blockModel.id = "not colliding item model generation marker" | ||
); | ||
|
||
@ApiStatus.Internal | ||
public final Map<Integer, List<Frame>> frames = new HashMap<>(); | ||
|
||
@Override | ||
public JsonUnbakedModel create(Function<SpriteIdentifier, Sprite> textureGetter, JsonUnbakedModel blockModel) { | ||
Map<String, Either<SpriteIdentifier, String>> map = new HashMap<>(); | ||
List<ModelElement> list = new ArrayList<>(); | ||
|
||
// Pre-Collects Frames | ||
// We collect the frames of each layer earlier in order to filter the colliding pixels. | ||
// Usage of super#getFrames in super#addSubComponents is then wrapped in a mixin. | ||
for (int i = 0; i < LAYERS.size(); i++) { | ||
String layer = LAYERS.get(i); | ||
SpriteIdentifier spriteIdentifier = blockModel.resolveSprite(layer); | ||
Sprite sprite = textureGetter.apply(spriteIdentifier); | ||
this.frames.put(i, super.getFrames(sprite)); | ||
} | ||
|
||
// Removes Colliding Pixels | ||
// Works by picking layers from the top to the bottom, and for each of them it removes all frames of all layers | ||
// that are lower than the current one and that are at the same position as frames of the current one. | ||
for (int i = LAYERS.size() - 1; i >= 0; i--) { | ||
List<Frame> layer = this.frames.get(i); | ||
for (int j = 1; j < i - 1; j++) { | ||
List<Frame> current = this.frames.get(i - j); | ||
for (Frame frame : layer) { | ||
current.removeIf( | ||
currentFrame -> frame.getMin() == currentFrame.getMin() | ||
&& frame.getMax() == currentFrame.getMax() | ||
&& frame.getLevel() == currentFrame.getLevel() | ||
); | ||
} | ||
this.frames.put(i - j, current); | ||
} | ||
this.frames.put(i, layer); | ||
} | ||
|
||
for (int i = 0; i < LAYERS.size(); i++) { | ||
String layer = LAYERS.get(i); | ||
if (!blockModel.textureExists(layer)) { | ||
break; | ||
} | ||
SpriteIdentifier spriteIdentifier = blockModel.resolveSprite(layer); | ||
map.put(layer, Either.left(spriteIdentifier)); | ||
Sprite sprite = textureGetter.apply(spriteIdentifier); | ||
List<ModelElement> newLayer = this.addLayerElements(i, layer, sprite); | ||
list.addAll(newLayer); | ||
} | ||
|
||
map.put("particle", blockModel.textureExists("particle") ? Either.left(blockModel.resolveSprite("particle")) : map.get("layer0")); | ||
JsonUnbakedModel jsonUnbakedModel = new JsonUnbakedModel( | ||
null, list, map, false, blockModel.getGuiLight(), blockModel.getTransformations(), blockModel.getOverrides() | ||
); | ||
jsonUnbakedModel.id = blockModel.id; | ||
|
||
return jsonUnbakedModel; | ||
} | ||
|
||
// Prevents Frame Expansion | ||
// That is less optimized, I guess, but it allows easier filtering of the frames. | ||
@Override | ||
protected void createOrExpandCurrentCube(List<Frame> cubes, Side side, int x, int y) { | ||
int j = side.isVertical() ? y : x; | ||
int k = side.isVertical() ? x : y; | ||
cubes.add(new Frame(side, k, j)); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/mmodding/mmodding_lib/mixin/injectors/client/ItemModelGeneratorMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.mmodding.mmodding_lib.mixin.injectors.client; | ||
|
||
import com.llamalad7.mixinextras.injector.wrapoperation.Operation; | ||
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; | ||
import com.llamalad7.mixinextras.sugar.Local; | ||
import com.mmodding.mmodding_lib.library.client.render.model.json.NotCollidingItemModelGenerator; | ||
import net.minecraft.client.render.model.json.ItemModelGenerator; | ||
import net.minecraft.client.texture.Sprite; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
|
||
import java.util.List; | ||
|
||
@Mixin(ItemModelGenerator.class) | ||
public class ItemModelGeneratorMixin { | ||
|
||
@WrapOperation(method = "addSubComponents", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/model/json/ItemModelGenerator;getFrames(Lnet/minecraft/client/texture/Sprite;)Ljava/util/List;")) | ||
private List<ItemModelGenerator.Frame> wrapFrames(ItemModelGenerator instance, Sprite sprite, Operation<List<ItemModelGenerator.Frame>> original, @Local(argsOnly = true) int layer) { | ||
ItemModelGenerator object = (ItemModelGenerator) (Object) this; | ||
if (object instanceof NotCollidingItemModelGenerator notCollidingItemModelGenerator) { | ||
return notCollidingItemModelGenerator.frames.get(layer); | ||
} | ||
else { | ||
return original.call(instance, sprite); | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/com/mmodding/mmodding_lib/mixin/injectors/client/JsonUnbakedModelMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.mmodding.mmodding_lib.mixin.injectors.client; | ||
|
||
import com.mmodding.mmodding_lib.library.client.render.model.json.NotCollidingItemModelGenerator; | ||
import com.mojang.datafixers.util.Pair; | ||
import net.minecraft.client.render.model.UnbakedModel; | ||
import net.minecraft.client.render.model.json.ItemModelGenerator; | ||
import net.minecraft.client.render.model.json.JsonUnbakedModel; | ||
import net.minecraft.client.util.SpriteIdentifier; | ||
import net.minecraft.util.Identifier; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; | ||
|
||
import java.util.Collection; | ||
import java.util.Set; | ||
import java.util.function.Function; | ||
|
||
@Mixin(JsonUnbakedModel.class) | ||
public abstract class JsonUnbakedModelMixin { | ||
|
||
@Shadow | ||
public abstract JsonUnbakedModel getRootModel(); | ||
|
||
@Shadow | ||
public abstract SpriteIdentifier resolveSprite(String spriteName); | ||
|
||
@Inject(method = "getTextureDependencies", at = @At("TAIL"), cancellable = true) | ||
private void insertNotCollidingGeneratedModelLogic(Function<Identifier, UnbakedModel> unbakedModelGetter, Set<Pair<String, String>> unresolvedTextureReferences, CallbackInfoReturnable<Collection<SpriteIdentifier>> cir) { | ||
if (this.getRootModel() == NotCollidingItemModelGenerator.MARKER) { | ||
Set<SpriteIdentifier> set2 = (Set<SpriteIdentifier>) cir.getReturnValue(); | ||
ItemModelGenerator.LAYERS.forEach(name -> set2.add(this.resolveSprite(name))); | ||
cir.setReturnValue(set2); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/main/resources/assets/mmodding/models/item/not_colliding_generated.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"parent": "mmodding:builtin/not_colliding_generated", | ||
"gui_light": "front", | ||
"display": { | ||
"ground": { | ||
"rotation": [ 0, 0, 0 ], | ||
"translation": [ 0, 2, 0], | ||
"scale": [ 0.5, 0.5, 0.5 ] | ||
}, | ||
"head": { | ||
"rotation": [ 0, 180, 0 ], | ||
"translation": [ 0, 13, 7], | ||
"scale": [ 1, 1, 1] | ||
}, | ||
"thirdperson_righthand": { | ||
"rotation": [ 0, 0, 0 ], | ||
"translation": [ 0, 3, 1 ], | ||
"scale": [ 0.55, 0.55, 0.55 ] | ||
}, | ||
"firstperson_righthand": { | ||
"rotation": [ 0, -90, 25 ], | ||
"translation": [ 1.13, 3.2, 1.13], | ||
"scale": [ 0.68, 0.68, 0.68 ] | ||
}, | ||
"fixed": { | ||
"rotation": [ 0, 180, 0 ], | ||
"scale": [ 1, 1, 1 ] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters