Skip to content

Commit

Permalink
format and cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
khjxiaogu committed Aug 7, 2020
1 parent 2abf7ae commit 88f8956
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 190 deletions.
73 changes: 37 additions & 36 deletions src/main/java/com/khjxiaogu/MCMidi/MCMidi.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import org.bukkit.configuration.serialization.ConfigurationSerialization;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.Vector;

import com.khjxiaogu.MCMidi.Midi.NoteBlockPlayers;
import com.khjxiaogu.MCMidi.Midi.NoteInfo;
import com.khjxiaogu.MCMidi.Midi.NotePlayers;
Expand Down Expand Up @@ -86,13 +84,13 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
e.printStackTrace();
return false;
}
}else if (args[0].equals("combine")) { //$NON-NLS-1$
} else if (args[0].equals("combine")) { //$NON-NLS-1$
MidiSheet mp = loaded.get(args[1]);
if (mp == null) {
sender.sendMessage(Messages.getString("MCMidi.invalid_midi")); //$NON-NLS-1$
return false;
}
if(mp.Combine())
if (mp.Combine())
sender.sendMessage(Messages.getString("MCMidi.combined"));
else
sender.sendMessage(Messages.getString("MCMidi.already_combined"));
Expand Down Expand Up @@ -129,15 +127,16 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
} else if (args[0].equals("loopblock")) { //$NON-NLS-1$
World w;
if (sender instanceof Player) {
w= ((Player) sender).getWorld();
} else if(sender instanceof BlockCommandSender){
w=(( BlockCommandSender)sender).getBlock().getWorld();
}else {
w = ((Player) sender).getWorld();
} else if (sender instanceof BlockCommandSender) {
w = ((BlockCommandSender) sender).getBlock().getWorld();

} else {
sender.sendMessage(Messages.getString("MCMidi.must_be_player"));//$NON-NLS-1$
return false;
}
Location l=new Location(w,Integer.parseInt(args[2]),Integer.parseInt(args[3]),Integer.parseInt(args[4]));
Location l = new Location(w, Integer.parseInt(args[2]), Integer.parseInt(args[3]),
Integer.parseInt(args[4]));
NoteBlockPlayers np = nbs.get(l);
if (np != null) {
np.cancel();
Expand All @@ -147,24 +146,25 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
sender.sendMessage(Messages.getString("MCMidi.invalid_midi")); //$NON-NLS-1$
return false;
}
Block ob=l.getBlock();
if(ob==null||ob.getType()!=Material.NOTE_BLOCK)
Block ob = l.getBlock();
if (ob == null || ob.getType() != Material.NOTE_BLOCK)
return false;
nbs.put(l, mp.playBlock((NoteBlock)ob.getState(),true));
nbs.put(l, mp.playBlock((NoteBlock) ob.getState(), true));
sender.sendMessage(Messages.getString("MCMidi.play_start")); //$NON-NLS-1$
return true;
} else if (args[0].equals("playblock")) { //$NON-NLS-1$
World w;
if (sender instanceof Player) {
w= ((Player) sender).getWorld();
} else if(sender instanceof BlockCommandSender){
w=(( BlockCommandSender)sender).getBlock().getWorld();
}else {
w = ((Player) sender).getWorld();
} else if (sender instanceof BlockCommandSender) {
w = ((BlockCommandSender) sender).getBlock().getWorld();

} else {
sender.sendMessage(Messages.getString("MCMidi.must_be_player"));//$NON-NLS-1$
return false;
}
Location l=new Location(w,Integer.parseInt(args[2]),Integer.parseInt(args[3]),Integer.parseInt(args[4]));
Location l = new Location(w, Integer.parseInt(args[2]), Integer.parseInt(args[3]),
Integer.parseInt(args[4]));
NoteBlockPlayers np = nbs.get(l);
if (np != null) {
np.cancel();
Expand All @@ -174,13 +174,13 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
sender.sendMessage(Messages.getString("MCMidi.invalid_midi")); //$NON-NLS-1$
return false;
}
Block ob=l.getBlock();
if(ob==null||ob.getType()!=Material.NOTE_BLOCK)
Block ob = l.getBlock();
if (ob == null || ob.getType() != Material.NOTE_BLOCK)
return false;
nbs.put(l, mp.playBlock((NoteBlock)ob.getState(),false));
nbs.put(l, mp.playBlock((NoteBlock) ob.getState(), false));
sender.sendMessage(Messages.getString("MCMidi.play_start")); //$NON-NLS-1$
return true;
}else if (args[0].equals("loop")) { //$NON-NLS-1$
} else if (args[0].equals("loop")) { //$NON-NLS-1$
Player p;
if (args.length >= 3) {
p = Bukkit.getPlayer(args[2]);
Expand Down Expand Up @@ -222,20 +222,20 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
sender.sendMessage(Messages.getString("MCMidi.must_be_player"));//$NON-NLS-1$
return false;
}
Player p=(Player) sender;
int width=24;
Material type=Material.STONE;
if(args.length>=3)
type=Material.getMaterial(Integer.valueOf(args[2]));
if(args.length>=4)
width=Integer.parseInt(args[3]);
if (!MidiAPI.generateStucture(args[1],p.getLocation(), width,type)) {
Player p = (Player) sender;
int width = 24;
Material type = Material.STONE;
if (args.length >= 3)
type = Material.getMaterial(Integer.valueOf(args[2]));
if (args.length >= 4)
width = Integer.parseInt(args[3]);
if (!MidiAPI.generateStucture(args[1], p.getLocation(), width, type)) {
sender.sendMessage(Messages.getString("MCMidi.invalid_midi")); //$NON-NLS-1$
return false;
}
sender.sendMessage(Messages.getString("MCMidi.generate_finish"));//$NON-NLS-1$
return true;
}
}
}
if (args.length >= 1) {
if (args[0].equals("stop")) { //$NON-NLS-1$
Expand Down Expand Up @@ -293,7 +293,8 @@ public List<String> onTabComplete(CommandSender sender, Command command, String
list.addAll(Arrays.asList(getDataFolder().list((d, n) -> {
return !n.endsWith(".yml");//$NON-NLS-1$
})));
} else if (args[0].equals("play") || args[0].equals("loop") || args[0].equals("generate") || args[0].equals("loopblock") || args[0].equals("playblock")) {//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
} else if (args[0].equals("play") || args[0].equals("loop") || args[0].equals("generate") //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|| args[0].equals("loopblock") || args[0].equals("playblock")) {
list.addAll(loaded.keySet());
} else if (args[0].equals("stop"))//$NON-NLS-1$
return null;
Expand Down Expand Up @@ -325,7 +326,7 @@ public void onLoad() {
ConfigurationSerialization.registerClass(NoteInfo.class);
ConfigurationSerialization.registerClass(NoteTrack.class);
ConfigurationSerialization.registerClass(MidiSheet.class);

}

@Override
Expand All @@ -341,7 +342,7 @@ public void onEnable() {
for (String s : cs.getKeys(false)) {
try {
ConfigurationSection cur = cs.getConfigurationSection(s);
getLogger().info("loading"+cur.getString("name"));
getLogger().info("loading" + cur.getString("name"));
loaded.put(cur.getString("name"), (MidiSheet) cur.get("midi"));//$NON-NLS-1$ //$NON-NLS-2$
} catch (Throwable t) {
getLogger().info("midi " + s + " load failure");//$NON-NLS-1$ //$NON-NLS-2$
Expand All @@ -361,7 +362,7 @@ public void onDisable() {
MCMidi.plugin = this;
ConfigurationSection cs = midifile.createSection("midi");//$NON-NLS-1$
int i = 0;
for(Map.Entry<String,MidiSheet> entry:loaded.entrySet()) {
for (Map.Entry<String, MidiSheet> entry : loaded.entrySet()) {
ConfigurationSection cur = cs.createSection(Integer.toString(i));
cur.set("name", entry.getKey());//$NON-NLS-1$
cur.set("midi", entry.getValue());//$NON-NLS-1$
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/khjxiaogu/MCMidi/Midi/NoteBlockPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@

public class NoteBlockPlayer extends TrackPlayer {
NoteBlock nb;

public NoteBlockPlayer(NoteBlock b, NoteTrack nc) {
super(null, nc);
nb=b;
nb = b;
}

@Override
public void play(NoteInfo note) {
note.play(nb);
}

@Override
public boolean canPlay() {
return nb.isPlaced();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.List;

import org.bukkit.block.NoteBlock;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;

import com.khjxiaogu.MCMidi.MCMidi;
Expand Down
85 changes: 50 additions & 35 deletions src/main/java/com/khjxiaogu/MCMidi/Midi/NoteInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,8 @@ interface Initializer {
private final int key;

public static void initNotes() {
if (!MCMidi.plugin.getConfig().getBoolean("universal", false)) {
for(int i=24;i>=10;i--) {
NoteInfo.inss[i] = Instrument.BASS_DRUM;
}

NoteInfo.inss[9] = Instrument.BASS_DRUM;
NoteInfo.inss[8] = Instrument.BASS_DRUM;
NoteInfo.inss[7] = Instrument.BASS_DRUM;
NoteInfo.inss[6] = Instrument.STICKS;
NoteInfo.inss[5] = Instrument.PIANO;
NoteInfo.inss[4] = Instrument.SNARE_DRUM;
NoteInfo.inss[3] = Instrument.BASS_GUITAR;
NoteInfo.inss[2] = Instrument.BASS_GUITAR;
NoteInfo.inss[1] = Instrument.BASS_GUITAR;
NoteInfo.inss[0] = Instrument.BASS_GUITAR;
NoteInfo.init = (t, k) -> {
t.n = NoteInfo.notes[k % 12+6];
t.ins = NoteInfo.inss[k / 12];
};
} else {
for(int i=24;i>=10;i--) {
if (MCMidi.plugin.getConfig().getBoolean("universal", false)) {
for (int i = 24; i >= 10; i--) {
NoteInfo.inss[i] = Instrument.SNARE_DRUM;
}
NoteInfo.inss[9] = Instrument.SNARE_DRUM;
Expand All @@ -71,10 +52,29 @@ public static void initNotes() {
NoteInfo.inss[1] = Instrument.BASS_DRUM;
NoteInfo.inss[0] = Instrument.BASS_DRUM;
NoteInfo.init = (t, k) -> {
k+=6;
k += 6;
t.n = NoteInfo.notes[k % 24];
t.ins = NoteInfo.inss[k / 12];
};
} else {
for (int i = 24; i >= 10; i--) {
NoteInfo.inss[i] = Instrument.BASS_DRUM;
}

NoteInfo.inss[9] = Instrument.BASS_DRUM;
NoteInfo.inss[8] = Instrument.BASS_DRUM;
NoteInfo.inss[7] = Instrument.BASS_DRUM;
NoteInfo.inss[6] = Instrument.STICKS;
NoteInfo.inss[5] = Instrument.PIANO;
NoteInfo.inss[4] = Instrument.SNARE_DRUM;
NoteInfo.inss[3] = Instrument.BASS_GUITAR;
NoteInfo.inss[2] = Instrument.BASS_GUITAR;
NoteInfo.inss[1] = Instrument.BASS_GUITAR;
NoteInfo.inss[0] = Instrument.BASS_GUITAR;
NoteInfo.init = (t, k) -> {
t.n = NoteInfo.notes[k % 12 + 6];
t.ins = NoteInfo.inss[k / 12];
};
}
NoteInfo.notes[0] = Note.sharp(0, Tone.F);
NoteInfo.notes[1] = Note.natural(0, Tone.G);
Expand Down Expand Up @@ -129,11 +129,13 @@ public void play(Player p) {
p.playNote(p.getLocation(), ins, n);
}
}

public void play(NoteBlock b) {
if (n != null) {
b.play(ins, n);
}
}

@Override
public Map<String, Object> serialize() {
Map<String, Object> map = new HashMap<>();
Expand All @@ -142,22 +144,35 @@ public Map<String, Object> serialize() {
map.put("v", volume);
return map;
}
public void placeBlock(Location l,Material base) {
Block b=l.getWorld().getBlockAt(l);
Block under=l.getWorld().getBlockAt(l.getBlockX(),l.getBlockY()-1,l.getBlockZ());
l.getWorld().getBlockAt(l.getBlockX(),l.getBlockY()-2,l.getBlockZ()).setType(base);
switch(ins) {
case BASS_DRUM:under.setType(Material.STONE);break;
case STICKS:under.setType(Material.GLASS);break;
case PIANO:under.setType(Material.IRON_BLOCK);break;
case SNARE_DRUM:under.setType(Material.SAND);break;
case BASS_GUITAR:under.setType(Material.WOOD);break;
default:under.setType(Material.GOLD_BLOCK);break;

public void placeBlock(Location l, Material base) {
Block b = l.getWorld().getBlockAt(l);
Block under = l.getWorld().getBlockAt(l.getBlockX(), l.getBlockY() - 1, l.getBlockZ());
l.getWorld().getBlockAt(l.getBlockX(), l.getBlockY() - 2, l.getBlockZ()).setType(base);
switch (ins) {
case BASS_DRUM:
under.setType(Material.STONE);
break;
case STICKS:
under.setType(Material.GLASS);
break;
case PIANO:
under.setType(Material.IRON_BLOCK);
break;
case SNARE_DRUM:
under.setType(Material.SAND);
break;
case BASS_GUITAR:
under.setType(Material.WOOD);
break;
default:
under.setType(Material.GOLD_BLOCK);
break;
}
b.setType(Material.NOTE_BLOCK);
NoteBlock nb=(NoteBlock) b.getState();
NoteBlock nb = (NoteBlock) b.getState();
nb.setNote(n);
nb.update();
//b.setData(n.getId());
// b.setData(n.getId());
}
}
Loading

0 comments on commit 88f8956

Please sign in to comment.