Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Getting ready for initial release (0.1.0)!
Browse files Browse the repository at this point in the history
  • Loading branch information
cocona20xx committed Oct 19, 2022
1 parent 8a1cc78 commit c539afa
Show file tree
Hide file tree
Showing 20 changed files with 252 additions and 401 deletions.
49 changes: 21 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,27 @@
# Quilt Template Mod w/ `gradle.properties` Dependency Declaration
## Glowstick: Emissive Textures via JSON, for Quilt Loader + Quilted Fabric API (1.19.2+)
Glowstick is a mod that adds a simplistic emissive/'glowing' textures feature to vanilla's generated item textures (nearly all items that use a set of texture layers).

A template mod for the Quilt Toolchain, based on the official template.
Currently (as of 0.1.0), *only item models are supported*, and armor overlay support is planned for the near future.

You can use this as a template for your own mods!
Tested to be compatible with [Chime](https://github.com/emilyploszaj/chime)'s item predicate extensions.

([And, you can find the official template here.](https://github.com/QuiltMC/quilt-template-mod))
### How to use Glowstick:

## Usage
In the main json object for an item model, add an array object with the name `"emissive_texures"`, and put the *texture ids* (not the layer names themselves!) for which you want the emissive effect to apply to within.

In order to use this mod as a template:
Example: (at `assets/minecraft/models/item/stick.json`)
```
{
"parent": "minecraft:item/handheld",
"textures": {
"layer0": "testpack:item/glowstick",
"layer1": "testpack:item/glowstick2"
},
"emissive_textures": [
"testpack:item/glowstick2"
]
}
```
### License:
Glowstick is licensed under the *Mozilla Public License, Version 2.0* (see the LICENSE file and the licensing header!)

1. Create a new repository from this template with `Use this template`
2. Clone the recently-created repo on your PC
3. Make the necessary changes in order to make it yours:
- Update `gradle.properties` as is necessary:
- Since this template declares dependency versions in `gradle.properties`, refer to the instructions in the comments on said file to update them.
- Also, update `gradle.properties` in order to use your Maven group and mod ID:
- If you don't know which Maven group to use, and you are planning to host the mod's source code on GitHub, use `io.github.<Your_Username_Here>`
- Update `quilt.mod.json` in order to reflect your mod's metadata:
- If you are planning to include (jar-in-jar) a mod, don't forget to declare its dependency on it!
- The icon provided here is a placeholder one. If you aren't able to replace it yet, you can delete it and remove the "icon" property
- Create a LICENSE file for this mod! If you don't know which license to use, check out [here](https://choosealicense.com/):
- If you use `LICENSE.md`, don't forget to update the buildscript in order to use that file name!
- In `quilt.mod.json`, don't forget to put the license's [SPDX identifier](https://spdx.org/licenses/) under the `"license"` property in `"metadata"`.
- The GPLv3 and AGPLv3 are not valid mod licenses, so you can use almost any license except for those.
- Update the Java sub-directory structure, so that it reflects your Maven group
4. The mod is now ready to be worked on!

## License

This template is licensed under the [Creative Common Zero v1.0 license](./LICENSE-TEMPLATE.md).

Mods created with this template are not automatically licensed under the CC0, and are not required to give any kind of credit back to QuiltMC (nor CoconaOfTheStars) for this template.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ quilted_fabric_api_version = 4.0.0-beta.13+0.62.0
# Mod Properties
version = 0.1.0+1.19.2
maven_group = io.github.cocona20xx
archives_base_name = retribution
archives_base_name = glowstick
17 changes: 17 additions & 0 deletions src/main/java/cocona20xx/glowstick/BakedQuadAccessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package cocona20xx.glowstick;

import net.minecraft.client.render.model.BakedQuad;

public interface BakedQuadAccessor {
int getModifier();
void setModifier(int newMod);

void storeActual(BakedQuad toStore);

BakedQuad getActual();
}
123 changes: 123 additions & 0 deletions src/main/java/cocona20xx/glowstick/EmissiveDataReloader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package cocona20xx.glowstick;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Lists;
import com.google.gson.*;
import net.minecraft.resource.Resource;
import net.minecraft.resource.ResourceManager;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.NotNull;
import org.quiltmc.qsl.resource.loader.api.reloader.SimpleSynchronousResourceReloader;

import java.io.BufferedReader;
import java.util.*;
import java.util.regex.Pattern;

public class EmissiveDataReloader implements SimpleSynchronousResourceReloader {
private static final ArrayListMultimap<Identifier, Identifier> EMISSIVE_MAP = ArrayListMultimap.create();
private static final Gson GSON = new Gson();
private static final String[] PARENT_BLACKLIST = {"template_banner", "template_bed", "template_shulker_box", "template_skull", "template_spawn_egg", "trident_in_hand", "spyglass_in_hand", "block"};
private static final ArrayList<String> BLACKLIST_LIST = Lists.newArrayList(PARENT_BLACKLIST);
public static final Identifier RELOADER_ID = new Identifier(GlowstickModClient.MOD_ID, "emissive");
public static final String ET_MEMBER_NAME = "emissive_textures";
private static final String[] flags = {"layer0", "layer1", "layer2", "layer3", "layer4"};

@Override
public void reload(ResourceManager manager) {
GlowstickModClient.LOGGER.info("Starting Emissive Load...");
EMISSIVE_MAP.clear();
for(Identifier modelId : manager.findResources("models/item", path -> path.toString().endsWith(".json")).keySet()){
try {
for(Resource r : manager.getAllResources(modelId)){
try (BufferedReader reader = r.openBufferedReader()){
JsonObject mainObject = GSON.fromJson(reader, JsonObject.class);
if(mainObject.has(ET_MEMBER_NAME)){
if(!BLACKLIST_LIST.contains(mainObject.get("parent").getAsString())){
JsonArray etArrayJson = mainObject.getAsJsonArray(ET_MEMBER_NAME);
int etLen = etArrayJson.size();
for (int i = 0; i < etLen; i++) {
JsonPrimitive current = etArrayJson.get(i).getAsJsonPrimitive();
if(current.isString()){
//emissive texture strings are not checked against the model as overrides can refer to other models, and this (should) hopefully let those textures also work? needs testing ofc (TODO)
Identifier sanitizedModelId = sanitizeValidId(modelId);
Identifier sanitizedTextureId = convertIdStringToSanitized(current.getAsString());
putResult(sanitizedTextureId, sanitizedModelId);
} else {
throw new Exception("Emissive Textures array on item model " + modelId.toString() + " contains non-string value at index " + i +",skipping.");
}
}
}
}
} catch (Exception e){
GlowstickModClient.LOGGER.warn("Error occurred when trying to parse emissive data: " + e);
}
}
} catch (Exception e){
GlowstickModClient.LOGGER.warn("IO Error when trying to parse emissive data: " + e);
}
}
}

@Override
public @NotNull Identifier getQuiltId() {
return RELOADER_ID;
}

private static void putResult(Identifier sanitizedTexId, Identifier sanitizedModelId){
EMISSIVE_MAP.put(sanitizedTexId, sanitizedModelId);
}

public static boolean checkValidity(Identifier sanitizedTexId, Identifier sanitizedModelId){
//you are valid! the model id, however, might not be
boolean validity = false;
List<Identifier> models = EMISSIVE_MAP.get(sanitizedTexId);
for (Identifier model : models) {
if (model.equals(sanitizedModelId)) {
validity = true;
break;
}
}
return validity;
}

public static Identifier sanitizeValidId(Identifier toSanitize){
String path = toSanitize.getPath();
if(path.contains("#")){
path = path.split(Pattern.quote("#"))[0];
}
path = removeLastIfValid(path, ".json");
path = removeLastIfValid(path, ".png");
path = removeFirstIfValid(path, "models/");
path = removeFirstIfValid(path, "textures/");
path = removeFirstIfValid(path, "item");
return new Identifier(toSanitize.getNamespace(), path);
}

public static Identifier convertIdStringToSanitized(String idString){
if(idString.contains(":")){
//id has defined namespace
String[] s = idString.split(Pattern.quote(":"));
return sanitizeValidId(new Identifier(s[0], s[1]));
} else {
//implicit minecraft namespace
return sanitizeValidId(new Identifier("minecraft", idString));
}
}

private static String removeLastIfValid(String current, String last){
if(current.endsWith(last)){
int l = current.lastIndexOf(last);
return current.substring(0, l);
} else return current;
}
private static String removeFirstIfValid(String current, String first){
if(current.startsWith(first)){
return current.replaceFirst(Pattern.quote(first), "");
} else return current;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package cocona20xx.retribution;
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package cocona20xx.glowstick;

import cocona20xx.retribution.impl.EmissiveDataReloader;
import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
import net.minecraft.client.MinecraftClient;
import net.minecraft.resource.ResourceType;
import org.quiltmc.loader.api.ModContainer;
import org.quiltmc.qsl.base.api.entrypoint.client.ClientModInitializer;
Expand All @@ -11,13 +13,19 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RetributionModClient implements ClientModInitializer {
public static final String MOD_ID = "retribution";
public class GlowstickModClient implements ClientModInitializer {
public static final String MOD_ID = "glowstick";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);

public static final int EMISSIVE_MODIFIER = 200;
@Override
public void onInitializeClient(ModContainer mod) {
LOGGER.info("Retribution is loading...");
LOGGER.info("Glowstick is loading...");
ResourceLoader.get(ResourceType.CLIENT_RESOURCES).registerReloader(new EmissiveDataReloader());
ResourceLoader.get(ResourceType.CLIENT_RESOURCES).addReloaderOrdering(EmissiveDataReloader.RELOADER_ID, ResourceReloaderKeys.BEFORE_VANILLA);
}

public static short shortMin(short a, short b){
return (a <= b) ? a : b;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package cocona20xx.retribution.mixins;
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package cocona20xx.glowstick.mixins;

import cocona20xx.retribution.internal.BakedQuadLightingModifierAccessor;
import cocona20xx.glowstick.BakedQuadAccessor;
import net.minecraft.client.render.model.BakedQuad;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;

@Mixin(BakedQuad.class)
public class BakedQuadMixin implements BakedQuadLightingModifierAccessor {
public class BakedQuadMixin implements BakedQuadAccessor {
@Unique private int modifierValue = 0;
@Unique private BakedQuad actual;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package cocona20xx.retribution.mixins;
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package cocona20xx.glowstick.mixins;

import cocona20xx.retribution.RetributionModClient;
import cocona20xx.retribution.impl.EmissiveDataReloader;
import cocona20xx.retribution.internal.BakedQuadLightingModifierAccessor;
import net.minecraft.client.MinecraftClient;
import cocona20xx.glowstick.GlowstickModClient;
import cocona20xx.glowstick.BakedQuadAccessor;
import net.minecraft.client.render.item.ItemRenderer;
import net.minecraft.client.render.model.BakedQuad;
import net.minecraft.client.util.math.MatrixStack;
Expand All @@ -15,14 +18,19 @@
public class ItemRenderMixin {
@ModifyArg(method = "renderBakedItemQuads", at = @At(value = "INVOKE", target = "Lcom/mojang/blaze3d/vertex/VertexConsumer;bakedQuad(Lnet/minecraft/client/util/math/MatrixStack$Entry;Lnet/minecraft/client/render/model/BakedQuad;FFFII)V"), index = 5)
public int modifyLighting(MatrixStack.Entry matrixEntry, BakedQuad quad, float red, float green, float blue, int light, int overlay){
BakedQuadLightingModifierAccessor wrap = (BakedQuadLightingModifierAccessor)quad;
String sanitySimpleId = EmissiveDataReloader.simplifyFromId(quad.getSprite().getId());
boolean sanity = EmissiveDataReloader.hasDataForSimple(sanitySimpleId);
if((wrap.getModifier() != 0 && light < 240) && sanity) {
int mod = light + wrap.getModifier();
return Math.min(mod, 240);
BakedQuadAccessor wrap = (BakedQuadAccessor)quad;
short sky = (short)(light >> 16);
short block = (short)light;
block = (short) (block << 4);
if((wrap.getModifier() != 0 && block < 240)) {
short mod = (short) (block + wrap.getModifier());
mod = GlowstickModClient.shortMin(mod, (short) 240);
//short finalMod = (short) (mod >> 4);
int combined = (sky << 16) | (mod & 0xFFFF);
return combined;
}
else return light;
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package cocona20xx.glowstick.mixins;

import cocona20xx.glowstick.GlowstickModClient;
import cocona20xx.glowstick.EmissiveDataReloader;
import cocona20xx.glowstick.BakedQuadAccessor;
import net.minecraft.client.render.model.BakedQuad;
import net.minecraft.client.render.model.ModelBakeSettings;
import net.minecraft.client.render.model.json.JsonUnbakedModel;
import net.minecraft.client.render.model.json.ModelElement;
import net.minecraft.client.render.model.json.ModelElementFace;
import net.minecraft.client.texture.Sprite;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.Direction;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(JsonUnbakedModel.class)
public class JsonUnbakedModelMixin{

@Inject(method = "createQuad", at = @At("RETURN"), cancellable = true)
private static void createQuadInjector(ModelElement element, ModelElementFace elementFace, Sprite sprite, Direction side, ModelBakeSettings settings, Identifier id, CallbackInfoReturnable<BakedQuad> cir){
BakedQuad initReturn = cir.getReturnValue();
if(sprite.getId().toString().contains("test")){
boolean useless = true;
}
if(EmissiveDataReloader.checkValidity(EmissiveDataReloader.sanitizeValidId(sprite.getId()), EmissiveDataReloader.sanitizeValidId(id))){
BakedQuadAccessor bakedQuadAccessor = (BakedQuadAccessor)initReturn;
bakedQuadAccessor.storeActual(initReturn);
bakedQuadAccessor.setModifier(GlowstickModClient.EMISSIVE_MODIFIER);
cir.setReturnValue(bakedQuadAccessor.getActual());
}
}
}
Loading

0 comments on commit c539afa

Please sign in to comment.