Moved from snow layers to entities

This commit is contained in:
Norbi Peti 2016-07-08 22:27:25 +02:00
parent 2ea3bf1866
commit 2da7a3182f
2 changed files with 222 additions and 138 deletions

View file

@ -1,4 +1,4 @@
name: AudioSpectrum name: AudioSpectrum
main: tk.sznp.audiospectrum.PluginMain main: tk.sznp.audiospectrum.PluginMain
version: 1.0 version: 1.0
commands: commands:

View file

@ -1,134 +1,218 @@
package tk.sznp.audiospectrum; package tk.sznp.audiospectrum;
import java.io.IOException; 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 org.bukkit.Bukkit; import java.util.HashMap;
import org.bukkit.Material; import java.util.List;
import org.bukkit.block.Block; import java.util.Map;
import org.bukkit.entity.Player; import java.util.Set;
import org.bukkit.material.MaterialData;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.Bukkit;
import org.bukkit.Color;
public class PluginMain extends JavaPlugin { import org.bukkit.Location;
private Thread thread; import org.bukkit.Material;
private boolean running = false; import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.ArmorStand;
// Fired when plugin is first enabled import org.bukkit.entity.EntityType;
@Override import org.bukkit.inventory.ItemFlag;
public void onEnable() { import org.bukkit.inventory.ItemStack;
thread = new Thread() { import org.bukkit.inventory.meta.LeatherArmorMeta;
// Runnable runnable=new Runnable() { import org.bukkit.inventory.meta.ItemMeta.Spigot;
public void run() { import org.bukkit.plugin.java.JavaPlugin;
PluginMain.this.run(5896); import org.bukkit.scoreboard.Score;
}
}; public class PluginMain extends JavaPlugin
running = true; {
thread.start(); private Thread thread;
// runnable.run(); private boolean running = false;
}
// Fired when plugin is first enabled
// Fired when plugin is disabled @Override
@Override public void onEnable()
public void onDisable() { {
running = false; System.out.println("Adding ArmorStands...");
} Collection<ArmorStand> as = Bukkit.getWorlds().get(0)
.getEntitiesByClass(ArmorStand.class);
private volatile Player player; for (ArmorStand a : as)
private volatile int moveup; {
private volatile byte[] packet; Score score = Bukkit.getScoreboardManager().getMainScoreboard()
private volatile boolean runningtask; .getObjective("BarIDX")
.getScore(a.getUniqueId().toString());
public void run(int port) { if (!score.isScoreSet())
DatagramSocket serverSocket = null; continue;
try { int x = score.getScore();
serverSocket = new DatagramSocket(port); score = Bukkit.getScoreboardManager().getMainScoreboard()
byte[] receiveData = new byte[8]; .getObjective("BarIDY")
.getScore(a.getUniqueId().toString());
System.out.printf("Listening on udp:%s:%d%n", InetAddress if (!score.isScoreSet())
.getLocalHost().getHostAddress(), port); continue;
DatagramPacket receivePacket = new DatagramPacket(receiveData, int y = score.getScore();
receiveData.length); ArmorStands.put(x + "-" + y, a);
}
while (running) { for (int i = 0; i < 8; i++)
serverSocket.receive(receivePacket); {
packet = receivePacket.getData(); for (int j = 0; j < 8; j++)
player = null; {
try { if (!ArmorStands.containsKey(i + "-" + j))
player = Bukkit.getPlayer("NorbiPeti"); {
} catch (Exception e) { ArmorStand a = (ArmorStand) Bukkit.getWorlds().get(0)
} .spawnEntity(new Location(Bukkit.getWorlds().get(0),
/* 129, 4, -600), EntityType.ARMOR_STAND);
* getServer().dispatchCommand( getServer().getConsoleSender(), ItemStack is = new ItemStack(Material.LEATHER_CHESTPLATE);
* String.format("scoreboard players set t%d tracks %d", LeatherArmorMeta lam = (LeatherArmorMeta) is.getItemMeta();
* packet[0], Byte.toUnsignedInt(packet[1]))); //TODO: Teljesen lam.setColor(Color.RED);
* szerveroldali legyen is.setItemMeta(lam);
*/ a.setChestplate(is);
if (player == null) ArmorStands.put(i + "-" + j, a);
continue; Score score = Bukkit.getScoreboardManager()
.getMainScoreboard().getObjective("BarIDX")
moveup = 0; .getScore(a.getUniqueId().toString());
// System.out.println("Packet[1]: " + packet[1]); score.setScore(i);
Runnable runnable = new Runnable() { score = Bukkit.getScoreboardManager().getMainScoreboard()
public void run() { .getObjective("BarIDY")
while (true) { .getScore(a.getUniqueId().toString());
if (player == null) { score.setScore(j);
System.out.println("Player is null"); System.out.println("Created armor stand " + i + "-" + j);
return; }
} }
Block block = player.getWorld().getBlockAt( }
// 129 - packet[0], 56 + moveup, -654); System.out.println("Done!");
129 - packet[0], 56 + moveup, -600); thread = new Thread()
if (Byte.toUnsignedInt(packet[1]) - moveup * 8 > 8) { {
block.setType(Material.SNOW_BLOCK); public void run()
block.getState().setData( {
new MaterialData(Material.SNOW_BLOCK, PluginMain.this.run(5896);
(byte) 0)); }
} else { };
block.setType(Material.SNOW); running = true;
/* thread.start();
* MaterialData data = new MaterialData( }
* Material.SNOW); data.setData((byte) (Byte
* .toUnsignedInt(packet[1]) - moveup * 8)); // Fired when plugin is disabled
*/ @Override
/* public void onDisable()
* player.sendMessage("Data: " + (byte) {
* (Byte.toUnsignedInt(packet[1]) - moveup * running = false;
* 8)); }
*/
// block.getState().setData(data); private volatile int moveup;
// block.getState().update(); private volatile byte[] packet;
block.setData((byte) (Byte private volatile boolean runningtask;
.toUnsignedInt(packet[1]) - moveup * 8)); private volatile HashMap<String, ArmorStand> ArmorStands = new HashMap<String, ArmorStand>();
}
if (Byte.toUnsignedInt(packet[1]) - moveup * 8 > 8) public void run(int port)
moveup += 1; {
else DatagramSocket serverSocket = null;
break; try
} {
for (int i = 56 + moveup + 1; i < 56 + 255 / 8; i++) { serverSocket = new DatagramSocket(port);
Block block = player.getWorld().getBlockAt( byte[] receiveData = new byte[8];
// 129 - packet[0], i, -654);
129 - packet[0], i, -600); System.out.printf("Listening on udp:%s:%d%n",
block.setType(Material.AIR); InetAddress.getLocalHost().getHostAddress(), port);
} DatagramPacket receivePacket = new DatagramPacket(receiveData,
runningtask = false; receiveData.length);
}
}; while (running)
try { {
runningtask = true; serverSocket.receive(receivePacket);
getServer().getScheduler().runTask(this, runnable); packet = receivePacket.getData();
while (runningtask)
; moveup = 0;
} catch (Exception e) { Runnable runnable = new Runnable()
} {
} public void run()
} catch (IOException e) { {
System.out.println(e); while (true)
} finally { {
serverSocket.close(); /*
} * 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)
{
System.out.println(e);
} finally
{
serverSocket.close();
}
}
}