Skip to content

Commit

Permalink
Create basic NBS song playing system
Browse files Browse the repository at this point in the history
  • Loading branch information
IanTapply22 committed Dec 2, 2024
1 parent e3444ec commit 6f326e1
Show file tree
Hide file tree
Showing 22 changed files with 1,143 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/main/java/com/iantapply/wynncraft/Wynncraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@

import com.iantapply.wynncraft.command.CommandCore;
import com.iantapply.wynncraft.database.DatabaseCore;
import com.iantapply.wynncraft.event.PlayerJoinEvent;
import com.iantapply.wynncraft.logger.Logger;
import com.iantapply.wynncraft.nbs.NBSCore;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;

public final class Wynncraft extends JavaPlugin {
@Getter
public static Wynncraft instance;
@Getter
public CommandCore commandCore;
@Getter
public NBSCore nbsCore;

@Override
public void onEnable() {
Expand All @@ -25,6 +30,10 @@ public void onEnable() {
this.commandCore.initialize();
this.commandCore.registerCommands();

this.nbsCore = new NBSCore();

Bukkit.getServer().getPluginManager().registerEvents(new PlayerJoinEvent(), this);

Logger.logStartup();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.iantapply.wynncraft.command.commands.guild.GuildCommandsCore;
import com.iantapply.wynncraft.command.commands.housing.HousingCommandsCore;
import com.iantapply.wynncraft.command.commands.ignore.IgnoreCommandsCore;
import com.iantapply.wynncraft.command.commands.nbs.NbsCore;
import com.iantapply.wynncraft.command.commands.party.PartyCommandsCore;
import com.iantapply.wynncraft.command.commands.toggle.ToggleCommandsCore;
import com.iantapply.wynncraft.logger.Logger;
Expand Down Expand Up @@ -66,6 +67,8 @@ public void initialize() {
CosmeticCommandsCore cosmeticCommandsCore = new CosmeticCommandsCore();
ToggleCommandsCore toggleCommandsCore = new ToggleCommandsCore();

NbsCore nbsCore = new NbsCore();

gameCommandsCore.initialize();
partyCommandsCore.initialize();
ignoreCommandsCore.initialize();
Expand All @@ -74,6 +77,7 @@ public void initialize() {
guildCommandsCore.initialize();
cosmeticCommandsCore.initialize();
toggleCommandsCore.initialize();
nbsCore.initialize();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.iantapply.wynncraft.command.commands.nbs;

import com.iantapply.wynncraft.command.WynncraftCommand;
import org.bukkit.command.CommandSender;

import java.util.ArrayList;

public class NbsCommand extends WynncraftCommand {
@Override
public String name() {
return "nbs";
}

@Override
public String syntax() {
return "nbs <play|stop> <args>";
}

@Override
public String description() {
return "Handles the testing and playing of NBS files.";
}

@Override
public ArrayList<WynncraftCommand> subcommands() {
ArrayList<WynncraftCommand> subcommands = new ArrayList<>();
subcommands.add(new NbsPlayCommand());
subcommands.add(new NbsStopCommand());

return subcommands;
}

@Override
public int minArgs() {
return 1;
}

@Override
public int maxArgs() {
return 2;
}

@Override
public void execute(CommandSender sender, String[] args) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.iantapply.wynncraft.command.commands.nbs;

import com.iantapply.wynncraft.Wynncraft;
import com.iantapply.wynncraft.nbs.players.PlayerOrientedSongPlayer;

public class NbsCore {
public static PlayerOrientedSongPlayer player;

public void initialize() {
Wynncraft.getInstance().getCommandCore().stageCommand(new NbsCommand());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.iantapply.wynncraft.command.commands.nbs;

import com.iantapply.wynncraft.Wynncraft;
import com.iantapply.wynncraft.command.WynncraftCommand;
import com.iantapply.wynncraft.nbs.handling.NBSFormatDecoder;
import com.iantapply.wynncraft.nbs.handling.NBSSong;
import com.iantapply.wynncraft.nbs.players.PlayerOrientedSongPlayer;
import org.bukkit.SoundCategory;
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 {
@Override
public String name() {
return "play";
}

@Override
public String syntax() {
return "play <file name>";
}

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

@Override
public int minArgs() {
return 1;
}

@Override
public int maxArgs() {
return 1;
}

@Override
public void execute(CommandSender sender, String[] args) {
try {
Player player = (Player) sender;
File songFile = new File("./plugins/" + args[0]);
InputStream inputStreamSong = new FileInputStream(songFile);
NBSSong song = NBSFormatDecoder.parse(inputStreamSong);
NbsCore.player = new PlayerOrientedSongPlayer(Wynncraft.getInstance().getNbsCore(), song);
NbsCore.player.setPlayer(player);
NbsCore.player.setSoundCategory(SoundCategory.RECORDS);

NbsCore.player.addPlayer(player);
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.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.iantapply.wynncraft.command.commands.nbs;

import com.iantapply.wynncraft.command.WynncraftCommand;
import org.bukkit.command.CommandSender;

public class NbsStopCommand extends WynncraftCommand {
@Override
public String name() {
return "stop";
}

@Override
public String syntax() {
return "stop";
}

@Override
public String description() {
return "Stops playing the currently playing NBS song.";
}

@Override
public int minArgs() {
return 0;
}

@Override
public int maxArgs() {
return 0;
}

@Override
public void execute(CommandSender sender, String[] args) {
try {
NbsCore.player.destroy();
sender.sendMessage("Stopped playing NBS song.");
} catch(Exception e) {
sender.sendMessage("Could not stop playing NBS song.");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
package com.iantapply.wynncraft.event;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

public class PlayerJoinEvent {
public class PlayerJoinEvent implements Listener {

@EventHandler
public void onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent event) {
// TODO: Create a model that will store the players data upon joining, that including the stats from DB and NBS player instance
}
}
21 changes: 21 additions & 0 deletions src/main/java/com/iantapply/wynncraft/nbs/CustomInstrument.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.iantapply.wynncraft.nbs;

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

@Getter
public class CustomInstrument{
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) {
this.index = index;
this.name = name;
this.soundFileName = soundFileName.replaceAll(".ogg", "");
if (this.soundFileName.equalsIgnoreCase("pling")){
this.sound = Sound.NOTE_PLING.bukkitSound();
}
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/iantapply/wynncraft/nbs/Instrument.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.iantapply.wynncraft.nbs;

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

public class Instrument {

public static org.bukkit.Sound getInstrument(byte instrument) {
return org.bukkit.Sound.valueOf(getInstrumentName(instrument));
}

public static String getInstrumentName(byte instrument) {
return InstrumentUtils.getInstrumentName(instrument);
}

public static org.bukkit.Instrument getBukkitInstrument(byte instrument) {
return InstrumentUtils.getBukkitInstrument(instrument);
}
}
45 changes: 45 additions & 0 deletions src/main/java/com/iantapply/wynncraft/nbs/NBSCore.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.iantapply.wynncraft.nbs;

import com.iantapply.wynncraft.nbs.players.NBSSongPlayer;
import lombok.Getter;
import org.bukkit.entity.Player;

import java.util.ArrayList;
import java.util.HashMap;

@Getter
public class NBSCore {
public HashMap<String, ArrayList<NBSSongPlayer>> playingSongs = new HashMap<>();
public HashMap<String, Byte> playerVolume = new HashMap<>();
public NBSCore instance;

public NBSCore() {
this.instance = this;
}

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

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

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

public byte getPlayerVolume(Player p) {
Byte b = this.playerVolume.get(p.getName());
if (b == null) {
b = 100;
playerVolume.put(p.getName(), b);
}
return b;
}
}
48 changes: 48 additions & 0 deletions src/main/java/com/iantapply/wynncraft/nbs/enums/NotePitch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.iantapply.wynncraft.nbs.enums;

public enum NotePitch {

NOTE_0(0, 0.5F),
NOTE_1(1, 0.53F),
NOTE_2(2, 0.56F),
NOTE_3(3, 0.6F),
NOTE_4(4, 0.63F),
NOTE_5(5, 0.67F),
NOTE_6(6, 0.7F),
NOTE_7(7, 0.76F),
NOTE_8(8, 0.8F),
NOTE_9(9, 0.84F),
NOTE_10(10, 0.9F),
NOTE_11(11, 0.94F),
NOTE_12(12, 1.0F),
NOTE_13(13, 1.06F),
NOTE_14(14, 1.12F),
NOTE_15(15, 1.18F),
NOTE_16(16, 1.26F),
NOTE_17(17, 1.34F),
NOTE_18(18, 1.42F),
NOTE_19(19, 1.5F),
NOTE_20(20, 1.6F),
NOTE_21(21, 1.68F),
NOTE_22(22, 1.78F),
NOTE_23(23, 1.88F),
NOTE_24(24, 2.0F);

public final int note;
public final float pitch;

NotePitch(int note, float pitch) {
this.note = note;
this.pitch = pitch;
}

public static float getPitch(int note) {
for (NotePitch notePitch : values()) {
if (notePitch.note == note) {
return notePitch.pitch;
}
}

return 0.0F;
}
}
Loading

0 comments on commit 6f326e1

Please sign in to comment.