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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1747 - (hide annotations) (download)
Sun Mar 18 11:38:57 2012 UTC (12 years, 2 months ago) by torben
File size: 4638 byte(s)
also parse subid/data fields
1 torben 1738 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.InputStreamReader;
7    
8     import org.bukkit.ChatColor;
9     import org.bukkit.Location;
10     import org.bukkit.World;
11     import org.bukkit.command.Command;
12     import org.bukkit.command.CommandExecutor;
13     import org.bukkit.command.CommandSender;
14     import org.bukkit.entity.Player;
15     import org.bukkit.plugin.Plugin;
16    
17     public class TemplateCommand implements CommandExecutor {
18    
19 torben 1746
20 torben 1747 class Pair {
21 torben 1738 public int id;
22     public int subId;
23 torben 1746
24 torben 1738 public Pair (int i, int s) {
25     id = i;
26     subId = s;
27     }
28 torben 1746
29 torben 1747 }
30 torben 1746
31 torben 1738 Plugin plugin;
32 torben 1746
33 torben 1738 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 torben 1746
44 torben 1738 Player player = (Player) sender;
45 torben 1746
46 torben 1738 if (! sender.isOp() ) {
47     sender.sendMessage( "you need to be op to run this command" );
48     return true;
49     }
50 torben 1746
51 torben 1738 if ( args.length != 1) {
52     sender.sendMessage( ChatColor.YELLOW + "Usage: /template <template-name>");
53     return true;
54     }
55 torben 1746
56 torben 1738 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 torben 1746
62 torben 1747 Pair[][][] template;
63 torben 1746
64     try {
65     template = parseFile(templateFile);
66     if( template == null) {
67     sender.sendMessage( ChatColor.YELLOW + "Invalid template file" );
68     return true;
69     }
70     } catch (Exception e) {
71     plugin.getLogger().info( e.getMessage() );
72     sender.sendMessage( ChatColor.YELLOW + e.getMessage() );
73     return true;
74 torben 1738 }
75 torben 1746
76 torben 1738 buildTemplate( player.getLocation(), template);
77    
78     return false;
79     }
80    
81 torben 1746
82    
83    
84 torben 1747 Pair[][][] parseFile(File templateFile) throws Exception {
85 torben 1746
86    
87     int lines = 0;
88    
89     FileInputStream fis = new FileInputStream( templateFile );
90     BufferedReader in = new BufferedReader( new InputStreamReader(fis) );
91     String line = in.readLine();
92     lines ++;
93    
94     if ( line.charAt(0) != '#') {
95     throw new Exception( "Template: invalid start character" );
96     }
97 torben 1738
98 torben 1746 line = line.replace('#', ' ').trim();
99     String dimensions[] = line.split(",");
100    
101     if ( dimensions.length != 3) {
102     throw new Exception( "Template: invalid dimensions line" );
103     }
104 torben 1738
105 torben 1746 int xsize = Integer.parseInt(dimensions[0]);
106     int ysize = Integer.parseInt(dimensions[1]);
107     int zsize = Integer.parseInt(dimensions[2]);
108    
109 torben 1747 Pair[][][] array = new Pair[xsize][ysize][zsize];
110 torben 1746
111     int x = 0;
112     int y = 0;
113    
114    
115     while ( (line = in.readLine() ) != null ) {
116     lines++;
117    
118     line = line.trim();
119     if (line.length() == 0)
120     continue;
121    
122     if (line.charAt(0) == '-')
123     continue;
124    
125     String elements[] = line.split(",");
126     if (elements.length != zsize) {
127     throw new Exception( "Template: invalid field count on line " + lines + ". Found " + elements.length + " but expected " + zsize );
128 torben 1738 }
129 torben 1746
130     for (int i=0; i<elements.length; i++) {
131 torben 1747 int val = 0;
132     int subval = 0;
133    
134     String element[] = elements[i].trim().split("\\.");
135 torben 1746
136 torben 1747
137     try {
138     val = Integer.parseInt( element[0] );
139 torben 1746 } catch (Exception e) {
140 torben 1747 throw new Exception( "Template: invalid value on line " + lines + ": " + element[0] );
141 torben 1738 }
142 torben 1747
143     if (val < 0 || val>255) {
144     throw new Exception( "Template: invalid value on line " + lines + ": " + val );
145     }
146    
147     if (element.length == 2) {
148     try {
149     subval = Integer.parseInt( element[1] );
150     } catch (Exception e) {
151     throw new Exception( "Template: invalid value on line " + lines + ": " + element[1] );
152     }
153     }
154    
155     array[x][y][i] = new Pair(val,subval);
156 torben 1738 }
157 torben 1746
158     x++;
159     if (x >= xsize) {
160     x = 0;
161     y++;
162 torben 1738 }
163     }
164 torben 1746
165     if (x != 0) {
166     throw new Exception( "Template: not enough lines to complete the last level" );
167 torben 1738 }
168 torben 1746
169     if (y != ysize) {
170     throw new Exception( "Template: not enough levels");
171     }
172    
173    
174     return array;
175    
176 torben 1738 }
177 torben 1746
178 torben 1747 void buildTemplate(Location loc, Pair[][][] template) {
179 torben 1738 World world = loc.getWorld();
180     for (int i=0; i<template.length; i++ ) {
181     for (int j=0; j<template[0].length; j++) {
182     for (int k=0; k<template[0][0].length; k++) {
183     int x = loc.getBlockX() + i + 1;
184     int y = loc.getBlockY() + j;
185     int z = loc.getBlockZ() + k;
186 torben 1746
187 torben 1747 int type = template[i][j][k].id;
188     byte data = (byte) template[i][j][k].subId;
189 torben 1746
190 torben 1745 //plugin.getLogger().info( String.format( "Setting typeid at %d,%d,%d to %d", x,y,z, type) );
191 torben 1746
192 torben 1747
193     world.getBlockAt(x, y, z).setTypeIdAndData(type, data, true);
194 torben 1738 }
195     }
196     }
197 torben 1746
198 torben 1738 }
199    
200     }

  ViewVC Help
Powered by ViewVC 1.1.20