Skip to content

Commit

Permalink
Remove hard-dep on Additional Resources
Browse files Browse the repository at this point in the history
  • Loading branch information
glowredman committed Feb 5, 2024
1 parent c8853a2 commit bfbabd2
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 121 deletions.
119 changes: 68 additions & 51 deletions src/main/java/portablejim/audiodeath/AudioDeath.java
Original file line number Diff line number Diff line change
@@ -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<String> 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\" ]",
" }",
"}");
}
}
40 changes: 40 additions & 0 deletions src/main/java/portablejim/audiodeath/ClientHandler.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

}
41 changes: 0 additions & 41 deletions src/main/java/portablejim/audiodeath/proxy/ClientProxy.java

This file was deleted.

10 changes: 0 additions & 10 deletions src/main/java/portablejim/audiodeath/proxy/IProxy.java

This file was deleted.

19 changes: 0 additions & 19 deletions src/main/java/portablejim/audiodeath/proxy/ServerProxy.java

This file was deleted.

0 comments on commit bfbabd2

Please sign in to comment.