It ALL works!

The single map mode has best performance
Some latency, fix by only using Java
This commit is contained in:
Norbi Peti 2017-11-09 21:57:35 +01:00
parent f9832eb554
commit 29d91a744d
4 changed files with 97 additions and 13 deletions

View file

@ -2,3 +2,6 @@ name: AudioSpectrum
main: io.github.norbipeti.audiospectrum.PluginMain main: io.github.norbipeti.audiospectrum.PluginMain
version: 4.0 version: 4.0
commands: commands:
barcount:
usage: Please give a number between 0 and 16.
singlemap:

View file

@ -1,27 +1,55 @@
package io.github.norbipeti.audiospectrum; package io.github.norbipeti.audiospectrum;
import java.awt.Color;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.map.*; import org.bukkit.map.*;
public class BarsRenderer extends MapRenderer public class BarsRenderer extends BarsRendererBase
{ {
private int[] bars; private boolean single = false;
public BarsRenderer(int[] bars) public BarsRenderer(int[] bars)
{ {
this.bars = bars; super(bars);
} }
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@Override @Override
public void render(MapView mv, MapCanvas mc, Player pl) public void render(MapView mv, MapCanvas mc, Player pl)
{ //Width: 16, empty space: 16, count per map: 8 { //Width: 8, empty space: 8, count per map: 8
//if (firstrender ? !(firstrender = !firstrender) : firstrender)
if (firstrender < 4 ? firstrender++ < 4 : false) //Only increment if true
for (int i = 0; i < 128; i++)
for (int j = 0; j < 128; j++)
mc.setPixel(i, j, MapPalette.matchColor(Color.black));
if (single)
{
if (mv.getId() != 0)
return;
for (int i = 0; i < 16 && i < count; i++)
for (int j = 0; j < 128; j++)
for (int k = 0; k < 4; k++)
mc.setPixel(i * 8 + k, 128 - j, j < bars[i] / 2 ? MapPalette.matchColor(j, 255 - j * 2, 0)
: MapPalette.matchColor(Color.BLACK));
return;
}
int offsetx = mv.getId() % 2 * 8, offsety = mv.getId() < 2 ? -128 : 0; int offsetx = mv.getId() % 2 * 8, offsety = mv.getId() < 2 ? -128 : 0;
//System.out.println("OX: " + offsetx + " OY: " + offsety + " ID: " + mv.getId()); //System.out.println("OX: " + offsetx + " OY: " + offsety + " ID: " + mv.getId());
for (int i = 0; i < 8; i++) for (int i = 0; i < 8 && i < count - offsetx; i++)
for (int j = 0; j < 128; j++) for (int j = 0; j < 128; j++)
for (int k = 0; k < 16; k++) for (int k = 0; k < 8; k++)
mc.setPixel(i * 32 + k, 128 - j, j < bars[offsetx + i] + offsety mc.setPixel(i * 16 + k, 128 - j,
? MapPalette.matchColor(255 - j + offsety, j - offsety, 0) : 0); //TODO: 0 is transparent j < bars[offsetx + i] + offsety ? MapPalette.matchColor(j - offsety, 255 - j + offsety, 0)
} //TODO: Render areas inbetween black : MapPalette.matchColor(Color.BLACK));
}
/**
* Sets whether to use a single map or 4
*/
public boolean toggleSingle()
{
firstrender = 0;
return this.single = !single;
}
} }

View file

@ -0,0 +1,23 @@
package io.github.norbipeti.audiospectrum;
import org.bukkit.map.MapRenderer;
public abstract class BarsRendererBase extends MapRenderer
{
protected int[] bars;
protected byte firstrender = 0;
protected byte count = 16;
public BarsRendererBase(int[] bars)
{
this.bars = bars;
}
public byte setBarCount(byte count)
{
firstrender = 0;
if (count >= 0 && count <= 16)
this.count = count;
return this.count;
}
}

View file

@ -4,9 +4,9 @@ import java.io.IOException;
import java.net.DatagramPacket; import java.net.DatagramPacket;
import java.net.DatagramSocket; import java.net.DatagramSocket;
import java.net.InetAddress; import java.net.InetAddress;
import java.util.Arrays;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.map.MapView; import org.bukkit.map.MapView;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
@ -15,13 +15,14 @@ public class PluginMain extends JavaPlugin
private Thread thread; private Thread thread;
private boolean running = false; private boolean running = false;
private volatile int[] bars = new int[16]; private volatile int[] bars = new int[16];
private BarsRenderer br;
// Fired when plugin is first enabled // Fired when plugin is first enabled
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@Override @Override
public void onEnable() public void onEnable()
{ {
BarsRenderer br = new BarsRenderer(bars); br = new BarsRenderer(bars);
for (short i = 0; i < 4; i++) for (short i = 0; i < 4; i++)
{ {
MapView map = Bukkit.getMap(i); MapView map = Bukkit.getMap(i);
@ -32,6 +33,7 @@ public class PluginMain extends JavaPlugin
} }
thread = new Thread() thread = new Thread()
{ {
@Override
public void run() public void run()
{ {
PluginMain.this.run(5896); PluginMain.this.run(5896);
@ -48,7 +50,7 @@ public class PluginMain extends JavaPlugin
running = false; running = false;
} }
private volatile byte[] packet = new byte[2]; private volatile byte[] packet = new byte[16];
public void run(int port) public void run(int port)
{ {
@ -76,4 +78,32 @@ public class PluginMain extends JavaPlugin
serverSocket.close(); serverSocket.close();
} }
} }
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
{
if (command.getName().equalsIgnoreCase("barcount"))
{
if (args.length < 1)
return false;
try
{
byte c = Byte.parseByte(args[0]);
sender.sendMessage("Bar count set to " + br.setBarCount(c));
return true;
} catch (Exception e)
{
return false;
}
} else if (command.getName().equalsIgnoreCase("singlemap"))
{
sender.sendMessage("Single map toggled, now " + br.toggleSingle());
return true;
} else
{
sender.sendMessage("Command not implemented!");
return true;
}
}
} }