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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1166 - (show annotations) (download)
Fri Oct 15 12:34:50 2010 UTC (13 years, 7 months ago) by torben
File size: 4848 byte(s)
import minecraft plugins
1
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 if (split.length != 2) {
113 p1.sendMessage( Colors.Rose + "usage: /whereis <playername>");
114 return;
115 }
116 Player p2 = etc.getServer().getPlayer(split[1]);
117
118 if (p2 == null) {
119 p1.sendMessage( Colors.Rose + "whereis: no player named " + split[1] );
120 return;
121 }
122 Location loc1 = p1.getLocation();
123 Location loc2 = p2.getLocation();
124 //Location loc2 = new Location();
125 //loc2.x = loc2.y = loc2.z = 0;
126
127 //System.out.println("p1: " + loc1.x + "," + loc1.z );
128 //System.out.println("p2: " + loc2.x + "," + loc2.z );
129
130 double distX = loc1.x - loc2.x ;
131 double distZ = loc1.z - loc2.z ;
132
133 int dist = (int) Math.round( Math.sqrt( (distX*distX) + (distZ*distZ)) );
134 /*System.out.println("distX:" + distX );
135 System.out.println("distZ:" + distZ );
136
137 System.out.println(">> " + (distZ / distX ) );*/
138
139 double angle = Math.toDegrees( Math.atan( distZ / distX ) );
140 if (angle < 0.0)
141 angle += 90.0;
142
143
144 if (distX >= 0.0 && distZ >= 0.0) { //both positive, 0-90 degrees
145 //do nothing
146 } else if (distX < 0.0 && distZ >= 0.0) { // 90-180 degrees
147 angle += 90.0;
148 } else if (distX < 0.0 && distZ < 0.0) {//Both negative 180-270 degrees
149 angle += 180.0;
150 } else {
151 angle += 270.0;
152 }
153
154 p1.sendMessage( Colors.Yellow + p2.getName() + " is at x:" + roundToPlaces(p2.getX(),2) + " y:" + roundToPlaces(p2.getY(),2) + " z: " + roundToPlaces(p2.getZ(),2) );
155 p1.sendMessage( Colors.Yellow + "Distance is " + dist + " blocks" );
156 p1.sendMessage( Colors.Yellow + "Bearing: " + (int) angle + " (" + getBearingStr( (int) angle) + ")" );
157
158
159
160 }
161
162
163 public void setPos(Player player, java.lang.String[] split) {
164 int x;
165 int z;
166 int y;
167
168 int userY = 0;
169 boolean hasY = false;
170
171 if (split.length <3 || split.length > 4) {
172 player.sendMessage(USAGE);
173 return;
174 }
175
176 try {
177 x = Integer.parseInt( split[1] );
178 z = Integer.parseInt( split[2] );
179 if (split.length == 4) {
180 hasY = true;
181 userY = Integer.parseInt( split[3] );
182 }
183 } catch (Exception e) {
184 player.sendMessage("/setpos error: non numeric argument");
185 return;
186 }
187
188
189
190 if (hasY) {
191 if (isFree(x,userY,z)) {
192 y = userY;
193 } else {
194 player.sendMessage("/setpos: that position is not free");
195 return;
196 }
197 } else {
198 y = getGroundLevel(x,z);
199 }
200
201 log.info("Transporting " + player.getName() + " to " + x + " " + z + "(" + y + ")" );
202
203
204 Location newLocation = new Location();
205 newLocation.rotY = player.getPitch();
206 newLocation.rotX = player.getRotation();
207 newLocation.x = x + 0.5;
208 newLocation.z = z + 0.5;
209 newLocation.y = y;
210
211 player.teleportTo(newLocation);
212 player.sendMessage("Whooooooosh");
213 }
214 }
215 }

  ViewVC Help
Powered by ViewVC 1.1.20