/[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 3239 - (hide annotations) (download)
Wed Jul 18 07:59:05 2018 UTC (5 years, 10 months ago) by torben
File size: 5932 byte(s)
Compile for spigot 1.13
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 torben 3239 import org.bukkit.Material;
11 torben 1738 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 torben 1746
21    
22 torben 1738 Plugin plugin;
23 torben 1746
24 torben 1738 public TemplateCommand(Plugin plugin) {
25     this.plugin = plugin;
26     }
27    
28     @Override
29     public boolean onCommand(final CommandSender sender, Command command, String label, String[] args) {
30     if (! (sender instanceof Player) ) {
31     sender.sendMessage( "this command can not be run from console" );
32     return true;
33     }
34 torben 1746
35 torben 1738 Player player = (Player) sender;
36 torben 1746
37 torben 1738 if (! sender.isOp() ) {
38     sender.sendMessage( "you need to be op to run this command" );
39     return true;
40     }
41 torben 1746
42 torben 1738 if ( args.length != 1) {
43     sender.sendMessage( ChatColor.YELLOW + "Usage: /template <template-name>");
44     return true;
45     }
46 torben 1746
47 torben 1753 File templateFile = new File( plugin.getDataFolder(), "templates/" + args[0] + ".txt");
48 torben 1738 if (! templateFile.exists() ) {
49     sender.sendMessage( ChatColor.YELLOW + "Template not found" );
50     return true;
51     }
52 torben 1746
53 torben 3239 Material[][][] template;
54 torben 1746
55     try {
56     template = parseFile(templateFile);
57     if( template == null) {
58     sender.sendMessage( ChatColor.YELLOW + "Invalid template file" );
59     return true;
60     }
61     } catch (Exception e) {
62     plugin.getLogger().info( e.getMessage() );
63     sender.sendMessage( ChatColor.YELLOW + e.getMessage() );
64     return true;
65 torben 1738 }
66 torben 1746
67 torben 1750 buildTemplate( player.getLocation(), template, 1);
68     buildTemplate( player.getLocation(), template, 2);
69 torben 1738
70     return false;
71     }
72    
73 torben 1746
74    
75    
76 torben 3239 Material[][][] parseFile(File templateFile) throws Exception {
77 torben 1746
78    
79     int lines = 0;
80    
81 torben 3201 try (FileInputStream fis = new FileInputStream( templateFile );
82     InputStreamReader isr = new InputStreamReader(fis);
83     BufferedReader in = new BufferedReader( isr )
84     ) {
85 torben 1755
86 torben 3201
87     String line = in.readLine();
88     lines ++;
89    
90     if ( line.charAt(0) != '#') {
91     throw new Exception( "Template: invalid start character" );
92 torben 1755 }
93 torben 3201
94     line = line.replace('#', ' ').trim();
95     String dimensions[] = line.split(",");
96    
97     if ( dimensions.length != 3) {
98     throw new Exception( "Template: invalid dimensions line" );
99 torben 1738 }
100 torben 3201
101     int xsize = Integer.parseInt(dimensions[0]);
102     int ysize = Integer.parseInt(dimensions[1]);
103     int zsize = Integer.parseInt(dimensions[2]);
104    
105 torben 3239 Material[][][] array = new Material[xsize][ysize][zsize];
106 torben 3201
107     int x = 0;
108     int y = 0;
109    
110    
111     while ( (line = in.readLine() ) != null ) {
112     lines++;
113    
114     line = line.trim();
115     if (line.length() == 0)
116     continue;
117    
118     if (line.charAt(0) == '#')
119     continue;
120 torben 1747
121 torben 3201 if ( y >= ysize) {
122     throw new Exception( "Template: found more levels in file than specified in header" );
123 torben 1738 }
124 torben 3201
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 1747 }
129 torben 3201
130     for (int i=0; i<elements.length; i++) {
131    
132     String element[] = elements[i].trim().split("\\.");
133    
134    
135 torben 3239 Material m;
136 torben 1747 try {
137 torben 3239 m = Material.valueOf(element[0]);
138 torben 1747 } catch (Exception e) {
139 torben 3239 throw new Exception( "Template: invalid material on line " + lines + ": " + element[0] );
140 torben 1747 }
141 torben 3201
142 torben 3239
143     array[x][y][i] = m;
144 torben 1747 }
145 torben 3201
146     x++;
147     if (x >= xsize) {
148     x = 0;
149     y++;
150     }
151 torben 1738 }
152 torben 3201
153     if (x != 0) {
154     throw new Exception( "Template: not enough lines to complete the last level" );
155 torben 1738 }
156 torben 3201
157     if (y != ysize) {
158     throw new Exception( "Template: not enough levels");
159     }
160    
161    
162     return array;
163    
164 torben 1738 }
165 torben 1746
166 torben 1738 }
167 torben 1746
168 torben 3239 void buildTemplate(Location loc, Material[][][] template, int pass) {
169 torben 1752
170    
171 torben 1738 World world = loc.getWorld();
172     for (int i=0; i<template.length; i++ ) {
173     for (int j=0; j<template[0].length; j++) {
174     for (int k=0; k<template[0][0].length; k++) {
175 torben 1752 int x, y ,z;
176 torben 1748
177 torben 1752 x = z = 0;
178    
179     y = loc.getBlockY() + j;
180 torben 1749 if (y >= 255) {
181 torben 1748 continue;
182 torben 1752 }
183    
184     switch ( getFacing(loc) ) {
185     case 0: //west
186     x = loc.getBlockX() - k;
187     z = loc.getBlockZ() + i + 1;
188     break;
189     case 1: //north
190     x = loc.getBlockX() - i - 1;
191     z = loc.getBlockZ() - k;
192     break;
193     case 2: //east
194     x = loc.getBlockX() + k;
195     z = loc.getBlockZ() - i - 1;
196     break;
197     case 3: //south
198     x = loc.getBlockX() + i + 1;
199     z = loc.getBlockZ() + k;
200     break;
201 torben 1748 }
202 torben 1746
203 torben 1752
204 torben 3239
205 torben 1757
206 torben 3239 Material mat = template[i][j][k];
207 torben 1746
208 torben 1745 //plugin.getLogger().info( String.format( "Setting typeid at %d,%d,%d to %d", x,y,z, type) );
209 torben 1746
210 torben 3239 //TODO: rewrite this to build a Set<Material> of skipped matterials
211 torben 1750 if (pass == 1) {
212 torben 3239
213     if ( mat == Material.TORCH || mat == Material.REDSTONE_TORCH || mat == Material.REDSTONE_WALL_TORCH) //torch / redstone torch
214 torben 1750 continue;
215 torben 3239
216     if (mat == Material.ACACIA_DOOR || mat == Material.BIRCH_DOOR || mat == Material.DARK_OAK_DOOR ) //door / irondoor
217 torben 1750 continue;
218 torben 3239
219     if (mat == Material.SIGN) // sign
220 torben 1750 continue;
221 torben 3239 if (mat == Material.LADDER) // ladder
222 torben 1756 continue;
223 torben 3239 if (mat == Material.OAK_STAIRS) //TODO: need to take all star variants into account
224 torben 1756 continue;
225 torben 1750 }
226 torben 1747
227 torben 3239
228     world.getBlockAt(x, y, z).setType(mat);
229 torben 1750
230 torben 1738 }
231     }
232     }
233 torben 1746
234 torben 1738 }
235 torben 1752
236    
237     //yaw is left,right looking direction, 0=straight west
238     int getFacing(Location loc) {
239     int angle = (int) loc.getYaw();
240    
241     if (angle < 45) {
242     return 0;
243     } else if (angle < 135) {
244     return 1;
245     } else if (angle < 225) {
246     return 2;
247     } else if (angle < 315) {
248     return 3;
249     } else {
250     return 0;
251     }
252     }
253 torben 1738
254     }

  ViewVC Help
Powered by ViewVC 1.1.20