Path: blob/main/samples/plugin/CommandVideoMap.java
8641 views
package plugin;12import java.io.BufferedReader;3import java.io.File;4import java.io.FileReader;5import java.io.IOException;6import java.util.ArrayList;7import java.util.List;89import org.bukkit.ChatColor;10import org.bukkit.Location;11import org.bukkit.command.Command;12import org.bukkit.command.CommandExecutor;13import org.bukkit.command.CommandSender;14import org.bukkit.entity.Player;1516import ayunami2000.VideoMapPacketCodecBukkit;1718public class CommandVideoMap implements CommandExecutor {1920private VideoMapPacketCodecBukkit currentCodecInstance = null;2122@Override23public boolean onCommand(CommandSender arg0, Command arg1, String arg2, String[] arg3) {24if(!(arg0 instanceof Player)) {25arg0.sendMessage(ChatColor.RED + "Internal Error: " + ChatColor.WHITE + "CommmandSender must be a Player");26return false;27}28if(arg3.length == 3 && arg3[0].equalsIgnoreCase("begin")) {29try {30List<int[]> mapRows = new ArrayList();31double x = 0.0d;32double y = 0.0d;33double z = 0.0d;34float volume = -1.0f;35BufferedReader r = new BufferedReader(new FileReader(new File("video_map_config.txt")));36String str;37while((str = r.readLine()) != null) {38str = str.trim();39if(str.startsWith("#")) {40continue;41}else if(str.startsWith("x")) {42int i = str.indexOf('=');43if(i > 0) {44x = Double.parseDouble(str.substring(i + 1).trim());45}46}else if(str.startsWith("y")) {47int i = str.indexOf('=');48if(i > 0) {49y = Double.parseDouble(str.substring(i + 1).trim());50}51}else if(str.startsWith("z")) {52int i = str.indexOf('=');53if(i > 0) {54z = Double.parseDouble(str.substring(i + 1).trim());55}56}else if(str.startsWith("volume")) {57int i = str.indexOf('=');58if(i > 0) {59volume = Float.parseFloat(str.substring(i + 1).trim());60}61}else {62try {63String[] digits = str.split(",");64int firstInt = Integer.parseInt(digits[0].trim());65int[] newRow = new int[digits.length];66newRow[0] = firstInt;67for(int i = 1; i < digits.length; ++i) {68newRow[i] = Integer.parseInt(digits[i].trim());69}70if(mapRows.size() > 0 && mapRows.get(0).length != newRow.length) {71throw new IOException("All rows in map list must be the same length (" + mapRows.get(0).length + " != " + newRow.length + ")");72}73mapRows.add(newRow);74}catch(NumberFormatException t) {75}76}77}78r.close();79if(mapRows.size() > 0) {80if(currentCodecInstance != null) {81currentCodecInstance.disableVideoBukkit().send((Player)arg0);82currentCodecInstance = null;83}84int[][] matrix = new int[mapRows.size()][mapRows.get(0).length];85for(int i = 0, l = mapRows.size(); i < l; ++i) {86for(int j = 0; j < matrix[i].length; ++j) {87matrix[i][j] = mapRows.get(i)[j];88}89}90currentCodecInstance = new VideoMapPacketCodecBukkit(matrix, x, y, z, volume);91currentCodecInstance.beginPlaybackBukkit(arg3[1], true, arg3[2].indexOf('.') > 0 ? Float.parseFloat(arg3[2]) : Float.parseFloat(arg3[2] + ".0")).send((Player)arg0);92arg0.sendMessage(ChatColor.GREEN + "Enabled video map, URL:" + ChatColor.WHITE + " " + arg3[1]);93return true;94}else {95throw new IOException("No map rows were defined");96}97}catch(IOException ex) {98arg0.sendMessage(ChatColor.RED + "Internal Error while reading \'video_map_config.txt\': " + ChatColor.WHITE + ex.toString());99}100}else if((arg3.length == 2 || arg3.length == 3) && arg3[0].equalsIgnoreCase("preload")) {101int ttl = arg3.length == 3 ? Integer.parseInt(arg3[2]) * 1000 : 180 * 1000;102VideoMapPacketCodecBukkit.bufferVideoBukkit(arg3[1], ttl).send((Player)arg0);103arg0.sendMessage(ChatColor.GREEN + "Buffered video URL:" + ChatColor.WHITE + " " + arg3[1] + " " + ChatColor.GREEN + "for " + ChatColor.WHITE + (ttl / 1000) + ChatColor.GREEN + " seconds");104return true;105}else {106if(arg3.length == 1 && arg3[0].equalsIgnoreCase("stop")) {107if(currentCodecInstance != null) {108currentCodecInstance.disableVideoBukkit().send((Player)arg0);109currentCodecInstance = null;110arg0.sendMessage(ChatColor.GREEN + "Disabled video map");111return true;112}else {113arg0.sendMessage(ChatColor.RED + "Error: " + ChatColor.WHITE + "no video is loaded");114}115}else if(arg3.length == 1 && arg3[0].equalsIgnoreCase("pause")) {116if(currentCodecInstance != null) {117currentCodecInstance.setPausedBukkit(true).send((Player)arg0);118arg0.sendMessage(ChatColor.GREEN + "Paused video map");119return true;120}else {121arg0.sendMessage(ChatColor.RED + "Error: " + ChatColor.WHITE + "no video is loaded");122}123}else if(arg3.length == 1 && arg3[0].equalsIgnoreCase("resume")) {124if(currentCodecInstance != null) {125currentCodecInstance.setPausedBukkit(false).send((Player)arg0);126arg0.sendMessage(ChatColor.GREEN + "Resumed video map");127return true;128}else {129arg0.sendMessage(ChatColor.RED + "Error: " + ChatColor.WHITE + "no video is loaded");130}131}else if((arg3.length == 1 || arg3.length == 2) && arg3[0].equalsIgnoreCase("loop")) {132if(currentCodecInstance != null) {133boolean gottaLoop = arg3.length == 1 || arg3[1].equalsIgnoreCase("true");134currentCodecInstance.setLoopEnableBukkit(gottaLoop).send((Player)arg0);135arg0.sendMessage(ChatColor.GREEN + (gottaLoop ? "Enabled video map loop" : "Disabled video map loop"));136return true;137}else {138arg0.sendMessage(ChatColor.RED + "Error: " + ChatColor.WHITE + "no video is loaded");139}140}else if(arg3.length == 1 && arg3[0].equalsIgnoreCase("move")) {141if(currentCodecInstance != null) {142Location l = ((Player)arg0).getLocation();143currentCodecInstance.moveAudioSourceBukkit(l.getX(), l.getY(), l.getZ(), currentCodecInstance.getVolume()).send((Player)arg0);144arg0.sendMessage(ChatColor.GREEN + "Repositioned audio source to " + l.getBlockX() + ", " + l.getBlockY() + ", " + l.getBlockZ());145return true;146}else {147arg0.sendMessage(ChatColor.RED + "Error: " + ChatColor.WHITE + "no video is loaded");148}149}150}151return false;152}153154}155156157