Skip to content

Commit 642c6fd

Browse files
authored
Merge pull request #60 from BlitzOffline/master
Made direction output configurable
2 parents eb47b35 + 2133751 commit 642c6fd

2 files changed

Lines changed: 49 additions & 36 deletions

File tree

src/main/java/com/extendedclip/papi/expansion/player/PlayerExpansion.java

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import org.bukkit.enchantments.Enchantment;
3333
import org.bukkit.entity.Player;
3434
import org.bukkit.potion.PotionEffectType;
35-
import org.bukkit.Location;
3635

3736
import java.util.*;
3837
import java.util.regex.Matcher;
@@ -42,8 +41,8 @@
4241
import static com.extendedclip.papi.expansion.player.PlayerUtil.format24;
4342
import static com.extendedclip.papi.expansion.player.PlayerUtil.getBiome;
4443
import static com.extendedclip.papi.expansion.player.PlayerUtil.getCapitalizedBiome;
45-
import static com.extendedclip.papi.expansion.player.PlayerUtil.getCardinalDirection;
4644
import static com.extendedclip.papi.expansion.player.PlayerUtil.getEmptySlots;
45+
import static com.extendedclip.papi.expansion.player.PlayerUtil.getDirection;
4746
import static com.extendedclip.papi.expansion.player.PlayerUtil.getTotalExperience;
4847
import static com.extendedclip.papi.expansion.player.PlayerUtil.getXZDirection;
4948
import static com.extendedclip.papi.expansion.player.PlayerUtil.itemInHand;
@@ -56,6 +55,15 @@ public final class PlayerExpansion extends PlaceholderExpansion implements Confi
5655
private int mediumValue;
5756
private int highValue;
5857

58+
private String north;
59+
private String northEast;
60+
private String east;
61+
private String southEast;
62+
private String south;
63+
private String southWest;
64+
private String west;
65+
private String northWest;
66+
5967
@Override
6068
public String getIdentifier() {
6169
return "player";
@@ -79,6 +87,14 @@ public Map<String, Object> getDefaults() {
7987
defaults.put("ping_color.low", "&a");
8088
defaults.put("ping_value.medium", 50);
8189
defaults.put("ping_value.high", 100);
90+
defaults.put("direction.north", "N");
91+
defaults.put("direction.north_east", "NE");
92+
defaults.put("direction.east", "E");
93+
defaults.put("direction.south_east", "SE");
94+
defaults.put("direction.south", "S");
95+
defaults.put("direction.south_west", "SW");
96+
defaults.put("direction.west", "W");
97+
defaults.put("direction.north_west", "NW");
8298
return defaults;
8399
}
84100

@@ -214,7 +230,25 @@ public String onRequest(OfflinePlayer player, String identifier) {
214230
case "gamemode":
215231
return p.getGameMode().name();
216232
case "direction":
217-
return getCardinalDirection(p);
233+
switch (getDirection(p)) {
234+
case NORTH:
235+
return north;
236+
case NORTH_EAST:
237+
return northEast;
238+
case EAST:
239+
return east;
240+
case SOUTH_EAST:
241+
return southEast;
242+
case SOUTH:
243+
return south;
244+
case SOUTH_WEST:
245+
return southWest;
246+
case WEST:
247+
return west;
248+
case NORTH_WEST:
249+
return northWest;
250+
}
251+
return "";
218252
case "direction_xz":
219253
return getXZDirection(p);
220254
case "world":
@@ -401,6 +435,14 @@ public boolean register() {
401435
high = this.getString("ping_color.high", "&c");
402436
mediumValue = this.getInt("ping_value.medium", 50);
403437
highValue = this.getInt("ping_value.high", 100);
438+
north = this.getString("direction.north", "N");
439+
northEast = this.getString("direction.north_east", "NE");
440+
east = this.getString("direction.east", "E");
441+
southEast = this.getString("direction.south_east", "SE");
442+
south = this.getString("direction.south", "S");
443+
southWest = this.getString("direction.south_west", "SW");
444+
west = this.getString("direction.west", "W");
445+
northWest = this.getString("direction.north_west", "NW");
404446

405447

406448
return super.register();

src/main/java/com/extendedclip/papi/expansion/player/PlayerUtil.java

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.bukkit.Bukkit;
44
import org.bukkit.Material;
5+
import org.bukkit.block.BlockFace;
56
import org.bukkit.entity.Player;
67
import org.bukkit.inventory.ItemStack;
78
import org.bukkit.inventory.PlayerInventory;
@@ -42,6 +43,7 @@ public final class PlayerUtil {
4243
public static final double ticksPerMinute = 1000d / 60d;
4344
private static final SimpleDateFormat twentyFour = new SimpleDateFormat("HH:mm", Locale.ENGLISH);
4445
private static final SimpleDateFormat twelve = new SimpleDateFormat("h:mm aa", Locale.ENGLISH);
46+
private static final BlockFace[] radial = { BlockFace.NORTH, BlockFace.NORTH_EAST, BlockFace.EAST, BlockFace.SOUTH_EAST, BlockFace.SOUTH, BlockFace.SOUTH_WEST, BlockFace.WEST, BlockFace.NORTH_WEST };
4547

4648
private PlayerUtil() {
4749
}
@@ -148,39 +150,8 @@ private static String ticksToTime(long ticks) {
148150
return (hours < 10 ? "0" + hours : hours) + ":" + (mins < 10 ? "0" + mins : mins);
149151
}
150152

151-
public static String getCardinalDirection(Player player) {
152-
double rotation = player.getLocation().getYaw() - 180.0F;
153-
if (rotation < 0.0D) {
154-
rotation += 360.0D;
155-
}
156-
if ((0.0D <= rotation) && (rotation < 22.5D)) {
157-
return "N";
158-
}
159-
if ((22.5D <= rotation) && (rotation < 67.5D)) {
160-
return "NE";
161-
}
162-
if ((67.5D <= rotation) && (rotation < 112.5D)) {
163-
return "E";
164-
}
165-
if ((112.5D <= rotation) && (rotation < 157.5D)) {
166-
return "SE";
167-
}
168-
if ((157.5D <= rotation) && (rotation < 202.5D)) {
169-
return "S";
170-
}
171-
if ((202.5D <= rotation) && (rotation < 247.5D) || (rotation <= -119.33) && (rotation > -179)) {
172-
return "SW";
173-
}
174-
if ((247.5D <= rotation) && (rotation < 292.5D) || (rotation <= -59.66) && (rotation > -119.33)) {
175-
return "W";
176-
}
177-
if ((292.5D <= rotation) && (rotation < 337.5D) || (rotation <= -0.0) && (rotation > -59.66)) {
178-
return "NW";
179-
}
180-
if ((337.5D <= rotation) && (rotation < 360.0D)) {
181-
return "N";
182-
}
183-
return "";
153+
public static BlockFace getDirection(Player player) {
154+
return radial[Math.round(player.getLocation().getYaw() / 45f) & 0x7].getOppositeFace();
184155
}
185156

186157
public static String getXZDirection(Player player) {

0 commit comments

Comments
 (0)