/[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 1743 - (show annotations) (download)
Sun Mar 18 10:52:16 2012 UTC (12 years, 2 months ago) by torben
File size: 4418 byte(s)
debug info
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 String element = elements[i].trim();
128 int val = Integer.parseInt( element );
129
130 array[x][y][i] = val;
131
132 } catch (Exception e) {
133 plugin.getLogger().info( "Template: invalid value on line " + lines + ": " + elements[i] );
134 return null;
135 }
136 }
137
138 x++;
139 if (x >= xsize) {
140 x = 0;
141 y++;
142 }
143 }
144
145 if (x != 0) {
146 plugin.getLogger().info( "Template: not enough lines to complete the last level" );
147 return null;
148 }
149
150 if (y != ysize) {
151 plugin.getLogger().info( "Template: not enough levels");
152 return null;
153 }
154
155
156 return array;
157 }
158 catch (IOException e) {
159 plugin.getLogger().info( "Template: Error parsing " + templateFile.getName() + ":" + e.getMessage() );
160 return null;
161 }
162 }
163
164 void buildTemplate(Location loc, int[][][] template) {
165 World world = loc.getWorld();
166 for (int i=0; i<template.length; i++ ) {
167 for (int j=0; j<template[0].length; j++) {
168 for (int k=0; k<template[0][0].length; k++) {
169 int x = loc.getBlockX() + i + 1;
170 int y = loc.getBlockY() + j;
171 int z = loc.getBlockZ() + k;
172
173 int type = template[i][j][k];
174
175
176 plugin.getLogger().info( String.format( "Setting typeid at %d,%d,%d to %d", x,y,z, type) );
177
178 world.getBlockAt(x, y, z).setTypeId( type );
179 }
180 }
181 }
182
183 }
184
185 }

  ViewVC Help
Powered by ViewVC 1.1.20