Skip to content

Commit

Permalink
Streamline for data-driven jukebox songs
Browse files Browse the repository at this point in the history
  • Loading branch information
OpenBagTwo committed Jun 11, 2024
1 parent fcd90cb commit 572a3e8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 108 deletions.
2 changes: 1 addition & 1 deletion src/main/java/net/openbagtwo/foxnap/FoxNap.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void onInitialize() {
List<SecretlyJustAGoatHorn> instruments = InstrumentRegistry.init();
List<Item> custom_discs = DiscRegistry.init(
config.getNumDiscs(),
config.getTrackLengths()
config.getMaximumNumberOfDiscs()
);
if (config.getMaestroEnabled()) {
Conductor.init(instruments, custom_discs);
Expand Down
56 changes: 9 additions & 47 deletions src/main/java/net/openbagtwo/foxnap/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import net.fabricmc.loader.api.FabricLoader;
import org.yaml.snakeyaml.DumperOptions;
Expand All @@ -36,9 +34,9 @@ public class Config {
private int numDiscs;

/**
* The length of each track in seconds
* The number of disc items that will be registered for the mod
*/
private List<Integer> trackLengths;
private int maximumNumberOfDiscs;

/**
* Whether to enable the Maestro
Expand All @@ -52,18 +50,11 @@ public int getNumDiscs() {
return this.numDiscs;
}

/**
* Get the length (in seconds) to register for each track on the server side
*/
public List<Integer> getTrackLengths() {
return new ArrayList<>(trackLengths);
}

/**
* Get the number of discs to be added to the server-side item registry
*/
public int getMaximumNumberOfDiscs() {
return this.trackLengths.size();
return this.maximumNumberOfDiscs;
}

/**
Expand All @@ -78,7 +69,6 @@ public boolean getMaestroEnabled() {
*/
private static final int DEFAULT_N_DISCS = 7;
private static final int DEFAULT_MAX_DISCS = 64;
private static final int DEFAULT_TRACK_LENGTH = 600; // technically default default track length
private static final boolean DEFAULT_MAESTRO_ENABLED = true;

/**
Expand All @@ -87,19 +77,19 @@ public boolean getMaestroEnabled() {
public static Config loadConfiguration() {
Config config;
try {
config = net.openbagtwo.foxnap.config.Config.fromConfigFile();
config = fromConfigFile();
LOGGER.debug("Loaded " + MOD_NAME + " configuration.");
} catch (FileNotFoundException e) {
LOGGER.warn("No " + MOD_NAME + " configuration file found.");
try {
net.openbagtwo.foxnap.config.Config.writeDefaultConfigFile();
writeDefaultConfigFile();
} catch (ConfigException writee) {
LOGGER.error("Could not write " + MOD_NAME + " configuration:\n" + writee);
}
config = net.openbagtwo.foxnap.config.Config.getDefaultConfiguration();
config = getDefaultConfiguration();
} catch (ConfigException e) {
LOGGER.error(MOD_NAME + " configuration is invalid:\n" + e);
config = net.openbagtwo.foxnap.config.Config.getDefaultConfiguration();
config = getDefaultConfiguration();
}
return config;
}
Expand All @@ -118,10 +108,7 @@ private static Config getDefaultConfiguration() {
LOGGER.info("Loading default " + MOD_NAME + " configuration");
Config config = new Config();
config.numDiscs = DEFAULT_N_DISCS;
config.trackLengths = new ArrayList<>();
for (int i = 0; i < DEFAULT_MAX_DISCS; i++) {
config.trackLengths.add(DEFAULT_TRACK_LENGTH);
}
config.maximumNumberOfDiscs = DEFAULT_MAX_DISCS;
config.enableMaestro = true;
return config;
}
Expand All @@ -143,7 +130,6 @@ private static void writeDefaultConfigFile() throws ConfigException {
}
Map<String, Object> writeme = new LinkedHashMap<>();
writeme.put("n_discs", DEFAULT_N_DISCS);
writeme.put("default_track_length", DEFAULT_TRACK_LENGTH);

(new Yaml(configFormat)).dump(writeme, configWriter);
LOGGER.info("Wrote " + MOD_NAME + " configuration file to " + config_path);
Expand All @@ -164,40 +150,16 @@ private static Config fromConfigFile() throws FileNotFoundException, ConfigExcep
try {
// Now we actually construct the thing
int numDiscs = Integer.parseInt(settings.getOrDefault("n_discs", DEFAULT_N_DISCS).toString());
int defaultTrackLength = Integer.parseInt(settings.getOrDefault("default_track_length",
DEFAULT_TRACK_LENGTH).toString());
int maxNumDiscs = Integer.parseInt(
settings.getOrDefault("max_discs", DEFAULT_MAX_DISCS).toString()
);
boolean enableMaestro = Boolean.parseBoolean(
settings.getOrDefault("enable_maestro", DEFAULT_MAESTRO_ENABLED).toString()
);

ArrayList<Integer> trackLengths = new ArrayList<>();
for (int i = 0; i < maxNumDiscs; i++) {
trackLengths.add(defaultTrackLength);
}

if (settings.containsKey("track_lengths")) {
Object trackSpecs = settings.get("track_lengths");
if (trackSpecs instanceof Map trackMap) {
for (Object key : trackMap.keySet()) {
trackLengths.set(
Integer.parseInt(key.toString()) - 1,
Integer.parseInt(trackMap.get(key).toString())
);
}
} else if (trackSpecs instanceof List trackList) {
for (int i = 0; i < trackList.size(); i++) {
trackLengths.set(i, Integer.parseInt(trackList.get(i).toString()));
}
} else {
throw new ConfigException("Track list is invalid");
}
}
Config config = new Config();
config.numDiscs = numDiscs;
config.trackLengths = trackLengths;
config.maximumNumberOfDiscs = maxNumDiscs;
config.enableMaestro = enableMaestro;
return config;

Expand Down
80 changes: 20 additions & 60 deletions src/main/java/net/openbagtwo/foxnap/discs/DiscRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@
import java.util.ArrayList;
import java.util.List;
import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents;
import net.minecraft.block.jukebox.JukeboxSong;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroups;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.sound.SoundEvent;
import net.minecraft.text.Text;
import net.minecraft.registry.Registry;
import net.minecraft.registry.Registries;
import net.minecraft.util.Identifier;
import net.minecraft.util.Rarity;
import net.minecraft.util.Util;
import net.openbagtwo.foxnap.FoxNap;

/**
Expand All @@ -27,47 +24,21 @@ public class DiscRegistry {
/**
* Create a new music disc, and register the item and its sound event
*
* @param track The sound event (read: jukebox song) to tie to the disc
* @param comparatorOutput The output signal a comparator should read from a jukebox with this
* disc loaded
* @param trackLength The length of the track in seconds. This value is currently only used
* by Allays (presumably to determine when to stop dancing and duping?).
* @param track The sound event (read: jukebox song) to tie to the disc
* @return the fully instantiated and registered music disc
*/
public static Item registerDisc(Track track, int comparatorOutput, int trackLength
) {
return registerDisc(track, comparatorOutput, trackLength, track.getId().getPath());
public static Item registerDisc(Track track) {
return registerDisc(track, track.getId().getPath());
}

/**
* Create a new music disc, and register the item and its sound event
*
* @param track The sound event (read: jukebox song) to tie to the disc
* @param comparatorOutput The output signal a comparator should read from a jukebox with this
* disc loaded
* @param trackLength The length of the track in seconds. This value is currently only used
* by Allays (presumably to determine when to stop dancing and duping?).
* @param trackName The identifier for the music disc (if distinct from the track ID)
* @param track The sound event (read: jukebox song) to tie to the disc
* @param trackName The identifier for the music disc (if distinct from the track ID)
* @return the fully instantiated and registered music disc
*/
public static Item registerDisc(
Track track,
int comparatorOutput,
int trackLength,
String trackName
) {
JukeboxSong jukeboxSong = new JukeboxSong(
registerTrack(track),
Text.translatable(
Util.createTranslationKey(
"jukebox_song",
RegistryKey.of(RegistryKeys.JUKEBOX_SONG, track.getId())
.getValue()
)
),
(float) trackLength,
comparatorOutput
);
public static Item registerDisc(Track track, String trackName) {
Item disc = Registry.register(
Registries.ITEM,
Identifier.of(FoxNap.MOD_ID, trackName),
Expand All @@ -78,9 +49,7 @@ public static Item registerDisc(
)
);

FoxNap.LOGGER.info(
"Registered " + disc + " with comparator signal "
+ jukeboxSong.comparatorOutput());
FoxNap.LOGGER.debug("Registered " + disc);
return disc;
}

Expand All @@ -93,19 +62,16 @@ private static RegistryEntry.Reference<SoundEvent> registerTrack(Track track) {
* with a sound event named track_i and emitting a comparator signal of strength i), starting at i
* = 1
*
* @param trackLengths The length (in seconds) of the tracks that will be made available on the
* use on the server (read: for the Maestro).
* @param numberOfDiscs The length (in seconds) of the tracks that will be made available on the
* use on the server (read: for the Maestro).
* @return A list of fully instantiated and registered music discs
*/
public static List<Item> init(List<Integer> trackLengths) {
public static List<Item> init(int numberOfDiscs) {
ArrayList<Item> discs = new ArrayList<>();
for (int i = 1; i <= trackLengths.size(); i++) {
for (int i = 1; i <= numberOfDiscs; i++) {
Track track = new Track(String.format("track_%d", i));
Item disc = registerDisc(
track,
(i - 1) % 15 + 1,
trackLengths.get(i - 1)
);
registerTrack(track);
Item disc = registerDisc(track);
discs.add(disc);
ItemGroupEvents.modifyEntriesEvent(ItemGroups.TOOLS).register(entries -> entries.add(disc));
}
Expand All @@ -117,28 +83,22 @@ public static List<Item> init(List<Integer> trackLengths) {
* dummy/placeholder discs to prevent client/server conflicts
*
* @param numberOfDiscs The number of discs that will actually be available for use
* @param trackLengths The length (in seconds) of the tracks that need to be registered
* server-side (the size of this array de facto sets the maximum number of
* discs)
* @param maxNumDiscs The maximum number of discs (the number of disc items that will be created
* in the namespace)
* @return The list of discs that should actually be available for use
*/
public static List<Item> init(int numberOfDiscs, List<Integer> trackLengths) {
public static List<Item> init(int numberOfDiscs, int maxNumDiscs) {
Track placeholder = new Track("placeholder");
placeholder.isPlaceholder = true;
registerTrack(placeholder);

int placeholderCount = 0;
for (int i = numberOfDiscs + 1; i <= trackLengths.size(); i++) {
registerDisc(
placeholder,
0,
trackLengths.get(i - 1),
String.format("track_%d", i)
);
for (int i = numberOfDiscs + 1; i <= maxNumDiscs; i++) {
registerDisc(placeholder, String.format("track_%d", i));
placeholderCount++;
}
FoxNap.LOGGER.info(String.format("Registered %d placeholder discs", placeholderCount));
FoxNap.LOGGER.debug(String.format("Registered %d placeholder discs", placeholderCount));

return init(trackLengths.subList(0, Math.min(numberOfDiscs, trackLengths.size())));
return init(numberOfDiscs);
}
}

0 comments on commit 572a3e8

Please sign in to comment.