From bfbabd2f7ec8e8dc48cb9e492fa59aae811eb187 Mon Sep 17 00:00:00 2001 From: glowredman <35727266+glowredman@users.noreply.github.com> Date: Mon, 5 Feb 2024 22:03:47 +0100 Subject: [PATCH] Remove hard-dep on Additional Resources --- .../portablejim/audiodeath/AudioDeath.java | 119 ++++++++++-------- .../portablejim/audiodeath/ClientHandler.java | 40 ++++++ .../audiodeath/proxy/ClientProxy.java | 41 ------ .../portablejim/audiodeath/proxy/IProxy.java | 10 -- .../audiodeath/proxy/ServerProxy.java | 19 --- 5 files changed, 108 insertions(+), 121 deletions(-) create mode 100644 src/main/java/portablejim/audiodeath/ClientHandler.java delete mode 100644 src/main/java/portablejim/audiodeath/proxy/ClientProxy.java delete mode 100644 src/main/java/portablejim/audiodeath/proxy/IProxy.java delete mode 100644 src/main/java/portablejim/audiodeath/proxy/ServerProxy.java diff --git a/src/main/java/portablejim/audiodeath/AudioDeath.java b/src/main/java/portablejim/audiodeath/AudioDeath.java index da1b338..d862409 100644 --- a/src/main/java/portablejim/audiodeath/AudioDeath.java +++ b/src/main/java/portablejim/audiodeath/AudioDeath.java @@ -1,71 +1,88 @@ package portablejim.audiodeath; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Arrays; +import java.util.List; + +import net.minecraftforge.common.MinecraftForge; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import cpw.mods.fml.common.Loader; import cpw.mods.fml.common.Mod; import cpw.mods.fml.common.Mod.EventHandler; -import cpw.mods.fml.common.SidedProxy; import cpw.mods.fml.common.event.FMLPreInitializationEvent; -import cpw.mods.fml.common.eventhandler.SubscribeEvent; import cpw.mods.fml.relauncher.Side; -import net.minecraftforge.client.event.GuiOpenEvent; -import net.minecraftforge.common.MinecraftForge; -import org.apache.logging.log4j.Logger; -import portablejim.audiodeath.proxy.IProxy; - -import java.io.File; -import java.io.FileOutputStream; -@Mod(modid = AudioDeath.MODID) -public class AudioDeath -{ - public static final String MODID = "audiodeath"; +@Mod( + acceptableRemoteVersions = "*", + acceptedMinecraftVersions = "[1.7.10]", + modid = AudioDeath.MODID, + name = "Audio Death", + version = Tags.VERSION) +public class AudioDeath { - @SidedProxy(clientSide = "portablejim.audiodeath.proxy.ClientProxy", serverSide = "portablejim.audiodeath.proxy.ServerProxy") - public static IProxy proxy; - public static Logger modLogger; + static final String MODID = "audiodeath"; + private static final Logger LOGGER = LogManager.getLogger(MODID); + private static final List SOUNDS_JSON_TEXT; - - @SuppressWarnings("UnusedDeclaration") @EventHandler - public void preInit(FMLPreInitializationEvent event) - { - if(event.getSide() == Side.CLIENT) { - MinecraftForge.EVENT_BUS.register(this); + public void preInit(FMLPreInitializationEvent event) { + if (event.getSide() != Side.CLIENT) { + LOGGER.info( + "Server environment detected: Audio Death is client-only and can safely be removed on the server."); + return; } - modLogger = event.getModLog(); + MinecraftForge.EVENT_BUS.register(new ClientHandler()); - File additionalResourcesFolder = new File(proxy.getMinecraftDir(), "mods-resourcepacks"); - File audioDeathFolder = new File(additionalResourcesFolder, MODID); - File soundsFolder = new File(audioDeathFolder, "sounds"); + Path rootDir = event.getModConfigurationDirectory() + .toPath() + .getParent(); + + if (Loader.isModLoaded("additionalresources")) { + createSoundsJSON( + rootDir.resolve("mods-resourcepacks") + .resolve(MODID)); + return; + } + if (Loader.isModLoaded("txloader")) { + createSoundsJSON( + rootDir.resolve("config") + .resolve("txloader") + .resolve("load") + .resolve(MODID)); + return; + } + if (Loader.isModLoaded("resourceloader")) { + createSoundsJSON( + rootDir.resolve("resources") + .resolve(MODID)); + } + } + + private static void createSoundsJSON(Path path) { try { - if(!soundsFolder.exists()) { - //noinspection ResultOfMethodCallIgnored - soundsFolder.mkdirs(); + Files.createDirectories(path.resolve("sounds")); + Path soundsJson = path.resolve("sounds.json"); + if (Files.notExists(soundsJson)) { + Files.write(soundsJson, SOUNDS_JSON_TEXT); } - if(event.getSide() == Side.CLIENT) { - String soundsJson = "" + - "{\n" + - " \"audiodeath.death\": {\n" + - " \"category\": \"record\",\n" + - " \"sounds\": [ \"audiodeath:deathSound\" ]\n" + - " }\n" + - "}"; - File soundsFile = new File(audioDeathFolder, "sounds.json"); - if(!soundsFile.exists()) { - FileOutputStream soundsFileStream = new FileOutputStream(soundsFile); - soundsFileStream.write(soundsJson.getBytes("utf-8")); - soundsFileStream.close(); - } - } - } - catch (Exception e) { - event.getModLog().error("Error creating required files and directories!", e); + } catch (IOException e) { + LOGGER.warn("Failed to create sounds dir or sounds.json!", e); } } - @SuppressWarnings("UnusedDeclaration") - @SubscribeEvent - public void deathScreen(GuiOpenEvent event) { - proxy.handleDeath(event); + static { + SOUNDS_JSON_TEXT = Arrays.asList( + "{", + " \"audiodeath.death\": {", + " \"category\": \"record\",", + " \"sounds\": [ \"audiodeath:deathSound\" ]", + " }", + "}"); } } diff --git a/src/main/java/portablejim/audiodeath/ClientHandler.java b/src/main/java/portablejim/audiodeath/ClientHandler.java new file mode 100644 index 0000000..5e7bb6c --- /dev/null +++ b/src/main/java/portablejim/audiodeath/ClientHandler.java @@ -0,0 +1,40 @@ +package portablejim.audiodeath; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.audio.ISound; +import net.minecraft.client.audio.PositionedSoundRecord; +import net.minecraft.client.gui.GuiGameOver; +import net.minecraft.util.ResourceLocation; +import net.minecraftforge.client.event.GuiOpenEvent; + +import cpw.mods.fml.common.eventhandler.SubscribeEvent; + +public class ClientHandler { + + private static int audio = 0; + private final ISound deathSound; + + public ClientHandler() { + this.deathSound = PositionedSoundRecord + .func_147673_a(new ResourceLocation(AudioDeath.MODID, "audiodeath.death")); + } + + @SubscribeEvent + public void handleDeath(GuiOpenEvent event) { + if (event.gui instanceof GuiGameOver && Minecraft.getMinecraft().currentScreen == null + && !Minecraft.getMinecraft().thePlayer.isDead) { + if (audio == 0) { + Minecraft.getMinecraft() + .getSoundHandler() + .playSound(this.deathSound); + audio = 1; + } + } else { + Minecraft.getMinecraft() + .getSoundHandler() + .stopSound(this.deathSound); + audio = 0; + } + } + +} diff --git a/src/main/java/portablejim/audiodeath/proxy/ClientProxy.java b/src/main/java/portablejim/audiodeath/proxy/ClientProxy.java deleted file mode 100644 index 559affb..0000000 --- a/src/main/java/portablejim/audiodeath/proxy/ClientProxy.java +++ /dev/null @@ -1,41 +0,0 @@ -package portablejim.audiodeath.proxy; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.audio.ISound; -import net.minecraft.client.audio.PositionedSoundRecord; -import net.minecraft.client.gui.GuiGameOver; -import net.minecraft.util.ResourceLocation; -import net.minecraftforge.client.event.GuiOpenEvent; - -import java.io.File; - -@SuppressWarnings("UnusedDeclaration") -public class ClientProxy implements IProxy { - private static int audio = 0; - private ISound deathSound; - - public ClientProxy() { - ResourceLocation deathSoundAudioResource = new ResourceLocation("audiodeath:audiodeath.death"); - deathSound = PositionedSoundRecord.func_147673_a(deathSoundAudioResource); - } - - @Override - public File getMinecraftDir() { - return Minecraft.getMinecraft().mcDataDir; - } - - @Override - public void handleDeath(GuiOpenEvent event) { - //To change body of implemented methods use File | Settings | File Templates. - if(event.gui instanceof GuiGameOver && Minecraft.getMinecraft().currentScreen == null && !Minecraft.getMinecraft().thePlayer.isDead) { - if(audio == 0) { - Minecraft.getMinecraft().getSoundHandler().playSound(deathSound); - audio = 1; - } - } - else { - Minecraft.getMinecraft().getSoundHandler().stopSound(deathSound); - audio = 0; - } - } -} diff --git a/src/main/java/portablejim/audiodeath/proxy/IProxy.java b/src/main/java/portablejim/audiodeath/proxy/IProxy.java deleted file mode 100644 index 8932932..0000000 --- a/src/main/java/portablejim/audiodeath/proxy/IProxy.java +++ /dev/null @@ -1,10 +0,0 @@ -package portablejim.audiodeath.proxy; - -import net.minecraftforge.client.event.GuiOpenEvent; - -import java.io.File; - -public interface IProxy { - public File getMinecraftDir(); - public void handleDeath(GuiOpenEvent event); -} diff --git a/src/main/java/portablejim/audiodeath/proxy/ServerProxy.java b/src/main/java/portablejim/audiodeath/proxy/ServerProxy.java deleted file mode 100644 index 90173a1..0000000 --- a/src/main/java/portablejim/audiodeath/proxy/ServerProxy.java +++ /dev/null @@ -1,19 +0,0 @@ -package portablejim.audiodeath.proxy; - -import net.minecraft.server.MinecraftServer; -import net.minecraftforge.client.event.GuiOpenEvent; -import portablejim.audiodeath.AudioDeath; - -import java.io.File; - -@SuppressWarnings("UnusedDeclaration") -public class ServerProxy implements IProxy { - public File getMinecraftDir() { - return MinecraftServer.getServer().getFile("."); - } - - @Override - public void handleDeath(GuiOpenEvent event) { - AudioDeath.modLogger.info("Somebody died. However as this is a client-only mod, this mod does nothing."); - } -}