Started map rendering

This commit is contained in:
Norbi Peti 2017-11-05 22:01:40 +01:00
parent 938fdc0ba0
commit 3d36ca8079
2 changed files with 37 additions and 159 deletions

View file

@ -0,0 +1,22 @@
package io.github.norbipeti.audiospectrum;
import org.bukkit.entity.Player;
import org.bukkit.map.MapCanvas;
import org.bukkit.map.MapRenderer;
import org.bukkit.map.MapView;
public class BarsRenderer extends MapRenderer
{
private int[] bars;
public BarsRenderer(int[] bars)
{
this.bars = bars;
}
@Override
public void render(MapView arg0, MapCanvas arg1, Player arg2)
{
}
}

View file

@ -4,82 +4,30 @@ 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.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Color; import org.bukkit.map.MapView;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.inventory.meta.ItemMeta.Spigot;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scoreboard.Score;
public class PluginMain extends JavaPlugin 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];
// Fired when plugin is first enabled // Fired when plugin is first enabled
@SuppressWarnings("deprecation")
@Override @Override
public void onEnable() public void onEnable()
{ {
System.out.println("Adding ArmorStands..."); BarsRenderer br = new BarsRenderer(bars);
Collection<ArmorStand> as = Bukkit.getWorlds().get(0) for (short i = 0; i < 4; i++)
.getEntitiesByClass(ArmorStand.class);
for (ArmorStand a : as)
{ {
Score score = Bukkit.getScoreboardManager().getMainScoreboard() MapView map = Bukkit.getMap(i);
.getObjective("BarIDX") if (map == null)
.getScore(a.getUniqueId().toString()); map = Bukkit.createMap(Bukkit.getWorlds().get(0));
if (!score.isScoreSet()) map.getRenderers().clear();
continue; map.addRenderer(br);
int x = score.getScore();
score = Bukkit.getScoreboardManager().getMainScoreboard()
.getObjective("BarIDY")
.getScore(a.getUniqueId().toString());
if (!score.isScoreSet())
continue;
int y = score.getScore();
ArmorStands.put(x + "-" + y, a);
} }
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
if (!ArmorStands.containsKey(i + "-" + j))
{
ArmorStand a = (ArmorStand) Bukkit.getWorlds().get(0)
.spawnEntity(new Location(Bukkit.getWorlds().get(0),
129, 4, -600), EntityType.ARMOR_STAND);
ItemStack is = new ItemStack(Material.LEATHER_CHESTPLATE);
LeatherArmorMeta lam = (LeatherArmorMeta) is.getItemMeta();
lam.setColor(Color.RED);
is.setItemMeta(lam);
a.setChestplate(is);
ArmorStands.put(i + "-" + j, a);
Score score = Bukkit.getScoreboardManager()
.getMainScoreboard().getObjective("BarIDX")
.getScore(a.getUniqueId().toString());
score.setScore(i);
score = Bukkit.getScoreboardManager().getMainScoreboard()
.getObjective("BarIDY")
.getScore(a.getUniqueId().toString());
score.setScore(j);
System.out.println("Created armor stand " + i + "-" + j);
}
}
}
System.out.println("Done!");
thread = new Thread() thread = new Thread()
{ {
public void run() public void run()
@ -98,10 +46,7 @@ public class PluginMain extends JavaPlugin
running = false; running = false;
} }
private volatile int moveup; private volatile byte[] packet = new byte[2];
private volatile byte[] packet;
private volatile boolean runningtask;
private volatile HashMap<String, ArmorStand> ArmorStands = new HashMap<String, ArmorStand>();
public void run(int port) public void run(int port)
{ {
@ -109,107 +54,18 @@ public class PluginMain extends JavaPlugin
try try
{ {
serverSocket = new DatagramSocket(port); serverSocket = new DatagramSocket(port);
byte[] receiveData = new byte[8];
System.out.printf("Listening on udp:%s:%d%n", System.out.printf("Listening on udp:%s:%d%n", InetAddress.getLocalHost().getHostAddress(), port);
InetAddress.getLocalHost().getHostAddress(), port); DatagramPacket receivePacket = new DatagramPacket(packet, packet.length);
DatagramPacket receivePacket = new DatagramPacket(receiveData,
receiveData.length);
while (running) while (running)
{ {
serverSocket.receive(receivePacket); serverSocket.receive(receivePacket);
packet = receivePacket.getData(); bars[Byte.toUnsignedInt(packet[0])] = Byte.toUnsignedInt(packet[1]);
moveup = 0;
Runnable runnable = new Runnable()
{
public void run()
{
while (true)
{
/*
* Block block =
* Bukkit.getWorlds().get(0).getBlockAt(
* 129 - packet[0], 56 + moveup, -600);
*/
ArmorStand as = ArmorStands.get(packet[0] + "-"
+ (Byte.toUnsignedInt(packet[1]) % 8));
/*
* System.out.println("packet[0]: " + packet[0]);
* //TODO: TMP
* System.out.println("packet[1]: " + packet[1]);
* //TODO: TMP
* System.out.println("get: " + packet[0] + "-"
* + (Byte.toUnsignedInt(packet[1]) % 8)); //TODO:
* TMP
* System.out.println("as: " + as); //TODO: TMP
*/
if (as == null)
break;
if (Byte.toUnsignedInt(packet[1]) - moveup * 8 > 8)
{
/*
* block.setType(Material.SNOW_BLOCK);
* block.getState().setData(new MaterialData(
* Material.SNOW_BLOCK, (byte) 0));
*/
System.out.println("A: " + new Location(
as.getWorld(), 129 - packet[0],
4 + moveup, -600));
as.teleport(new Location(as.getWorld(),
129 - packet[0], 4 + moveup, -600));
//TODO: Change armor color
} else
{
/*
* block.setType(Material.SNOW);
* block.setData(
* (byte) (Byte.toUnsignedInt(packet[1])
* - moveup * 8));
*/
System.out.println("B: " + new Location(
as.getWorld(), 129 - packet[0],
4 + moveup
+ (Byte.toUnsignedInt(packet[1])
- moveup * 8) * 0.1,
-600));
as.teleport(new Location(as.getWorld(),
129 - packet[0],
4 + moveup
+ (Byte.toUnsignedInt(packet[1])
- moveup * 8) * 0.1,
-600));
}
if (Byte.toUnsignedInt(packet[1]) - moveup * 8 > 8)
moveup += 1;
else
break;
}
/*
* for (int i = 56 + moveup + 1; i < 56 + 255 / 8; i++)
* {
* Block block = Bukkit.getWorlds().get(0)
* .getBlockAt(129 - packet[0], i, -600);
* block.setType(Material.AIR);
* }
*/
runningtask = false;
}
};
try
{
runningtask = true;
getServer().getScheduler().runTask(this, runnable);
while (runningtask)
;
} catch (Exception e)
{
}
} }
} catch (IOException e) } catch (IOException e)
{ {
System.out.println(e); e.printStackTrace();
} finally } finally
{ {
serverSocket.close(); serverSocket.close();