Skip to content

Commit

Permalink
Remove unneeded methods and comments
Browse files Browse the repository at this point in the history
Add example songs

Pick songs from resource folder instead of plugins
  • Loading branch information
IanTapply22 committed Dec 2, 2024
1 parent 6f326e1 commit f65254f
Show file tree
Hide file tree
Showing 14 changed files with 120 additions and 195 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

public class NbsPlayCommand extends WynncraftCommand {
Expand All @@ -26,7 +24,7 @@ public String syntax() {

@Override
public String description() {
return "Plays an NBS file from the plugins folder.";
return "Plays an NBS file from the internal resources folder.";
}

@Override
Expand All @@ -43,8 +41,7 @@ public int maxArgs() {
public void execute(CommandSender sender, String[] args) {
try {
Player player = (Player) sender;
File songFile = new File("./plugins/" + args[0]);
InputStream inputStreamSong = new FileInputStream(songFile);
InputStream inputStreamSong = getClass().getClassLoader().getResourceAsStream("songs/" + args[0]);
NBSSong song = NBSFormatDecoder.parse(inputStreamSong);
NbsCore.player = new PlayerOrientedSongPlayer(Wynncraft.getInstance().getNbsCore(), song);
NbsCore.player.setPlayer(player);
Expand All @@ -54,7 +51,8 @@ public void execute(CommandSender sender, String[] args) {
NbsCore.player.setPlaying(true);
sender.sendMessage("Started playing NBS song: " + args[0]);
} catch (Exception e) {
sender.sendMessage("The provided file does not exist. Please provide a valid NBS file in the plugins directory.");
e.printStackTrace();
sender.sendMessage("The provided file does not exist. Please provide a valid NBS file in the songs resource directory internally.");
}
}
}
28 changes: 14 additions & 14 deletions src/main/java/com/iantapply/wynncraft/nbs/NBSCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,29 @@ public NBSCore() {
this.instance = this;
}

public boolean isReceivingSong(Player p) {
return ((this.playingSongs.get(p.getName()) != null) && (!this.playingSongs.get(p.getName()).isEmpty()));
public boolean isReceivingSong(Player player) {
return ((this.playingSongs.get(player.getName()) != null) && (!this.playingSongs.get(player.getName()).isEmpty()));
}

public void stopPlaying(Player p) {
if (this.playingSongs.get(p.getName()) == null) {
public void stopPlaying(Player player) {
if (this.playingSongs.get(player.getName()) == null) {
return;
}
for (NBSSongPlayer s : this.playingSongs.get(p.getName())) {
s.removePlayer(p);
for (NBSSongPlayer songPlayer : this.playingSongs.get(player.getName())) {
songPlayer.removePlayer(player);
}
}

public void setPlayerVolume(Player p, byte volume) {
this.playerVolume.put(p.getName(), volume);
public void setPlayerVolume(Player player, byte volume) {
this.playerVolume.put(player.getName(), volume);
}

public byte getPlayerVolume(Player p) {
Byte b = this.playerVolume.get(p.getName());
if (b == null) {
b = 100;
playerVolume.put(p.getName(), b);
public byte getPlayerVolume(Player player) {
Byte volumeByte = this.playerVolume.get(player.getName());
if (volumeByte == null) {
volumeByte = 100;
this.playerVolume.put(player.getName(), volumeByte);
}
return b;
return volumeByte;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.iantapply.wynncraft.nbs.enums;

public enum NotePitch {

NOTE_0(0, 0.5F),
NOTE_1(1, 0.53F),
NOTE_2(2, 0.56F),
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/iantapply/wynncraft/nbs/enums/Sound.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

// TODO: Omit usage of valueOf and use PaperMC registry
public enum Sound {

NOTE_PIANO("NOTE_PIANO", "BLOCK_NOTE_HARP", "BLOCK_NOTE_BLOCK_HARP"),
NOTE_BASS("NOTE_BASS", "BLOCK_NOTE_BASS", "BLOCK_NOTE_BLOCK_BASS"),
NOTE_BASS_DRUM("NOTE_BASS_DRUM", "BLOCK_NOTE_BASEDRUM", "BLOCK_NOTE_BLOCK_BASEDRUM"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.iantapply.wynncraft.logger.Logger;
import com.iantapply.wynncraft.logger.LoggingLevel;
import com.iantapply.wynncraft.nbs.CustomInstrument;
import com.iantapply.wynncraft.nbs.instruments.NBSCustomInstrument;
import com.iantapply.wynncraft.nbs.utils.VersionUtils;
import com.iantapply.wynncraft.nbs.utils.InstrumentUtils;

Expand All @@ -11,14 +11,13 @@
import java.util.Arrays;
import java.util.HashMap;

// TODO: Better error handling and rework/lighten up
public class NBSFormatDecoder {

public static NBSSong parse(File decodeFile) {
try {
return parse(new FileInputStream(decodeFile), decodeFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
Logger.log(LoggingLevel.ERROR, "The file provided could not be found. Please provide a valid file.");
}
return null;
}
Expand All @@ -32,17 +31,17 @@ private static NBSSong parse(InputStream inputStream, File songFile) {
try {
DataInputStream dataInputStream = new DataInputStream(inputStream);
short length = readShort(dataInputStream);
int firstcustominstrument = 10;
int firstcustominstrumentdiff;
int firstCustomInstrument = 10;
int firstCustomInstrumentDelta;
int nbsversion = 0;
if (length == 0) {
nbsversion = dataInputStream.readByte();
firstcustominstrument = dataInputStream.readByte();
firstCustomInstrument = dataInputStream.readByte();
if (nbsversion >= 3) {
length = readShort(dataInputStream);
}
}
firstcustominstrumentdiff = InstrumentUtils.getCustomInstrumentFirstIndex() - firstcustominstrument;
firstCustomInstrumentDelta = InstrumentUtils.getCustomInstrumentFirstIndex() - firstCustomInstrument;
short songHeight = readShort(dataInputStream);
String title = readString(dataInputStream);
String author = readString(dataInputStream);
Expand Down Expand Up @@ -75,8 +74,8 @@ private static NBSSong parse(InputStream inputStream, File songFile) {
layer += jumpLayers;
byte instrument = dataInputStream.readByte();

if (firstcustominstrumentdiff > 0 && instrument >= firstcustominstrument){
instrument += firstcustominstrumentdiff;
if (firstCustomInstrumentDelta > 0 && instrument >= firstCustomInstrument){
instrument += (byte) firstCustomInstrumentDelta;
}

byte key = dataInputStream.readByte();
Expand All @@ -92,54 +91,49 @@ private static NBSSong parse(InputStream inputStream, File songFile) {
}
}

if (nbsversion > 0 && nbsversion < 3) {
length = tick;
}
if (nbsversion > 0 && nbsversion < 3) length = tick;

for (int i = 0; i < songHeight; i++) {
NBSLayer layer = layerHashMap.get(i);

String name = readString(dataInputStream);
if (nbsversion >= 4){
dataInputStream.readByte(); // layer lock
}
if (nbsversion >= 4) dataInputStream.readByte(); // layer lock

byte volume = dataInputStream.readByte();
if (nbsversion >= 2){
dataInputStream.readByte(); // layer stereo
}
if (nbsversion >= 2) dataInputStream.readByte(); // layer stereo

if (layer != null) {
layer.setName(name);
layer.setVolume(volume);
}
}

byte customInstrumentAmount = dataInputStream.readByte();
CustomInstrument[] customInstrumentsArray = new CustomInstrument[customInstrumentAmount];
NBSCustomInstrument[] customInstrumentsArray = new NBSCustomInstrument[customInstrumentAmount];

for (int index = 0; index < customInstrumentAmount; index++) {
customInstrumentsArray[index] = new CustomInstrument((byte) index,
customInstrumentsArray[index] = new NBSCustomInstrument((byte) index,
readString(dataInputStream), readString(dataInputStream));
dataInputStream.readByte(); // pitch
dataInputStream.readByte(); // key
}

if (firstcustominstrumentdiff < 0){
ArrayList<CustomInstrument> customInstruments = VersionUtils.getVersionCustomInstrumentsForSong(firstcustominstrument);
if (firstCustomInstrumentDelta < 0){
ArrayList<NBSCustomInstrument> customInstruments = VersionUtils.getVersionCustomInstrumentsForSong(firstCustomInstrument);
customInstruments.addAll(Arrays.asList(customInstrumentsArray));
customInstrumentsArray = customInstruments.toArray(customInstrumentsArray);
} else {
firstcustominstrument += firstcustominstrumentdiff;
firstCustomInstrument += firstCustomInstrumentDelta;
}

return new NBSSong(speed, layerHashMap, songHeight, length, title,
author, description, songFile, firstcustominstrument, customInstrumentsArray);
author, description, songFile, firstCustomInstrument, customInstrumentsArray);
} catch (FileNotFoundException e) {
e.printStackTrace();
Logger.log(LoggingLevel.ERROR, "The file provided could not be found. Please provide a valid file.");
} catch (EOFException e) {
Logger.log(LoggingLevel.ERROR, "File is corrupted: " + e.getMessage());
Logger.log(LoggingLevel.ERROR, "File is corrupted or wrong NBS version/format: " + e.getMessage());
} catch (IOException e) {
e.printStackTrace();
Logger.log(LoggingLevel.ERROR, "Could not form IO link: " + e.getMessage());
}
return null;
}
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/com/iantapply/wynncraft/nbs/handling/NBSSong.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.iantapply.wynncraft.nbs.handling;

import com.iantapply.wynncraft.nbs.CustomInstrument;
import com.iantapply.wynncraft.nbs.instruments.NBSCustomInstrument;
import lombok.Getter;

import java.io.File;
Expand All @@ -18,27 +18,27 @@ public class NBSSong {
private final String description;
private final float speed;
private final float delay;
private final CustomInstrument[] customInstruments;
private final NBSCustomInstrument[] customInstruments;
private final int firstCustomInstrumentIndex;
private final boolean isStereo;

public NBSSong(float speed, HashMap<Integer, NBSLayer> layerHashMap,
short songHeight, final short length, String title, String author,
String description, File path, int firstCustomInstrumentIndex, CustomInstrument[] customInstruments) {
String description, File path, int firstCustomInstrumentIndex, NBSCustomInstrument[] customInstruments) {
this(speed, layerHashMap, songHeight, length, title, author, description, path, firstCustomInstrumentIndex, customInstruments, false);
}

public NBSSong(float speed, HashMap<Integer, NBSLayer> layerHashMap,
short songHeight, final short length, String title, String author,
String description, File path, int firstCustomInstrumentIndex, CustomInstrument[] customInstruments, boolean isStereo) {
short songHeight, final short length, String title, String author,
String description, File path, int firstCustomInstrumentIndex, NBSCustomInstrument[] customInstruments, boolean isStereo) {
this(speed, layerHashMap, songHeight, length, title, author, "", description, path, firstCustomInstrumentIndex, customInstruments, isStereo);
}

public NBSSong(float speed, HashMap<Integer, NBSLayer> layerHashMap,
short songHeight, final short length, String title, String author, String originalAuthor,
String description, File path, int firstCustomInstrumentIndex, CustomInstrument[] customInstruments, boolean isStereo) {
short songHeight, final short length, String title, String author, String originalAuthor,
String description, File path, int firstCustomInstrumentIndex, NBSCustomInstrument[] customInstruments, boolean isStereo) {
this.speed = speed;
delay = 20 / speed;
this.delay = 20 / speed;
this.layerHashMap = layerHashMap;
this.songHeight = songHeight;
this.length = length;
Expand All @@ -53,6 +53,6 @@ public NBSSong(float speed, HashMap<Integer, NBSLayer> layerHashMap,
}

public boolean isStereo() {
return isStereo;
return this.isStereo;
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package com.iantapply.wynncraft.nbs;
package com.iantapply.wynncraft.nbs.instruments;

import com.iantapply.wynncraft.nbs.enums.Sound;
import lombok.Getter;

@Getter
public class CustomInstrument{
public class NBSCustomInstrument {
private final byte index;
private final String name;
private final String soundFileName;
private org.bukkit.Sound sound;

public CustomInstrument(byte index, String name, String soundFileName) {
public NBSCustomInstrument(byte index, String name, String soundFileName) {
this.index = index;
this.name = name;
this.soundFileName = soundFileName.replaceAll(".ogg", "");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.iantapply.wynncraft.nbs;
package com.iantapply.wynncraft.nbs.instruments;

import com.iantapply.wynncraft.nbs.utils.InstrumentUtils;

public class Instrument {
public class NBSInstrument {

public static org.bukkit.Sound getInstrument(byte instrument) {
return org.bukkit.Sound.valueOf(getInstrumentName(instrument));
Expand All @@ -11,8 +11,4 @@ public static org.bukkit.Sound getInstrument(byte instrument) {
public static String getInstrumentName(byte instrument) {
return InstrumentUtils.getInstrumentName(instrument);
}

public static org.bukkit.Instrument getBukkitInstrument(byte instrument) {
return InstrumentUtils.getBukkitInstrument(instrument);
}
}
Loading

0 comments on commit f65254f

Please sign in to comment.