Replace ByteArrayUtilities code with Guava and Replace PlotGameMode with WorldEdit equivalent.

This commit is contained in:
MattBDev 2019-11-28 21:50:21 -05:00
parent 006d730185
commit 4bb45fc220
12 changed files with 75 additions and 122 deletions

View File

@ -7,7 +7,6 @@ import com.github.intellectualsites.plotsquared.plot.object.Location;
import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer;
import com.github.intellectualsites.plotsquared.plot.util.EconHandler;
import com.github.intellectualsites.plotsquared.plot.util.MathMan;
import com.github.intellectualsites.plotsquared.plot.util.PlotGameMode;
import com.github.intellectualsites.plotsquared.plot.util.PlotWeather;
import com.github.intellectualsites.plotsquared.plot.util.StringMan;
import com.github.intellectualsites.plotsquared.plot.util.UUIDHandler;
@ -32,6 +31,11 @@ import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import static com.sk89q.worldedit.world.gamemode.GameModes.ADVENTURE;
import static com.sk89q.worldedit.world.gamemode.GameModes.CREATIVE;
import static com.sk89q.worldedit.world.gamemode.GameModes.SPECTATOR;
import static com.sk89q.worldedit.world.gamemode.GameModes.SURVIVAL;
public class BukkitPlayer extends PlotPlayer {
private static boolean CHECK_EFFECTIVE = true;
@ -234,36 +238,29 @@ public class BukkitPlayer extends PlotPlayer {
}
}
@NotNull @Override public PlotGameMode getGameMode() {
@NotNull @Override public com.sk89q.worldedit.world.gamemode.GameMode getGameMode() {
switch (this.player.getGameMode()) {
case ADVENTURE:
return PlotGameMode.ADVENTURE;
return ADVENTURE;
case CREATIVE:
return PlotGameMode.CREATIVE;
return CREATIVE;
case SPECTATOR:
return PlotGameMode.SPECTATOR;
return SPECTATOR;
case SURVIVAL:
return PlotGameMode.SURVIVAL;
default:
return PlotGameMode.NOT_SET;
return SURVIVAL;
}
}
@Override public void setGameMode(@NotNull final PlotGameMode gameMode) {
switch (gameMode) {
case ADVENTURE:
@Override public void setGameMode(@NotNull final com.sk89q.worldedit.world.gamemode.GameMode gameMode) {
if (ADVENTURE.equals(gameMode)) {
this.player.setGameMode(GameMode.ADVENTURE);
break;
case CREATIVE:
} else if (CREATIVE.equals(gameMode)) {
this.player.setGameMode(GameMode.CREATIVE);
break;
case SPECTATOR:
} else if (SPECTATOR.equals(gameMode)) {
this.player.setGameMode(GameMode.SPECTATOR);
break;
case SURVIVAL:
default:
} else {
this.player.setGameMode(GameMode.SURVIVAL);
break;
}
}
@ -303,7 +300,7 @@ public class BukkitPlayer extends PlotPlayer {
}
@Override public void stopSpectating() {
if (getGameMode() == PlotGameMode.SPECTATOR) {
if (getGameMode() == SPECTATOR) {
this.player.setSpectatorTarget(null);
}
}

View File

@ -12,11 +12,11 @@ import com.github.intellectualsites.plotsquared.plot.object.PlotArea;
import com.github.intellectualsites.plotsquared.plot.object.PlotId;
import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer;
import com.github.intellectualsites.plotsquared.plot.object.RunnableVal;
import com.github.intellectualsites.plotsquared.plot.util.ByteArrayUtilities;
import com.github.intellectualsites.plotsquared.plot.util.EconHandler;
import com.github.intellectualsites.plotsquared.plot.util.MainUtil;
import com.github.intellectualsites.plotsquared.plot.util.Permissions;
import com.github.intellectualsites.plotsquared.plot.util.TaskManager;
import com.google.common.primitives.Ints;
import org.jetbrains.annotations.Nullable;
import java.util.Set;
@ -47,8 +47,7 @@ public class Auto extends SubCommand {
MainUtil.sendMessage(player, Captions.CANT_CLAIM_MORE_PLOTS_NUM, -diff + "");
return false;
} else if (player.hasPersistentMeta("grantedPlots")) {
int grantedPlots =
ByteArrayUtilities.bytesToInteger(player.getPersistentMeta("grantedPlots"));
int grantedPlots = Ints.fromByteArray(player.getPersistentMeta("grantedPlots"));
if (grantedPlots - diff < sizeX * sizeZ) {
player.removePersistentMeta("grantedPlots");
MainUtil.sendMessage(player, Captions.CANT_CLAIM_MORE_PLOTS);
@ -58,8 +57,7 @@ public class Auto extends SubCommand {
if (left == 0) {
player.removePersistentMeta("grantedPlots");
} else {
player.setPersistentMeta("grantedPlots",
ByteArrayUtilities.integerToBytes(left));
player.setPersistentMeta("grantedPlots", Ints.toByteArray(left));
}
MainUtil.sendMessage(player, Captions.REMOVED_GRANTED_PLOT, "" + left,
"" + (grantedPlots - left));

View File

@ -15,6 +15,7 @@ import com.github.intellectualsites.plotsquared.plot.util.ByteArrayUtilities;
import com.github.intellectualsites.plotsquared.plot.util.EconHandler;
import com.github.intellectualsites.plotsquared.plot.util.Permissions;
import com.github.intellectualsites.plotsquared.plot.util.TaskManager;
import com.google.common.primitives.Ints;
@CommandDeclaration(command = "claim", aliases = "c",
description = "Claim the current plot you're standing on", category = CommandCategory.CLAIMING,
@ -38,7 +39,7 @@ public class Claim extends SubCommand {
if (currentPlots >= player.getAllowedPlots()) {
if (player.hasPersistentMeta("grantedPlots")) {
grants =
ByteArrayUtilities.bytesToInteger(player.getPersistentMeta("grantedPlots"));
Ints.fromByteArray(player.getPersistentMeta("grantedPlots"));
if (grants <= 0) {
player.removePersistentMeta("grantedPlots");
return sendMessage(player, Captions.CANT_CLAIM_MORE_PLOTS);
@ -85,7 +86,7 @@ public class Claim extends SubCommand {
player.removePersistentMeta("grantedPlots");
} else {
player.setPersistentMeta("grantedPlots",
ByteArrayUtilities.integerToBytes(grants - 1));
Ints.toByteArray(grants - 1));
}
sendMessage(player, Captions.REMOVED_GRANTED_PLOT, "1", "" + (grants - 1));
}

View File

@ -11,9 +11,9 @@ import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer;
import com.github.intellectualsites.plotsquared.plot.util.EventUtil;
import com.github.intellectualsites.plotsquared.plot.util.MainUtil;
import com.github.intellectualsites.plotsquared.plot.util.Permissions;
import com.github.intellectualsites.plotsquared.plot.util.PlotGameMode;
import com.github.intellectualsites.plotsquared.plot.util.UUIDHandler;
import com.github.intellectualsites.plotsquared.plot.util.WorldUtil;
import com.sk89q.worldedit.world.gamemode.GameModes;
import java.util.Set;
import java.util.UUID;
@ -94,7 +94,7 @@ import java.util.UUID;
if (player.hasPermission("plots.admin.entry.denied")) {
return;
}
if (player.getGameMode() == PlotGameMode.SPECTATOR) {
if (player.getGameMode() == GameModes.SPECTATOR) {
player.stopSpectating();
}
Location location = player.getLocation();

View File

@ -8,10 +8,10 @@ import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer;
import com.github.intellectualsites.plotsquared.plot.object.RunnableVal;
import com.github.intellectualsites.plotsquared.plot.object.RunnableVal2;
import com.github.intellectualsites.plotsquared.plot.object.RunnableVal3;
import com.github.intellectualsites.plotsquared.plot.util.ByteArrayUtilities;
import com.github.intellectualsites.plotsquared.plot.util.MainUtil;
import com.github.intellectualsites.plotsquared.plot.util.Permissions;
import com.github.intellectualsites.plotsquared.plot.util.UUIDHandler;
import com.google.common.primitives.Ints;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
@ -56,7 +56,7 @@ import java.util.concurrent.CompletableFuture;
if (array == null) {
granted = 0;
} else {
granted = ByteArrayUtilities.bytesToInteger(array);
granted = Ints.fromByteArray(array);
}
Captions.GRANTED_PLOTS.send(player, granted);
} else { // add
@ -64,11 +64,11 @@ import java.util.concurrent.CompletableFuture;
if (array == null) {
amount = 1;
} else {
amount = 1 + ByteArrayUtilities.bytesToInteger(array);
amount = 1 + Ints.fromByteArray(array);
}
boolean replace = array != null;
String key = "grantedPlots";
byte[] rawData = ByteArrayUtilities.integerToBytes(amount);
byte[] rawData = Ints.toByteArray(amount);
PlotPlayer online = UUIDHandler.getPlayer(uuid);
if (online != null) {
online.setPersistentMeta(key, rawData);

View File

@ -1,38 +1,38 @@
package com.github.intellectualsites.plotsquared.plot.flag;
import com.github.intellectualsites.plotsquared.plot.config.Captions;
import com.github.intellectualsites.plotsquared.plot.util.PlotGameMode;
import com.sk89q.worldedit.world.gamemode.GameMode;
import com.sk89q.worldedit.world.gamemode.GameModes;
public class GameModeFlag extends Flag<PlotGameMode> {
public class GameModeFlag extends Flag<GameMode> {
public GameModeFlag(String name) {
super(Captions.FLAG_CATEGORY_GAMEMODE, name);
}
@Override public String valueToString(Object value) {
return ((PlotGameMode) value).getName();
return ((GameMode) value).getName();
}
@Override public PlotGameMode parseValue(String value) {
@Override public GameMode parseValue(String value) {
switch (value.toLowerCase()) {
case "survival":
case "s":
case "0":
return PlotGameMode.SURVIVAL;
case "creative":
case "c":
case "1":
return PlotGameMode.CREATIVE;
return GameModes.CREATIVE;
case "adventure":
case "a":
case "2":
return PlotGameMode.ADVENTURE;
return GameModes.ADVENTURE;
case "spectator":
case "sp":
case "3":
return PlotGameMode.SPECTATOR;
return GameModes.SPECTATOR;
case "survival":
case "s":
case "0":
default:
return PlotGameMode.NOT_SET;
return GameModes.SURVIVAL;
}
}

View File

@ -15,13 +15,14 @@ import com.github.intellectualsites.plotsquared.plot.util.CommentManager;
import com.github.intellectualsites.plotsquared.plot.util.EventUtil;
import com.github.intellectualsites.plotsquared.plot.util.MainUtil;
import com.github.intellectualsites.plotsquared.plot.util.Permissions;
import com.github.intellectualsites.plotsquared.plot.util.PlotGameMode;
import com.github.intellectualsites.plotsquared.plot.util.PlotWeather;
import com.github.intellectualsites.plotsquared.plot.util.StringMan;
import com.github.intellectualsites.plotsquared.plot.util.TaskManager;
import com.github.intellectualsites.plotsquared.plot.util.UUIDHandler;
import com.github.intellectualsites.plotsquared.plot.util.expiry.ExpireManager;
import com.github.intellectualsites.plotsquared.plot.util.world.ItemUtil;
import com.sk89q.worldedit.world.gamemode.GameMode;
import com.sk89q.worldedit.world.gamemode.GameModes;
import com.sk89q.worldedit.world.item.ItemType;
import com.sk89q.worldedit.world.item.ItemTypes;
@ -95,9 +96,9 @@ public class PlotListener {
Optional<Boolean> flyFlag = plot.getFlag(Flags.FLY);
if (flyFlag.isPresent()) {
boolean flight = player.getFlight();
PlotGameMode gamemode = player.getGameMode();
if (flight != (gamemode == PlotGameMode.CREATIVE
|| gamemode == PlotGameMode.SPECTATOR)) {
GameMode gamemode = player.getGameMode();
if (flight != (gamemode == GameModes.CREATIVE
|| gamemode == GameModes.SPECTATOR)) {
player.setPersistentMeta("flight",
ByteArrayUtilities.booleanToBytes(player.getFlight()));
}
@ -105,7 +106,7 @@ public class PlotListener {
player.setFlight(flyFlag.get());
}
}
Optional<PlotGameMode> gamemodeFlag = plot.getFlag(Flags.GAMEMODE);
Optional<GameMode> gamemodeFlag = plot.getFlag(Flags.GAMEMODE);
if (gamemodeFlag.isPresent()) {
if (player.getGameMode() != gamemodeFlag.get()) {
if (!Permissions.hasPermission(player, "plots.gamemode.bypass")) {
@ -118,7 +119,7 @@ public class PlotListener {
}
}
}
Optional<PlotGameMode> guestGamemodeFlag = plot.getFlag(Flags.GUEST_GAMEMODE);
Optional<GameMode> guestGamemodeFlag = plot.getFlag(Flags.GUEST_GAMEMODE);
if (guestGamemodeFlag.isPresent()) {
if (player.getGameMode() != guestGamemodeFlag.get() && !plot
.isAdded(player.getUUID())) {
@ -227,7 +228,7 @@ public class PlotListener {
} else {
MainUtil.sendMessage(player, StringMan
.replaceAll(Captions.GAMEMODE_WAS_BYPASSED.getTranslated(), "{plot}",
plot.toString(), "{gamemode}", pw.GAMEMODE.name().toLowerCase()));
plot.toString(), "{gamemode}", pw.GAMEMODE.getName().toLowerCase()));
}
}
}
@ -258,8 +259,8 @@ public class PlotListener {
ByteArrayUtilities.bytesToBoolean(player.getPersistentMeta("flight")));
player.removePersistentMeta("flight");
} else {
PlotGameMode gameMode = player.getGameMode();
if (gameMode == PlotGameMode.SURVIVAL || gameMode == PlotGameMode.ADVENTURE) {
GameMode gameMode = player.getGameMode();
if (gameMode == GameModes.SURVIVAL || gameMode == GameModes.ADVENTURE) {
player.setFlight(false);
} else if (!player.getFlight()) {
player.setFlight(true);

View File

@ -3,10 +3,11 @@ package com.github.intellectualsites.plotsquared.plot.object;
import com.github.intellectualsites.plotsquared.plot.PlotSquared;
import com.github.intellectualsites.plotsquared.plot.commands.RequiredType;
import com.github.intellectualsites.plotsquared.plot.database.DBFunc;
import com.github.intellectualsites.plotsquared.plot.util.PlotGameMode;
import com.github.intellectualsites.plotsquared.plot.util.PlotWeather;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.regions.CuboidRegion;
import com.sk89q.worldedit.world.gamemode.GameMode;
import com.sk89q.worldedit.world.gamemode.GameModes;
import com.sk89q.worldedit.world.item.ItemType;
import org.jetbrains.annotations.NotNull;
@ -110,11 +111,11 @@ public class ConsolePlayer extends PlotPlayer {
@Override public void setWeather(@NotNull PlotWeather weather) {
}
@NotNull @Override public PlotGameMode getGameMode() {
return PlotGameMode.NOT_SET;
@Override public @NotNull GameMode getGameMode() {
return GameModes.SPECTATOR;
}
@Override public void setGameMode(@NotNull PlotGameMode gameMode) {
@Override public void setGameMode(@NotNull GameMode gameMode) {
}
@Override public void setTime(long time) {

View File

@ -14,7 +14,6 @@ import com.github.intellectualsites.plotsquared.plot.util.EconHandler;
import com.github.intellectualsites.plotsquared.plot.util.EventUtil;
import com.github.intellectualsites.plotsquared.plot.util.MainUtil;
import com.github.intellectualsites.plotsquared.plot.util.MathMan;
import com.github.intellectualsites.plotsquared.plot.util.PlotGameMode;
import com.github.intellectualsites.plotsquared.plot.util.StringMan;
import com.github.intellectualsites.plotsquared.plot.util.area.QuadMap;
import com.github.intellectualsites.plotsquared.plot.util.block.GlobalBlockQueue;
@ -27,6 +26,8 @@ import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.regions.CuboidRegion;
import com.sk89q.worldedit.world.biome.BiomeType;
import com.sk89q.worldedit.world.biome.BiomeTypes;
import com.sk89q.worldedit.world.gamemode.GameMode;
import com.sk89q.worldedit.world.gamemode.GameModes;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -82,7 +83,7 @@ public abstract class PlotArea {
public PlotLoc DEFAULT_HOME;
public int MAX_BUILD_HEIGHT = 256;
public int MIN_BUILD_HEIGHT = 1;
public PlotGameMode GAMEMODE = PlotGameMode.CREATIVE;
public GameMode GAMEMODE = GameModes.CREATIVE;
private int hash;
private CuboidRegion region;
private ConcurrentHashMap<String, Object> meta;
@ -248,27 +249,25 @@ public abstract class PlotArea {
this.MIN_BUILD_HEIGHT = config.getInt("world.min_height");
switch (config.getString("world.gamemode").toLowerCase()) {
case "survival":
case "s":
case "0":
this.GAMEMODE = PlotGameMode.SURVIVAL;
break;
case "creative":
case "c":
case "1":
this.GAMEMODE = PlotGameMode.CREATIVE;
this.GAMEMODE = GameModes.CREATIVE;
break;
case "adventure":
case "a":
case "2":
this.GAMEMODE = PlotGameMode.ADVENTURE;
this.GAMEMODE = GameModes.ADVENTURE;
break;
case "spectator":
case "3":
this.GAMEMODE = PlotGameMode.SPECTATOR;
this.GAMEMODE = GameModes.SPECTATOR;
break;
case "survival":
case "s":
case "0":
default:
this.GAMEMODE = PlotGameMode.NOT_SET;
this.GAMEMODE = GameModes.SURVIVAL;
break;
}
@ -363,7 +362,7 @@ public abstract class PlotArea {
options.put("home.nonmembers", position);
options.put("world.max_height", this.MAX_BUILD_HEIGHT);
options.put("world.min_height", this.MIN_BUILD_HEIGHT);
options.put("world.gamemode", this.GAMEMODE.name().toLowerCase());
options.put("world.gamemode", this.GAMEMODE.getName().toLowerCase());
if (this.TYPE != 0) {
options.put("generator.terrain", this.TERRAIN);

View File

@ -13,13 +13,13 @@ import com.github.intellectualsites.plotsquared.plot.object.worlds.SinglePlotAre
import com.github.intellectualsites.plotsquared.plot.util.EconHandler;
import com.github.intellectualsites.plotsquared.plot.util.EventUtil;
import com.github.intellectualsites.plotsquared.plot.util.Permissions;
import com.github.intellectualsites.plotsquared.plot.util.PlotGameMode;
import com.github.intellectualsites.plotsquared.plot.util.PlotWeather;
import com.github.intellectualsites.plotsquared.plot.util.TaskManager;
import com.github.intellectualsites.plotsquared.plot.util.UUIDHandler;
import com.github.intellectualsites.plotsquared.plot.util.expiry.ExpireManager;
import com.google.common.base.Preconditions;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.world.gamemode.GameMode;
import com.sk89q.worldedit.world.item.ItemType;
import lombok.NonNull;
import org.jetbrains.annotations.NotNull;
@ -438,14 +438,14 @@ public abstract class PlotPlayer implements CommandCaller, OfflinePlotPlayer {
*
* @return the gamemode of the player.
*/
@NotNull public abstract PlotGameMode getGameMode();
public abstract @NotNull GameMode getGameMode();
/**
* Set this player's gameMode.
*
* @param gameMode the gamemode to set
*/
public abstract void setGameMode(@NotNull PlotGameMode gameMode);
public abstract void setGameMode(@NotNull GameMode gameMode);
/**
* Set this player's local time (ticks).

View File

@ -1,21 +1,9 @@
package com.github.intellectualsites.plotsquared.plot.util;
import com.google.common.primitives.Ints;
public class ByteArrayUtilities {
public static byte[] integerToBytes(int i) {
byte[] bytes = new byte[4];
bytes[0] = (byte) (i >> 24);
bytes[1] = (byte) (i >> 16);
bytes[2] = (byte) (i >> 8);
bytes[3] = (byte) (i);
return bytes;
}
public static int bytesToInteger(byte[] bytes) {
return (bytes[0] << 24) & 0xff000000 | (bytes[1] << 16) & 0x00ff0000
| (bytes[2] << 8) & 0x0000ff00 | (bytes[3]) & 0x000000ff;
}
public static boolean bytesToBoolean(byte[] bytes) {
return bytes[0] == 1;
}

View File

@ -1,32 +0,0 @@
package com.github.intellectualsites.plotsquared.plot.util;
public enum PlotGameMode {
NOT_SET(-1, ""), SURVIVAL(0, "survival"), CREATIVE(1, "creative"), ADVENTURE(2,
"adventure"), SPECTATOR(3, "spectator");
private final int id;
private final String name;
PlotGameMode(int id, String name) {
this.id = id;
this.name = name;
}
/**
* The magic-value id of the GameMode.
*
* @return the GameMode id
*/
public int getId() {
return this.id;
}
/**
* Get the name of this GameMode
*
* @return the GameMode name
*/
public String getName() {
return this.name;
}
}