Skip to content

Commit

Permalink
Resolve compile errors
Browse files Browse the repository at this point in the history
and get the mod to a place where music discs will at least load in the same

So far the implementation of tracks and placeholders conforms to the current API, but then again, the discs aren't actually playing any music!
  • Loading branch information
OpenBagTwo committed Jun 11, 2024
1 parent 7f29f92 commit 481d847
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 28 deletions.
72 changes: 51 additions & 21 deletions src/main/java/net/openbagtwo/foxnap/discs/DiscRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.sound.SoundEvent;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
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 @@ -23,47 +23,74 @@
*/
public class DiscRegistry {


/**
* Create a new music disc, and register the item and its sound event
*
* @param trackName The name of the sound event (as specified in sounds.json)
* @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?).
* @return the fully instantiated and registered music disc
*/
public static Item registerDisc(String trackName, int comparatorOutput, int trackLength) {
public static Item registerDisc(Track track, int comparatorOutput, int trackLength
) {
return registerDisc(track, comparatorOutput, trackLength, 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)
* @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(trackName),
registerTrack(track),
Text.translatable(
Util.createTranslationKey(
"jukebox_song",
RegistryKey.of(RegistryKeys.JUKEBOX_SONG, Identifier.of(FoxNap.MOD_ID, trackName))
RegistryKey.of(RegistryKeys.JUKEBOX_SONG, track.getId())
.getValue()
)
),
(float) trackLength,
comparatorOutput
);
Item disc = new Item((new Item.Settings()).maxCount(1).rarity(Rarity.RARE).jukeboxPlayable(
RegistryKey.of(RegistryKeys.JUKEBOX_SONG, Identifier.of(FoxNap.MOD_ID, trackName)))
Item disc = Registry.register(
Registries.ITEM,
Identifier.of(FoxNap.MOD_ID, trackName),
new Item(
(new Item.Settings()).maxCount(1).rarity(Rarity.RARE).jukeboxPlayable(
RegistryKey.of(RegistryKeys.JUKEBOX_SONG, track.getId())
)
)
);

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

private static RegistryEntry.Reference<SoundEvent> registerTrack(String trackName) {
Identifier track_id = Identifier.of(FoxNap.MOD_ID, trackName);
return Registry.registerReference(Registries.SOUND_EVENT, track_id, new Track(track_id));
private static RegistryEntry.Reference<SoundEvent> registerTrack(Track track) {
return Registry.registerReference(Registries.SOUND_EVENT, track.getId(), track);
}

/**
* Create and register a procedurally-generated set of music discs named track_i (each registered
* with a sound event name track_i and emitting a comparator signal of strength i), starting at i
* 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
Expand All @@ -73,8 +100,9 @@ private static RegistryEntry.Reference<SoundEvent> registerTrack(String trackNam
public static List<Item> init(List<Integer> trackLengths) {
ArrayList<Item> discs = new ArrayList<>();
for (int i = 1; i <= trackLengths.size(); i++) {
Track track = new Track(String.format("track_%d", i));
Item disc = registerDisc(
String.format("track_%d", i),
track,
(i - 1) % 15 + 1,
trackLengths.get(i - 1)
);
Expand All @@ -95,19 +123,21 @@ public static List<Item> init(List<Integer> trackLengths) {
* @return The list of discs that should actually be available for use
*/
public static List<Item> init(int numberOfDiscs, List<Integer> trackLengths) {
registerTrack("placeholder"); // really tempting to make placeholder a placeholder
Track placeholder = new Track("placeholder");
placeholder.isPlaceholder = true;
registerTrack(placeholder);

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

return init(trackLengths.subList(0, Math.min(numberOfDiscs, trackLengths.size())));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void onInitializeModelLoader(Context pluginContext) {
pluginContext.modifyModelOnLoad().register((original, context) -> {
for (int i = this.placeholderStart; i < this.maxNumDiscs; i++) {
Identifier matchMe = Identifier.of(MOD_ID, String.format("item/track_%d", i + 1));
if (context.resourceId().equals(matchMe)) {
if (context.topLevelId().equals(matchMe)) {
return context.getOrLoadModel(placeholder);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/openbagtwo/foxnap/discs/Track.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public class Track extends SoundEvent {
*/
public boolean isPlaceholder = false;

public Track(Identifier id) {
super(id, 16.0f, false);
public Track(String trackName) {
super(Identifier.of(FoxNap.MOD_ID, trackName), 16.0f, false);
}

@Override
Expand Down
21 changes: 17 additions & 4 deletions src/main/java/net/openbagtwo/foxnap/villagers/Conductor.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
import net.minecraft.item.Item;
import net.minecraft.item.Items;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.sound.SoundEvent;
import net.minecraft.util.Identifier;
import net.minecraft.registry.RegistryKey;
import net.minecraft.village.TradeOffers;
import net.minecraft.village.VillagerProfession;
import net.minecraft.world.poi.PointOfInterestType;
import net.openbagtwo.foxnap.FoxNap;
import net.openbagtwo.foxnap.instruments.InstrumentRegistry;
import net.openbagtwo.foxnap.instruments.SecretlyJustAGoatHorn;
Expand All @@ -40,10 +42,21 @@ private static VillagerProfession makeConductor() {
ImmutableSet.copyOf(Blocks.JUKEBOX.getStateManager().getStates())
);

return VillagerProfession.register(
(Identifier.of(MOD_ID, "conductor")).toString(),
RegistryKey.of(Registries.POINT_OF_INTEREST_TYPE.getKey(), poi_id),
CONDUCTOR_WORK_SOUND
return Registry.register(
Registries.VILLAGER_PROFESSION,
Identifier.of(MOD_ID, "conductor"),
new VillagerProfession(
"conductor",
entry -> entry.matchesKey(
RegistryKey.of(Registries.POINT_OF_INTEREST_TYPE.getKey(), poi_id)
),
entry -> entry.matchesKey(
RegistryKey.of(Registries.POINT_OF_INTEREST_TYPE.getKey(), poi_id)
),
ImmutableSet.of(),
ImmutableSet.of(),
CONDUCTOR_WORK_SOUND
)
);
}

Expand Down

0 comments on commit 481d847

Please sign in to comment.