/[projects]/miscJava/minecraft-plugins/setposplugin/src/Setpos.java
ViewVC logotype

Annotation of /miscJava/minecraft-plugins/setposplugin/src/Setpos.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1167 - (hide annotations) (download)
Sat Oct 16 13:54:50 2010 UTC (13 years, 7 months ago) by torben
File size: 5575 byte(s)
add home and warppoints to /whereis
1 torben 1166
2     import java.util.logging.*;
3    
4     public class Setpos extends Plugin {
5    
6     final static Logger log = Logger.getLogger("SetposPlugin");
7    
8    
9     @Override
10     public void disable() {}
11    
12     @Override
13     public void enable() {}
14    
15     @Override
16     public void initialize() {
17     PluginLoader loader = etc.getLoader();
18     loader.addListener( PluginLoader.Hook.COMMAND, new RealSetposPlugin(), this, PluginListener.Priority.MEDIUM );
19     }
20    
21    
22    
23     public static class RealSetposPlugin extends PluginListener {
24     final static String USAGE = "Usage: /setpos <x> <z> [height]";
25     final static int AIRBLOCK = 0; //block id = 0 is air
26    
27    
28    
29     //http://www.minecraftforum.net/viewtopic.php?f=35&t=14739
30     //Y is height
31    
32     // air == blockID:0
33    
34     private int getGroundLevel(int x, int z) {
35     Server srv = etc.getServer();
36    
37     int level = srv.getHighestBlockY(x,z);
38    
39     if (level == -1)
40     level = 128;
41     return level;
42    
43     /*
44     int y;
45    
46     for (y=128; y>=0 && srv.getBlockIdAt(x,y-1,z) == AIRBLOCK; y--)
47     ;
48    
49     System.out.println("Groundlevel: " + y);
50     System.out.println("getHighestBlockY: " + srv.getHighestBlockY(x,z) );
51    
52     return y;
53     */
54     }
55    
56    
57     private boolean isFree(int x, int y, int z) {
58     Server srv = etc.getServer();
59    
60     return (srv.getBlockIdAt(x,y,z) == AIRBLOCK && srv.getBlockIdAt(x,y+1,z) == AIRBLOCK);
61     }
62    
63    
64    
65     @Override
66     public boolean onCommand(Player player, java.lang.String[] split) {
67     if( split[0].equals("/setpos") ) {
68     setPos(player, split);
69     return true;
70     } else if ( split[0].equals("/whereis" ) ) {
71     whereIs(player, split);
72     return true;
73     } else {
74     return false;
75     }
76    
77     }
78    
79    
80    
81     double roundToPlaces(double value, int places) {
82     double pow = Math.pow(10, places);
83     double temp = Math.round( value*pow );
84    
85     return temp / pow;
86     }
87    
88    
89     private String getBearingStr(int angle) {
90     if (angle < 22) {
91     return "N";
92     } else if (angle < 67) {
93     return "NE";
94     } else if (angle < 112) {
95     return "E";
96     } else if (angle < 157) {
97     return "SE";
98     } else if (angle < 202) {
99     return "S";
100     } else if (angle < 257) {
101     return "SW";
102     } else if (angle < 292) {
103     return "W";
104     } else if (angle < 337) {
105     return "NW";
106     } else {
107     return "N";
108     }
109     }
110    
111     public void whereIs(Player p1, java.lang.String[] split) {
112 torben 1167 if (split.length < 2 || split.length >3) {
113     p1.sendMessage( Colors.Rose + "usage: /whereis (playername|home|warp) [warpname]" );
114 torben 1166 return;
115     }
116    
117 torben 1167 Location loc1 = p1.getLocation();
118     Location loc2;
119     String name2;
120    
121     if (split[1].equals("home") ) {
122    
123     Warp home = etc.getDataSource().getHome( p1.getName() );
124     if (home == null) {
125     p1.sendMessage(Colors.Rose + "you haven't set a home.");
126     return;
127     }
128     loc2 = home.Location;
129     name2 = "Your home";
130     } else if (split[1].equals("warp")) {
131     if (split.length != 3) {
132     p1.sendMessage("you have to enter the name of the warp point");
133     return;
134     }
135     Warp warp = etc.getDataSource().getWarp( split[2] );
136     if (warp == null) {
137     p1.sendMessage("Found now warp with name " + split[2]);
138     return;
139     }
140     loc2 = warp.Location;
141     name2 = "Warppoint " + split[2];
142    
143    
144     } else {
145    
146     Player p2 = etc.getServer().getPlayer(split[1]);
147    
148     if (p2 == null) {
149     p1.sendMessage( Colors.Rose + "whereis: no player named " + split[1] );
150     return;
151     }
152    
153     loc2 = p2.getLocation();
154     name2 = p2.getName();
155 torben 1166 }
156     //Location loc2 = new Location();
157     //loc2.x = loc2.y = loc2.z = 0;
158    
159     //System.out.println("p1: " + loc1.x + "," + loc1.z );
160     //System.out.println("p2: " + loc2.x + "," + loc2.z );
161    
162     double distX = loc1.x - loc2.x ;
163     double distZ = loc1.z - loc2.z ;
164    
165     int dist = (int) Math.round( Math.sqrt( (distX*distX) + (distZ*distZ)) );
166     /*System.out.println("distX:" + distX );
167     System.out.println("distZ:" + distZ );
168    
169     System.out.println(">> " + (distZ / distX ) );*/
170    
171     double angle = Math.toDegrees( Math.atan( distZ / distX ) );
172     if (angle < 0.0)
173     angle += 90.0;
174    
175    
176     if (distX >= 0.0 && distZ >= 0.0) { //both positive, 0-90 degrees
177     //do nothing
178     } else if (distX < 0.0 && distZ >= 0.0) { // 90-180 degrees
179     angle += 90.0;
180     } else if (distX < 0.0 && distZ < 0.0) {//Both negative 180-270 degrees
181     angle += 180.0;
182     } else {
183     angle += 270.0;
184     }
185    
186 torben 1167 p1.sendMessage( Colors.Yellow + name2 + " is at x:" + roundToPlaces(loc2.x, 2) + " y:" + roundToPlaces(loc2.y, 2) + " z: " + roundToPlaces(loc2.z, 2) );
187 torben 1166 p1.sendMessage( Colors.Yellow + "Distance is " + dist + " blocks" );
188     p1.sendMessage( Colors.Yellow + "Bearing: " + (int) angle + " (" + getBearingStr( (int) angle) + ")" );
189    
190    
191    
192     }
193    
194    
195     public void setPos(Player player, java.lang.String[] split) {
196     int x;
197     int z;
198     int y;
199    
200     int userY = 0;
201     boolean hasY = false;
202    
203     if (split.length <3 || split.length > 4) {
204     player.sendMessage(USAGE);
205     return;
206     }
207    
208     try {
209     x = Integer.parseInt( split[1] );
210     z = Integer.parseInt( split[2] );
211     if (split.length == 4) {
212     hasY = true;
213     userY = Integer.parseInt( split[3] );
214     }
215     } catch (Exception e) {
216     player.sendMessage("/setpos error: non numeric argument");
217     return;
218     }
219    
220    
221    
222     if (hasY) {
223     if (isFree(x,userY,z)) {
224     y = userY;
225     } else {
226     player.sendMessage("/setpos: that position is not free");
227     return;
228     }
229     } else {
230     y = getGroundLevel(x,z);
231     }
232    
233     log.info("Transporting " + player.getName() + " to " + x + " " + z + "(" + y + ")" );
234    
235    
236     Location newLocation = new Location();
237     newLocation.rotY = player.getPitch();
238     newLocation.rotX = player.getRotation();
239     newLocation.x = x + 0.5;
240     newLocation.z = z + 0.5;
241     newLocation.y = y;
242    
243     player.teleportTo(newLocation);
244     player.sendMessage("Whooooooosh");
245     }
246     }
247     }

  ViewVC Help
Powered by ViewVC 1.1.20