Implement PriceFlag

This commit is contained in:
Hannes Greule 2020-02-17 18:06:22 +01:00
parent 06c6628274
commit 085dfbf735
6 changed files with 28 additions and 22 deletions

View File

@ -3,7 +3,7 @@ package com.github.intellectualsites.plotsquared.plot.commands;
import com.github.intellectualsites.plotsquared.commands.Command; import com.github.intellectualsites.plotsquared.commands.Command;
import com.github.intellectualsites.plotsquared.commands.CommandDeclaration; import com.github.intellectualsites.plotsquared.commands.CommandDeclaration;
import com.github.intellectualsites.plotsquared.plot.config.Captions; import com.github.intellectualsites.plotsquared.plot.config.Captions;
import com.github.intellectualsites.plotsquared.plot.flag.Flags; import com.github.intellectualsites.plotsquared.plot.flags.implementations.PriceFlag;
import com.github.intellectualsites.plotsquared.plot.object.Plot; import com.github.intellectualsites.plotsquared.plot.object.Plot;
import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer; import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer;
import com.github.intellectualsites.plotsquared.plot.object.RunnableVal2; import com.github.intellectualsites.plotsquared.plot.object.RunnableVal2;
@ -12,7 +12,6 @@ import com.github.intellectualsites.plotsquared.plot.util.EconHandler;
import com.github.intellectualsites.plotsquared.plot.util.MainUtil; import com.github.intellectualsites.plotsquared.plot.util.MainUtil;
import com.github.intellectualsites.plotsquared.plot.util.UUIDHandler; import com.github.intellectualsites.plotsquared.plot.util.UUIDHandler;
import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
@ -41,11 +40,10 @@ import java.util.concurrent.CompletableFuture;
Set<Plot> plots = plot.getConnectedPlots(); Set<Plot> plots = plot.getConnectedPlots();
checkTrue(player.getPlotCount() + plots.size() <= player.getAllowedPlots(), checkTrue(player.getPlotCount() + plots.size() <= player.getAllowedPlots(),
Captions.CANT_CLAIM_MORE_PLOTS); Captions.CANT_CLAIM_MORE_PLOTS);
Optional<Double> flag = plot.getFlag(Flags.PRICE); double price = plot.getFlag(PriceFlag.class);
if (!flag.isPresent()) { if (price <= 0) {
throw new CommandException(Captions.NOT_FOR_SALE); throw new CommandException(Captions.NOT_FOR_SALE);
} }
final double price = flag.get();
checkTrue(player.getMoney() >= price, Captions.CANNOT_AFFORD_PLOT); checkTrue(player.getMoney() >= price, Captions.CANNOT_AFFORD_PLOT);
player.withdraw(price); player.withdraw(price);
// Failure // Failure
@ -58,7 +56,7 @@ import java.util.concurrent.CompletableFuture;
if (owner != null) { if (owner != null) {
Captions.PLOT_SOLD.send(owner, plot.getId(), player.getName(), price); Captions.PLOT_SOLD.send(owner, plot.getId(), player.getName(), price);
} }
plot.removeFlag(Flags.PRICE); plot.removeFlag(PriceFlag.class);
plot.setOwner(player.getUUID()); plot.setOwner(player.getUUID());
Captions.CLAIMED.send(player); Captions.CLAIMED.send(player);
whenDone.run(Buy.this, CommandResult.SUCCESS); whenDone.run(Buy.this, CommandResult.SUCCESS);

View File

