Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Directory Manager #54

Merged
merged 1 commit into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/turniplabs/halplibe/HalpLibe.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,6 @@ public void onInitialize() {
@Override
public void onPreLaunch() {
// Initializes halp statics first
NetworkHelper.register(PacketModList.class, false, true);
NetworkHelper.register(PacketModList.class, false, true); // Register Halplibe packets first
}
}
9 changes: 5 additions & 4 deletions src/main/java/turniplabs/halplibe/helper/SoundHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.minecraft.client.Minecraft;
import turniplabs.halplibe.HalpLibe;
import turniplabs.halplibe.util.DirectoryManager;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -128,7 +129,7 @@ public static void addCaveMusic(String MOD_ID, String soundSource){
return;
}
String destination = caveMusicDirectory.getPath();
String source = ("/assets/" + MOD_ID + "/cavemusic/" + soundSource).replace("//", "/").trim();
String source = (DirectoryManager.getCaveMusicDirectory(MOD_ID) + soundSource).replace("//", "/").trim();
HalpLibe.LOGGER.info("File source: " + source);
HalpLibe.LOGGER.info("File destination: " + destination);

Expand All @@ -143,7 +144,7 @@ public static void addStreaming(String MOD_ID, String soundSource){
return;
}
String destination = streamingDirectory.getPath();
String source = ("/assets/" + MOD_ID + "/streaming/" + soundSource).replace("//", "/").trim();
String source = (DirectoryManager.getStreamingDirectory(MOD_ID) + soundSource).replace("//", "/").trim();
HalpLibe.LOGGER.info("File source: " + source);
HalpLibe.LOGGER.info("File destination: " + destination);

Expand All @@ -158,7 +159,7 @@ public static void addMusic(String MOD_ID, String soundSource){
return;
}
String destination = musicDirectory.getPath();
String source = ("/assets/" + MOD_ID + "/music/" + soundSource).replace("//", "/").trim();
String source = (DirectoryManager.getMusicDirectory(MOD_ID) + soundSource).replace("//", "/").trim();
HalpLibe.LOGGER.info("File source: " + source);
HalpLibe.LOGGER.info("File destination: " + destination);

Expand All @@ -173,7 +174,7 @@ public static void addSound(String MOD_ID, String soundSource){
return;
}
String destination = soundDirectory + ("/" + MOD_ID + "/").replace("//", "/");
String source = ("/assets/" + MOD_ID + "/sound/" + soundSource).replace("//", "/").trim();
String source = (DirectoryManager.getSoundDirectory(MOD_ID) + soundSource).replace("//", "/").trim();
HalpLibe.LOGGER.info("File source: " + source);
HalpLibe.LOGGER.info("File destination: " + destination);

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/turniplabs/halplibe/helper/TextureHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.minecraft.core.Global;
import net.minecraft.core.block.Block;
import turniplabs.halplibe.util.BlockCoords;
import turniplabs.halplibe.util.DirectoryManager;
import turniplabs.halplibe.util.ItemCoords;
import turniplabs.halplibe.util.TextureHandler;

Expand Down Expand Up @@ -88,10 +89,10 @@ public static int getOrCreateItemTextureIndex(String modId, String itemTexture){
}

public static void addTextureToTerrain(String modId, String blockTexture, int x, int y) {
textureHandlers.add(new TextureHandler("/terrain.png", "/assets/" + modId + "/block/" + blockTexture, Block.texCoordToIndex(x, y), 16, 1));
textureHandlers.add(new TextureHandler("/terrain.png", DirectoryManager.getBlockTextureDirectory(modId) + blockTexture, Block.texCoordToIndex(x, y), 16, 1));
}

public static void addTextureToItems(String modId, String itemTexture, int x, int y) {
textureHandlers.add(new TextureHandler("/gui/items.png", "/assets/" + modId + "/item/" + itemTexture, Block.texCoordToIndex(x, y), 16, 1));
textureHandlers.add(new TextureHandler("/gui/items.png", DirectoryManager.getItemTextureDirectory(modId) + itemTexture, Block.texCoordToIndex(x, y), 16, 1));
}
}
8 changes: 5 additions & 3 deletions src/main/java/turniplabs/halplibe/mixin/mixins/I18nMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import turniplabs.halplibe.HalpLibe;
import turniplabs.halplibe.mixin.accessors.LanguageAccessor;
import turniplabs.halplibe.util.DirectoryManager;

