/[projects]/miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/TemplateCommand.java
ViewVC logotype

Contents of /miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/TemplateCommand.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1740 - (show annotations) (download)
Sun Mar 18 10:38:54 2012 UTC (12 years, 2 months ago) by torben
File size: 4237 byte(s)
ignore empty lines

1 package dk.thoerup.bukkit.hoeruputils;
2
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.FileInputStream;
6 import java.io.IOException;
7 import java.io.InputStreamReader;
8
9 import org.bukkit.ChatColor;
10 import org.bukkit.Location;
11 import org.bukkit.World;
12 import org.bukkit.command.Command;
13 import org.bukkit.command.CommandExecutor;
14 import org.bukkit.command.CommandSender;
15 import org.bukkit.entity.Player;
16 import org.bukkit.plugin.Plugin;
17
18 public class TemplateCommand implements CommandExecutor {
19
20 /*class Pair {
21 public int id;
22 public int subId;
23
24 public Pair (int i, int s) {
25 id = i;
26 subId = s;
27 }
28
29 }*/
30
31 Plugin plugin;
32
33 public TemplateCommand(Plugin plugin) {
34 this.plugin = plugin;
35 }
36
37 @Override
38 public boolean onCommand(final CommandSender sender, Command command, String label, String[] args) {
39 if (! (sender instanceof Player) ) {
40 sender.sendMessage( "this command can not be run from console" );
41 return true;
42 }
43
44 Player player = (Player) sender;
45
46 if (! sender.isOp() ) {
47 sender.sendMessage( "you need to be op to run this command" );
48 return true;
49 }
50
51 if ( args.length != 1) {
52 sender.sendMessage( ChatColor.YELLOW + "Usage: /template <template-name>");
53 return true;
54 }
55
56 File templateFile = new File( plugin.getDataFolder(), "template_" + args[0] + ".txt");
57 if (! templateFile.exists() ) {
58 sender.sendMessage( ChatColor.YELLOW + "Template not found" );
59 return true;
60 }
61
62 int[][][] template = parseFile(templateFile);
63 if( template == null) {
64 sender.sendMessage( ChatColor.YELLOW + "Invalid template file" );
65 return true;
66 }
67
68 buildTemplate( player.getLocation(), template);
69
70 return false;
71 }
72
73
74
75 // we use y /ver
76
77 int[][][] parseFile(File templateFile) {
78
79
80 try {
81 int lines = 0;
82
83 FileInputStream fis = new FileInputStream( templateFile );
84 BufferedReader in = new BufferedReader( new InputStreamReader(fis) );
85 String line = in.readLine();
86 lines ++;
87
88 if ( line.charAt(0) != '#') {
89 plugin.getLogger().info( "Template: invalid start character" );
90 return null;
91 }
92 line = line.replace('#', ' ').trim();
93 String dimensions[] = line.split(",");
94
95 if ( dimensions.length != 3) {
96 plugin.getLogger().info( "Template: invalid dimensions line" );
97 return null;
98 }
99 int xsize = Integer.parseInt(dimensions[0]);
100 int ysize = Integer.parseInt(dimensions[1]);
101 int zsize = Integer.parseInt(dimensions[2]);
102
103 int[][][] array = new int[xsize][ysize][zsize];
104
105 int x = 0;
106 int y = 0;
107
108
109 while ( (line = in.readLine() ) != null ) {
110 lines++;
111
112 line = line.trim();
113 if (line.length() == 0)
114 continue;
115
116 if (line.charAt(0) == '-')
117 continue;
118
119 String elements[] = line.split(",");
120 if (elements.length != zsize) {
121 plugin.getLogger().info( "Template: invalid field count on line " + lines + ". Found " + elements.length + " but expected " + zsize );
122 return null;
123 }
124
125 for (int i=0; i<elements.length; i++) {
126 try {
127 int val = Integer.parseInt( elements[i] );
128
129 array[x][y][i] = val;
130
131 } catch (Exception e) {
132 plugin.getLogger().info( "Template: invalid value on line " + lines + ": " + elements[i] );
133 return null;
134 }
135 }
136
137 x++;
138 if (x >= xsize) {
139 x = 0;
140 y++;
141 }
142 }
143
144 if (x != 0) {
145 plugin.getLogger().info( "Template: not enough lines to complete the last level" );
146 return null;
147 }
148
149 if (y != ysize) {
150 plugin.getLogger().info( "Template: not enough levels");
151 return null;
152 }
153
154
155 return array;
156 }
157 catch (IOException e) {
158 plugin.getLogger().info( "Template: Error parsing " + templateFile.getName() + ":" + e.getMessage() );
159 return null;
160 }
161 }
162
163 void buildTemplate(Location loc, int[][][] template) {
164 World world = loc.getWorld();
165 for (int i=0; i<template.length; i++ ) {
166 for (int j=0; j<template[0].length; j++) {
167 for (int k=0; k<template[0][0].length; k++) {
168 int x = loc.getBlockX() + i + 1;
169 int y = loc.getBlockY() + j;
170 int z = loc.getBlockZ() + k;
171
172 world.getBlockAt(x, y, z).setTypeId( template[x][y][z] );
173 }
174 }
175 }
176
177 }
178
179 }

  ViewVC Help
Powered by ViewVC 1.1.20