@ -4,8 +4,8 @@ import com.github.intellectualsites.plotsquared.commands.CommandDeclaration;
import com.github.intellectualsites.plotsquared.plot.PlotSquared; import com.github.intellectualsites.plotsquared.plot.PlotSquared;
import com.github.intellectualsites.plotsquared.plot.PlotSquared.SortType; import com.github.intellectualsites.plotsquared.plot.PlotSquared.SortType;
import com.github.intellectualsites.plotsquared.plot.config.Captions; import com.github.intellectualsites.plotsquared.plot.config.Captions;
import com.github.intellectualsites.plotsquared.plot.flag.Flags;
import com.github.intellectualsites.plotsquared.plot.flags.implementations.DoneFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.DoneFlag;
import com.github.intellectualsites.plotsquared.plot.flags.implementations.PriceFlag;
import com.github.intellectualsites.plotsquared.plot.object.Plot; import com.github.intellectualsites.plotsquared.plot.object.Plot;
import com.github.intellectualsites.plotsquared.plot.object.PlotArea; import com.github.intellectualsites.plotsquared.plot.object.PlotArea;
import com.github.intellectualsites.plotsquared.plot.object.PlotMessage; import com.github.intellectualsites.plotsquared.plot.object.PlotMessage;
@ -25,7 +25,6 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Optional;
import java.util.UUID; import java.util.UUID;
@CommandDeclaration(command = "list", aliases = {"l", "find", "search"}, description = "List plots", @CommandDeclaration(command = "list", aliases = {"l", "find", "search"}, description = "List plots",
@ -248,8 +247,7 @@ public class ListCmd extends SubCommand {
} }
plots = new ArrayList<>(); plots = new ArrayList<>();
for (Plot plot : PlotSquared.get().getPlots()) { for (Plot plot : PlotSquared.get().getPlots()) {
Optional<Double> price = plot.getFlag(Flags.PRICE); if (plot.getFlag(PriceFlag.class) > 0) {
if (price.isPresent()) {
plots.add(plot); plots.add(plot);
} }
} }

View File

@ -609,6 +609,7 @@ public enum Captions implements Caption {
FLAG_DESCRIPTION_NOTIFY_LEAVE("Set to `true` to notify the plot owners when someone leaves the plot.", "Flags"), FLAG_DESCRIPTION_NOTIFY_LEAVE("Set to `true` to notify the plot owners when someone leaves the plot.", "Flags"),
FLAG_DESCRIPTION_NO_WORLDEDIT("Set to `true` to disable WorldEdit usage within the plot.", "Flags"), FLAG_DESCRIPTION_NO_WORLDEDIT("Set to `true` to disable WorldEdit usage within the plot.", "Flags"),
FLAG_DESCRIPTION_PLAYER_INTERACT("Set to `true` to allow guests to interact with players in the plot.", "Flags"), FLAG_DESCRIPTION_PLAYER_INTERACT("Set to `true` to allow guests to interact with players in the plot.", "Flags"),
FLAG_DESCRIPTION_PRICE("Set a price for a plot. Must be a positive decimal number.", "Flags"),
FLAG_DESCRIPTION_PVE("Set to `true` to enable PVE inside the plot.", "Flags"), FLAG_DESCRIPTION_PVE("Set to `true` to enable PVE inside the plot.", "Flags"),
FLAG_DESCRIPTION_PVP("Set to `true` to enable PVP inside the plot.", "Flags"), FLAG_DESCRIPTION_PVP("Set to `true` to enable PVP inside the plot.", "Flags"),
FLAG_DESCRIPTION_REDSTONE("Set to `false` to disable redstone in the plot.", "Flags"), FLAG_DESCRIPTION_REDSTONE("Set to `false` to disable redstone in the plot.", "Flags"),

View File

@ -6,21 +6,9 @@ import com.github.intellectualsites.plotsquared.plot.util.MathMan;
public final class Flags { public final class Flags {
public static final IntervalFlag FEED = new IntervalFlag("feed");
public static final IntervalFlag HEAL = new IntervalFlag("heal");
public static final GameModeFlag GAMEMODE = new GameModeFlag("gamemode"); public static final GameModeFlag GAMEMODE = new GameModeFlag("gamemode");
public static final GameModeFlag GUEST_GAMEMODE = new GameModeFlag("guest-gamemode"); public static final GameModeFlag GUEST_GAMEMODE = new GameModeFlag("guest-gamemode");
public static final LongFlag TIME = new LongFlag("time"); public static final LongFlag TIME = new LongFlag("time");
public static final DoubleFlag PRICE = new DoubleFlag("price") {
@Override public Double parseValue(String input) {
Double value = super.parseValue(input);
return value != null && value > 0 ? value : null;
}
@Override public String getValueDescription() {
return Captions.FLAG_ERROR_DOUBLE.getTranslated();
}
};
public static final StringListFlag BLOCKED_CMDS = new StringListFlag("blocked-cmds"); public static final StringListFlag BLOCKED_CMDS = new StringListFlag("blocked-cmds");
public static final BlockStateListFlag USE = new BlockStateListFlag("use"); public static final BlockStateListFlag USE = new BlockStateListFlag("use");
public static final BlockStateListFlag BREAK = new BlockStateListFlag("break"); public static final BlockStateListFlag BREAK = new BlockStateListFlag("break");

View File

@ -46,6 +46,7 @@ import com.github.intellectualsites.plotsquared.plot.flags.implementations.Notif
import com.github.intellectualsites.plotsquared.plot.flags.implementations.NotifyLeaveFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.NotifyLeaveFlag;
import com.github.intellectualsites.plotsquared.plot.flags.implementations.PlayerInteractFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.PlayerInteractFlag;
import com.github.intellectualsites.plotsquared.plot.flags.implementations.PlotWeatherFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.PlotWeatherFlag;
import com.github.intellectualsites.plotsquared.plot.flags.implementations.PriceFlag;
import com.github.intellectualsites.plotsquared.plot.flags.implementations.PveFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.PveFlag;
import com.github.intellectualsites.plotsquared.plot.flags.implementations.PvpFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.PvpFlag;
import com.github.intellectualsites.plotsquared.plot.flags.implementations.RedstoneFlag; import com.github.intellectualsites.plotsquared.plot.flags.implementations.RedstoneFlag;
@ -148,6 +149,9 @@ public final class GlobalFlagContainer extends FlagContainer {
this.addFlag(FeedFlag.FEED_NOTHING); this.addFlag(FeedFlag.FEED_NOTHING);
this.addFlag(HealFlag.HEAL_NOTHING); this.addFlag(HealFlag.HEAL_NOTHING);
// Double flags
this.addFlag(PriceFlag.PRICE_NOT_BUYABLE);
// Internal flags // Internal flags
this.addFlag(new AnalysisFlag(Collections.emptyList())); this.addFlag(new AnalysisFlag(Collections.emptyList()));
this.addFlag(new DoneFlag("")); this.addFlag(new DoneFlag(""));

View File

@ -0,0 +1,17 @@
package com.github.intellectualsites.plotsquared.plot.flags.implementations;
import com.github.intellectualsites.plotsquared.plot.config.Captions;
import com.github.intellectualsites.plotsquared.plot.flags.types.DoubleFlag;
import org.jetbrains.annotations.NotNull;
public class PriceFlag extends DoubleFlag<PriceFlag> {
public static final PriceFlag PRICE_NOT_BUYABLE = new PriceFlag(0D);
protected PriceFlag(@NotNull Double value) {
super(value, Double.MIN_NORMAL, Double.MAX_VALUE, Captions.FLAG_DESCRIPTION_PRICE);
}
@Override protected PriceFlag flagOf(@NotNull Double value) {
return new PriceFlag(value);
}
}