Migrate from Guava Optionals to Java Optionals

This commit is contained in:
matt 2019-01-02 23:11:26 -05:00
parent ae9e52f093
commit c7b9bfeb4b
13 changed files with 40 additions and 132 deletions

View File

@ -12,7 +12,6 @@ import com.github.intellectualsites.plotsquared.plot.listener.PlayerBlockEventTy
import com.github.intellectualsites.plotsquared.plot.listener.PlotListener;
import com.github.intellectualsites.plotsquared.plot.object.*;
import com.github.intellectualsites.plotsquared.plot.util.*;
import com.google.common.base.Optional;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
@ -1693,7 +1692,7 @@ import java.util.regex.Pattern;
return;
}
Plot plot = area.getOwnedPlot(location);
if (plot == null || !plot.getFlag(Flags.EXPLOSION).or(false)) {
if (plot == null || !plot.getFlag(Flags.EXPLOSION).orElse(false)) {
event.setCancelled(true);
}
event.blockList().removeIf(
@ -2170,7 +2169,7 @@ import java.util.regex.Pattern;
|| !plotIgnited.equals(plot)) ||
(igniteCause == BlockIgniteEvent.IgniteCause.SPREAD
|| igniteCause == BlockIgniteEvent.IgniteCause.LAVA) && (
!plot.getFlag(Flags.BLOCK_IGNITION).or(false) || plotIgnited == null
!plot.getFlag(Flags.BLOCK_IGNITION).orElse(false) || plotIgnited == null
|| !plotIgnited.equals(plot))) {
event.setCancelled(true);
}

View File

@ -7,7 +7,6 @@ import com.github.intellectualsites.plotsquared.plot.flag.Flags;
import com.github.intellectualsites.plotsquared.plot.listener.PlotListener;
import com.github.intellectualsites.plotsquared.plot.object.Plot;
import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer;
import com.google.common.base.Optional;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.block.Block;
@ -28,6 +27,7 @@ import org.bukkit.plugin.java.JavaPlugin;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.UUID;
@SuppressWarnings("unused")
@ -139,15 +139,11 @@ public class PlotPlusListener extends PlotListener implements Listener {
Player player = event.getPlayer();
Plot plot = event.getPlot();
Optional<Integer[]> feed = plot.getFlag(Flags.FEED);
if (feed.isPresent()) {
Integer[] value = feed.get();
feedRunnable.put(player.getUniqueId(), new Interval(value[0], value[1], 20));
}
feed.ifPresent( value -> feedRunnable.put(player.getUniqueId(), new Interval(value[0], value[1], 20))
);
Optional<Integer[]> heal = plot.getFlag(Flags.HEAL);
if (heal.isPresent()) {
Integer[] value = heal.get();
healRunnable.put(player.getUniqueId(), new Interval(value[0], value[1], 20));
}
heal.ifPresent( value -> healRunnable.put(player.getUniqueId(), new Interval(value[0], value[1], 20))
);
}
@EventHandler

View File