import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -101,9 +102,10 @@ public void addHalplibeModLangFiles(String languageCode, boolean save, CallbackI
String currentLangId = currentLanguage.getId();
HalpLibe.LOGGER.debug("Current lang: " + currentLangId);
for (ModContainer mod : FabricLoader.getInstance().getAllMods()) {
String[] rootLangs = filesInDir("/lang/" + mod.getMetadata().getId() + "/");
String[] subCurrentLangs = filesInDir("/lang/" + mod.getMetadata().getId() + "/" + currentLangId + "/");
String[] subDefaultLangs = filesInDir("/lang/" + mod.getMetadata().getId() + "/" + defaultLangId + "/");
String path = DirectoryManager.getLanguageDirectory(mod.getMetadata().getId());
String[] rootLangs = filesInDir(path);
String[] subCurrentLangs = filesInDir(path + currentLangId + "/");
String[] subDefaultLangs = filesInDir(path + defaultLangId + "/");
HalpLibe.LOGGER.debug(mod.getMetadata().getId() + " contains " + rootLangs.length + subDefaultLangs.length + subCurrentLangs.length + " language files.");
HalpLibe.LOGGER.debug(Arrays.toString(rootLangs));
HalpLibe.LOGGER.debug(Arrays.toString(subCurrentLangs));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.spongepowered.asm.mixin.injection.ModifyArg;
import turniplabs.halplibe.HalpLibe;
import turniplabs.halplibe.util.ArmorMaterialMixinInterface;
import turniplabs.halplibe.util.DirectoryManager;

@Mixin(value = PlayerRenderer.class, remap = false)
abstract public class PlayerRendererMixin extends LivingRenderer<EntityPlayer> {
Expand All @@ -25,7 +26,7 @@ private String changeTexturePath(String vanillaTexturePath, @Local(ordinal = 0)
return vanillaTexturePath;
}

String path = "/assets/" + modId + "/armor/" + itemArmor.material.name + "_" + (renderPass != 2 ? 1 : 2) + ".png";
String path = DirectoryManager.getArmorDirectory(modId) + itemArmor.material.name + "_" + (renderPass != 2 ? 1 : 2) + ".png";
HalpLibe.LOGGER.info(path);
return path;
}
Expand Down
117 changes: 117 additions & 0 deletions src/main/java/turniplabs/halplibe/util/DirectoryManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package turniplabs.halplibe.util;

import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;
import net.fabricmc.loader.api.metadata.ModMetadata;

import java.util.HashMap;

public class DirectoryManager {
public static final HashMap<String, HashMap<String, String>> resourceDirectoryMap = new HashMap<>();
public static final String BLOCK_TEXTURES = "halplibe:block_textures";
public static final String ITEM_TEXTURES = "halplibe:item_textures";
public static final String LANGUAGE = "halplibe:language";
public static final String SOUND = "halplibe:sound";
public static final String MUSIC = "halplibe:music";
public static final String STREAMING = "halplibe:streaming";
public static final String CAVE_MUSIC = "halplibe:cave_music";
public static final String ARMOR = "halplibe:armor";
public static final FabricLoader fl = FabricLoader.getInstance();
static {
for (ModContainer container : fl.getAllMods()){
String id = container.getMetadata().getId();
ModMetadata metadata = container.getMetadata();
if (metadata.containsCustomValue(BLOCK_TEXTURES)){
assignBlockTextureDirectory(id, metadata.getCustomValue(BLOCK_TEXTURES).getAsString());
}
if (metadata.containsCustomValue(ITEM_TEXTURES)){
assignItemTextureDirectory(id, metadata.getCustomValue(ITEM_TEXTURES).getAsString());
}
if (metadata.containsCustomValue(LANGUAGE)){
assignLanguageDirectory(id, metadata.getCustomValue(LANGUAGE).getAsString());
}
if (metadata.containsCustomValue(MUSIC)){
assignMusicDirectory(id, metadata.getCustomValue(MUSIC).getAsString());
}
if (metadata.containsCustomValue(SOUND)){
assignSoundDirectory(id, metadata.getCustomValue(SOUND).getAsString());
}
if (metadata.containsCustomValue(CAVE_MUSIC)){
assignCaveMusicDirectory(id, metadata.getCustomValue(CAVE_MUSIC).getAsString());
}
if (metadata.containsCustomValue(STREAMING)){
assignStreamingDirectory(id, metadata.getCustomValue(STREAMING).getAsString());
}
if (metadata.containsCustomValue(ARMOR)){
assignArmorDirectory(id, metadata.getCustomValue(ARMOR).getAsString());
}
}
}
public static void assignBlockTextureDirectory(String modID, String path){
resourceDirectoryMap.putIfAbsent(modID, new HashMap<>());
resourceDirectoryMap.get(modID).put(BLOCK_TEXTURES, path);
}
public static void assignItemTextureDirectory(String modID, String path){
resourceDirectoryMap.putIfAbsent(modID, new HashMap<>());
resourceDirectoryMap.get(modID).put(ITEM_TEXTURES, path);
}
public static void assignLanguageDirectory(String modID, String path){
resourceDirectoryMap.putIfAbsent(modID, new HashMap<>());
resourceDirectoryMap.get(modID).put(LANGUAGE, path);
}
public static void assignSoundDirectory(String modID, String path){
resourceDirectoryMap.putIfAbsent(modID, new HashMap<>());
resourceDirectoryMap.get(modID).put(SOUND, path);
}
public static void assignMusicDirectory(String modID, String path){
resourceDirectoryMap.putIfAbsent(modID, new HashMap<>());
resourceDirectoryMap.get(modID).put(MUSIC, path);
}
public static void assignCaveMusicDirectory(String modID, String path){
resourceDirectoryMap.putIfAbsent(modID, new HashMap<>());
resourceDirectoryMap.get(modID).put(CAVE_MUSIC, path);
}
public static void assignStreamingDirectory(String modID, String path){
resourceDirectoryMap.putIfAbsent(modID, new HashMap<>());
resourceDirectoryMap.get(modID).put(STREAMING, path);
}
public static void assignArmorDirectory(String modID, String path){
resourceDirectoryMap.putIfAbsent(modID, new HashMap<>());
resourceDirectoryMap.get(modID).put(ARMOR, path);
}
public static String getBlockTextureDirectory(String modID){
resourceDirectoryMap.putIfAbsent(modID, new HashMap<>());
return resourceDirectoryMap.get(modID).getOrDefault(BLOCK_TEXTURES, "/assets/" + modID + "/block/");
}
public static String getItemTextureDirectory(String modID){
resourceDirectoryMap.putIfAbsent(modID, new HashMap<>());
return resourceDirectoryMap.get(modID).getOrDefault(ITEM_TEXTURES, "/assets/" + modID + "/item/");
}
public static String getLanguageDirectory(String modID){
resourceDirectoryMap.putIfAbsent(modID, new HashMap<>());
return resourceDirectoryMap.get(modID).getOrDefault(LANGUAGE, "/lang/" + modID + "/");
}
public static String getSoundDirectory(String modID){
resourceDirectoryMap.putIfAbsent(modID, new HashMap<>());
return resourceDirectoryMap.get(modID).getOrDefault(SOUND, "/assets/" + modID + "/sound/");
}
public static String getMusicDirectory(String modID){
resourceDirectoryMap.putIfAbsent(modID, new HashMap<>());
return resourceDirectoryMap.get(modID).getOrDefault(MUSIC, "/assets/" + modID + "/music/");
}
public static String getCaveMusicDirectory(String modID){
resourceDirectoryMap.putIfAbsent(modID, new HashMap<>());
return resourceDirectoryMap.get(modID).getOrDefault(CAVE_MUSIC, "/assets/" + modID + "/cavemusic/");
}
public static String getStreamingDirectory(String modID){
resourceDirectoryMap.putIfAbsent(modID, new HashMap<>());
if (fl.getModContainer(modID).isPresent() && fl.getModContainer(modID).get().getMetadata().containsCustomValue(STREAMING)){
resourceDirectoryMap.get(modID).put(STREAMING, fl.getModContainer(modID).get().getMetadata().getCustomValue(STREAMING).getAsString());
}
return resourceDirectoryMap.get(modID).getOrDefault(STREAMING, "/assets/" + modID + "/streaming/");
}
public static String getArmorDirectory(String modID){
resourceDirectoryMap.putIfAbsent(modID, new HashMap<>());
return resourceDirectoryMap.get(modID).getOrDefault(ARMOR, "/assets/" + modID + "/armor/");
}
}
Loading