/[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 1738 - (show annotations) (download)
Sun Mar 18 10:19:19 2012 UTC (12 years, 2 months ago) by torben
File size: 4189 byte(s)
First attempt on new Template command
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.charAt(0) == '-')
114 continue;
115
116 String elements[] = line.split(",");
117 if (elements.length != zsize) {
118 plugin.getLogger().info( "Template: invalid field count on line " + lines + ". Found " + elements.length + " but expected " + zsize );
119 return null;
120 }
121
122 for (int i=0; i<elements.length; i++) {
123 try {
124 int val = Integer.parseInt( elements[i] );
125
126 array[x][y][i] = val;
127
128 } catch (Exception e) {
129 plugin.getLogger().info( "Template: invalid value on line " + lines + ": " + elements[i] );
130 return null;
131 }
132 }
133
134 x++;
135 if (x >= xsize) {
136 x = 0;
137 y++;
138 }
139 }
140
141 if (x != 0) {
142 plugin.getLogger().info( "Template: not enough lines to complete the last level" );
143 return null;
144 }
145
146 if (y != ysize) {
147 plugin.getLogger().info( "Template: not enough levels");
148 return null;
149 }
150
151
152 return array;
153 }
154 catch (IOException e) {
155 plugin.getLogger().info( "Template: Error parsing " + templateFile.getName() + ":" + e.getMessage() );
156 return null;
157 }
158 }
159
160 void buildTemplate(Location loc, int[][][] template) {
161 World world = loc.getWorld();
162 for (int i=0; i<template.length; i++ ) {
163 for (int j=0; j<template[0].length; j++) {
164 for (int k=0; k<template[0][0].length; k++) {
165 int x = loc.getBlockX() + i + 1;
166 int y = loc.getBlockY() + j;
167 int z = loc.getBlockZ() + k;
168
169 world.getBlockAt(x, y, z).setTypeId( template[x][y][z] );
170 }
171 }
172 }
173
174 }
175
176 }

  ViewVC Help
Powered by ViewVC 1.1.20