@ -11,8 +11,8 @@ import com.github.intellectualsites.plotsquared.plot.object.RunnableVal3;
import com.github.intellectualsites.plotsquared.plot.util.EconHandler;
import com.github.intellectualsites.plotsquared.plot.util.MainUtil;
import com.github.intellectualsites.plotsquared.plot.util.UUIDHandler;
import com.google.common.base.Optional;
import java.util.Optional;
import java.util.Set;
@CommandDeclaration(command = "buy", description = "Buy the plot you are standing on",

View File

@ -10,7 +10,6 @@ import com.github.intellectualsites.plotsquared.plot.object.Location;
import com.github.intellectualsites.plotsquared.plot.object.Plot;
import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer;
import com.github.intellectualsites.plotsquared.plot.util.*;
import com.google.common.base.Optional;
import java.util.*;

View File

@ -8,7 +8,6 @@ import com.github.intellectualsites.plotsquared.plot.flag.Flags;
import com.github.intellectualsites.plotsquared.plot.object.*;
import com.github.intellectualsites.plotsquared.plot.util.*;
import com.github.intellectualsites.plotsquared.plot.util.expiry.ExpireManager;
import com.google.common.base.Optional;
import java.util.*;
import java.util.Map.Entry;
@ -162,10 +161,9 @@ public class ListCmd extends SubCommand {
plots = new ArrayList<>();
for (Plot plot : PlotSquared.get().getPlots()) {
Optional<String> flag = plot.getFlag(Flags.DONE);
if (!flag.isPresent()) {
continue;
if (flag.isPresent()) {
plots.add(plot);
}
plots.add(plot);
}
Collections.sort(plots, new Comparator<Plot>() {
@Override public int compare(Plot a, Plot b) {

View File

@ -5,10 +5,8 @@ import com.github.intellectualsites.plotsquared.plot.database.DBFunc;
import com.github.intellectualsites.plotsquared.plot.object.*;
import com.github.intellectualsites.plotsquared.plot.util.EventUtil;
import com.github.intellectualsites.plotsquared.plot.util.Permissions;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableSet;
import java.lang.reflect.Field;
import java.util.*;
/**
@ -16,38 +14,9 @@ import java.util.*;
*/
public class FlagManager {
/**
* Some events can be called millions of times each second (e.g. physics)
* and reusing is a lot faster.
*/
private static final Optional MUTABLE_OPTIONAL;
private static Field MUTABLE_OPTIONAL_FIELD;
static {
MUTABLE_OPTIONAL = Optional.of(new Object());
try {
MUTABLE_OPTIONAL_FIELD = MUTABLE_OPTIONAL.getClass().getDeclaredField("reference");
MUTABLE_OPTIONAL_FIELD.setAccessible(true);
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
public static <V> Optional<V> getPlotFlag(Plot plot, Flag<V> key) {
V value = FlagManager.getPlotFlagRaw(plot, key);
if (value != null) {
if (PlotSquared.get().isMainThread(Thread.currentThread())) {
try {
MUTABLE_OPTIONAL_FIELD.set(MUTABLE_OPTIONAL, value);
return MUTABLE_OPTIONAL;
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return Optional.of(value);
}
return Optional.absent();
return Optional.ofNullable(value);
}
/**

View File

@ -8,10 +8,10 @@ import com.github.intellectualsites.plotsquared.plot.flag.Flags;
import com.github.intellectualsites.plotsquared.plot.object.*;
import com.github.intellectualsites.plotsquared.plot.util.*;
import com.github.intellectualsites.plotsquared.plot.util.expiry.ExpireManager;
import com.google.common.base.Optional;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
public class PlotListener {
@ -122,9 +122,8 @@ public class PlotListener {
}
}
Optional<PlotWeather> weatherFlag = plot.getFlag(Flags.WEATHER);
if (weatherFlag.isPresent()) {
player.setWeather(weatherFlag.get());
} Optional<String> musicFlag = plot.getFlag(Flags.MUSIC);
weatherFlag.ifPresent(player::setWeather);
Optional<String> musicFlag = plot.getFlag(Flags.MUSIC);
if (musicFlag.isPresent()) {
final String id = musicFlag.get();
final PlotBlock block = PlotBlock.get(id);
@ -211,14 +210,13 @@ public class PlotListener {
}
}
Optional<String> farewell = plot.getFlag(Flags.FAREWELL);
if (farewell.isPresent()) {
MainUtil.format(C.PREFIX_FAREWELL.s() + farewell.get(), plot, player, false,
farewell.ifPresent(s -> MainUtil.format(C.PREFIX_FAREWELL.s() + s, plot, player, false,
new RunnableVal<String>() {
@Override public void run(String value) {
@Override
public void run(String value) {
MainUtil.sendMessage(player, value);
}
});
}
}));
Optional<Boolean> leave = plot.getFlag(Flags.NOTIFY_LEAVE);
if (leave.isPresent() && leave.get()) {
if (!Permissions.hasPermission(player, "plots.flag.notify-enter.bypass")) {

View File

@ -16,7 +16,6 @@ import com.github.intellectualsites.plotsquared.plot.util.block.GlobalBlockQueue
import com.github.intellectualsites.plotsquared.plot.util.block.LocalBlockQueue;
import com.github.intellectualsites.plotsquared.plot.util.expiry.ExpireManager;
import com.github.intellectualsites.plotsquared.plot.util.expiry.PlotAnalysis;
import com.google.common.base.Optional;
import com.google.common.collect.BiMap;
import com.google.common.collect.ImmutableSet;
import com.sk89q.jnbt.CompoundTag;

View File

@ -8,11 +8,11 @@ import com.github.intellectualsites.plotsquared.plot.flag.Flags;
import com.github.intellectualsites.plotsquared.plot.listener.PlayerBlockEventType;
import com.github.intellectualsites.plotsquared.plot.object.*;
import com.github.intellectualsites.plotsquared.plot.util.expiry.ExpireManager;
import com.google.common.base.Optional;
import javax.annotation.Nullable;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
public abstract class EventUtil {
@ -154,7 +154,7 @@ public abstract class EventUtil {
return Permissions
.hasPermission(player, C.PERMISSION_ADMIN_INTERACT_ROAD.s(), notifyPerms);
}
if (plot.getFlag(Flags.HANGING_BREAK).or(false)) {
if (plot.getFlag(Flags.HANGING_BREAK).orElse(false)) {
return true;
}
if (plot.hasOwner()) {
@ -170,7 +170,7 @@ public abstract class EventUtil {
return Permissions
.hasPermission(player, C.PERMISSION_ADMIN_INTERACT_ROAD.s(), notifyPerms);
}
if (plot.getFlag(Flags.MISC_BREAK).or(false)) {
if (plot.getFlag(Flags.MISC_BREAK).orElse(false)) {
return true;
}
if (plot.hasOwner()) {
@ -186,7 +186,7 @@ public abstract class EventUtil {
return Permissions
.hasPermission(player, C.PERMISSION_ADMIN_INTERACT_ROAD.s(), notifyPerms);
}
if (plot.getFlag(Flags.VEHICLE_BREAK).or(false)) {
if (plot.getFlag(Flags.VEHICLE_BREAK).orElse(false)) {
return true;
}
if (plot.hasOwner()) {
@ -210,12 +210,7 @@ public abstract class EventUtil {
notifyPerms);
}
Optional<HashSet<PlotBlock>> flagValue = plot.getFlag(Flags.USE);
HashSet<PlotBlock> value;
if (flagValue.isPresent()) {
value = flagValue.get();
} else {
value = null;
}
HashSet<PlotBlock> value = flagValue.orElse(null);
if (value == null || !PlotBlock.containsEverything(value) && !value
.contains(block.getPlotBlock())) {
return Permissions
@ -235,12 +230,7 @@ public abstract class EventUtil {
.hasPermission(player, C.PERMISSION_ADMIN_BUILD_UNOWNED.s(), notifyPerms);
}
Optional<HashSet<PlotBlock>> flagValue = plot.getFlag(Flags.PLACE);
HashSet<PlotBlock> value;
if (flagValue.isPresent()) {
value = flagValue.get();
} else {
value = null;
}
HashSet<PlotBlock> value = flagValue.orElse(null);
if (value == null || !PlotBlock.containsEverything(value) && !value
.contains(block.getPlotBlock())) {
if (Permissions
@ -261,16 +251,11 @@ public abstract class EventUtil {
return Permissions
.hasPermission(player, C.PERMISSION_ADMIN_INTERACT_UNOWNED.s(), false);
}
if (plot.getFlag(Flags.DEVICE_INTERACT).or(false)) {
if (plot.getFlag(Flags.DEVICE_INTERACT).orElse(false)) {
return true;
}
Optional<HashSet<PlotBlock>> flagValue = plot.getFlag(Flags.USE);
HashSet<PlotBlock> value;
if (flagValue.isPresent()) {
value = flagValue.get();
} else {
value = null;
}
HashSet<PlotBlock> value = flagValue.orElse(null);
if (value == null || !PlotBlock.containsEverything(value) && !value
.contains(block.getPlotBlock())) {
if (Permissions
@ -291,16 +276,11 @@ public abstract class EventUtil {
.hasPermission(player, C.PERMISSION_ADMIN_INTERACT_UNOWNED.s(),
notifyPerms);
}
if (plot.getFlag(Flags.HOSTILE_INTERACT).or(false)) {
if (plot.getFlag(Flags.HOSTILE_INTERACT).orElse(false)) {
return true;
}
Optional<HashSet<PlotBlock>> flagValue = plot.getFlag(Flags.USE);
HashSet<PlotBlock> value;
if (flagValue.isPresent()) {
value = flagValue.get();
} else {
value = null;
}
HashSet<PlotBlock> value = flagValue.orElse(null);
if (value == null || !PlotBlock.containsEverything(value) && !value
.contains(block.getPlotBlock())) {
if (Permissions
@ -322,16 +302,11 @@ public abstract class EventUtil {
.hasPermission(player, C.PERMISSION_ADMIN_INTERACT_UNOWNED.s(),
notifyPerms);
}
if (plot.getFlag(Flags.MISC_INTERACT).or(false)) {
if (plot.getFlag(Flags.MISC_INTERACT).orElse(false)) {
return true;
}
Optional<HashSet<PlotBlock>> flag = plot.getFlag(Flags.USE);
HashSet<PlotBlock> value;
if (flag.isPresent()) {
value = flag.get();
} else {
value = null;
}
HashSet<PlotBlock> value = flag.orElse(null);
if (value == null || !PlotBlock.containsEverything(value) && !value
.contains(block.getPlotBlock())) {
if (Permissions
@ -353,16 +328,11 @@ public abstract class EventUtil {
.hasPermission(player, C.PERMISSION_ADMIN_INTERACT_UNOWNED.s(),
notifyPerms);
}
if (plot.getFlag(Flags.VEHICLE_USE).or(false)) {
if (plot.getFlag(Flags.VEHICLE_USE).orElse(false)) {
return true;
}
Optional<HashSet<PlotBlock>> flag = plot.getFlag(Flags.USE);
HashSet<PlotBlock> value;
if (flag.isPresent()) {
value = flag.get();
} else {
value = null;
}
HashSet<PlotBlock> value = flag.orElse(null);
if (value == null || !PlotBlock.containsEverything(value) && !value
.contains(block.getPlotBlock())) {
if (Permissions
@ -384,16 +354,11 @@ public abstract class EventUtil {
.hasPermission(player, C.PERMISSION_ADMIN_INTERACT_UNOWNED.s(),
notifyPerms);
}
if (plot.getFlag(Flags.MOB_PLACE).or(false)) {
if (plot.getFlag(Flags.MOB_PLACE).orElse(false)) {
return true;
}
Optional<HashSet<PlotBlock>> flagValue = plot.getFlag(Flags.PLACE);
HashSet<PlotBlock> value;
if (flagValue.isPresent()) {
value = flagValue.get();
} else {
value = null;
}
HashSet<PlotBlock> value = flagValue.orElse(null);
if (value == null || !PlotBlock.containsEverything(value) && !value
.contains(block.getPlotBlock())) {
if (Permissions
@ -417,16 +382,11 @@ public abstract class EventUtil {
.hasPermission(player, C.PERMISSION_ADMIN_INTERACT_UNOWNED.s(),
notifyPerms);
}
if (plot.getFlag(Flags.MISC_PLACE).or(false)) {
if (plot.getFlag(Flags.MISC_PLACE).orElse(false)) {
return true;
}
Optional<HashSet<PlotBlock>> flag = plot.getFlag(Flags.PLACE);
HashSet<PlotBlock> value;
if (flag.isPresent()) {
value = flag.get();
} else {
value = null;
}
HashSet<PlotBlock> value = flag.orElse(null);
if (value == null || !PlotBlock.containsEverything(value) && !value
.contains(block.getPlotBlock())) {
if (Permissions
@ -449,16 +409,11 @@ public abstract class EventUtil {
.hasPermission(player, C.PERMISSION_ADMIN_INTERACT_UNOWNED.s(),
notifyPerms);
}
if (plot.getFlag(Flags.VEHICLE_PLACE).or(false)) {
if (plot.getFlag(Flags.VEHICLE_PLACE).orElse(false)) {
return true;
}
Optional<HashSet<PlotBlock>> flag = plot.getFlag(Flags.PLACE);
HashSet<PlotBlock> value;
if (flag.isPresent()) {
value = flag.get();
} else {
value = null;
}
HashSet<PlotBlock> value = flag.orElse(null);
if (value == null || !PlotBlock.containsEverything(value) && !value
.contains(block.getPlotBlock())) {
if (Permissions

View File

@ -10,7 +10,6 @@ import com.github.intellectualsites.plotsquared.plot.flag.Flags;
import com.github.intellectualsites.plotsquared.plot.object.*;
import com.github.intellectualsites.plotsquared.plot.object.stream.AbstractDelegateOutputStream;
import com.github.intellectualsites.plotsquared.plot.util.expiry.ExpireManager;
import com.google.common.base.Optional;
import java.io.*;
import java.net.HttpURLConnection;

View File

@ -12,7 +12,6 @@ import com.github.intellectualsites.plotsquared.plot.util.MainUtil;
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.google.common.base.Optional;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

View File

@ -8,7 +8,6 @@ import com.github.intellectualsites.plotsquared.plot.object.Plot;
import com.github.intellectualsites.plotsquared.plot.object.RunnableVal;
import com.github.intellectualsites.plotsquared.plot.util.MathMan;
import com.github.intellectualsites.plotsquared.plot.util.TaskManager;
import com.google.common.base.Optional;
import java.lang.reflect.Array;
import java.util.*;

View File

@ -9,12 +9,12 @@ import com.github.intellectualsites.plotsquared.plot.object.PlotBlock;
import com.github.intellectualsites.plotsquared.plot.object.PlotId;
import com.github.intellectualsites.plotsquared.plot.util.EventUtil;
import com.github.intellectualsites.plotsquared.plot.util.EventUtilTest;
import com.google.common.base.Optional;
import org.junit.Before;
import org.junit.Test;
import java.util.Collection;
import java.util.HashSet;
import java.util.Optional;
import java.util.UUID;
import static org.junit.Assert.assertEquals;
@ -39,9 +39,7 @@ public class FlagTest {
testBlock = PlotBlock.get((short) 1, (byte) 0);
flag.get().add(testBlock);
}
if (flag.isPresent()) {
System.out.println(Flags.USE.valueToString(flag.get()));
}
flag.ifPresent(collection -> System.out.println(Flags.USE.valueToString(collection)));
Optional<HashSet<PlotBlock>> flag2 = plot.getFlag(Flags.USE);
if (flag2.isPresent()) {
// assertThat(flag2.get(), (Matcher<? super HashSet<PlotBlock>>) IsCollectionContaining.hasItem(testBlock));