added shrinking border appliance
This commit is contained in:
parent
d1b5d81fa7
commit
9bae26044a
@ -0,0 +1,97 @@
|
||||
package eu.mhsl.craftattack.spawn.varo.appliances.metaGameplay.shrinkingBorder;
|
||||
|
||||
import eu.mhsl.craftattack.core.Main;
|
||||
import eu.mhsl.craftattack.core.appliance.Appliance;
|
||||
import eu.mhsl.craftattack.core.appliance.ApplianceCommand;
|
||||
import eu.mhsl.craftattack.core.config.Configuration;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.WorldBorder;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.time.LocalTime;
|
||||
import java.util.*;
|
||||
|
||||
public class ShrinkingBorder extends Appliance {
|
||||
private final String shrinkPerDayKey = "shrinkPerDay";
|
||||
private final String shrinkTimeKey = "shrinkTime";
|
||||
private final String minimumSizeKey = "minimumSize";
|
||||
public int shrinkPerDay;
|
||||
public LocalTime shrinkTime;
|
||||
public int minimumSize;
|
||||
private final Timer timer = new Timer();
|
||||
private final Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
|
||||
|
||||
private final WorldBorder overworldBorder = Bukkit.getWorlds().stream()
|
||||
.filter(world -> world.getEnvironment().equals(World.Environment.NORMAL))
|
||||
.findFirst().orElseThrow().getWorldBorder();
|
||||
private final WorldBorder netherBorder = Bukkit.getWorlds().stream()
|
||||
.filter(world -> world.getEnvironment().equals(World.Environment.NETHER))
|
||||
.findFirst().orElseThrow().getWorldBorder();
|
||||
|
||||
private class ShrinkBorderTask extends TimerTask {
|
||||
@Override
|
||||
public void run() {
|
||||
Bukkit.getScheduler().runTask(Main.instance(), ShrinkingBorder.this::shrinkBorder);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public ShrinkingBorder() {
|
||||
super("shrinkingBorder");
|
||||
this.shrinkPerDay = this.localConfig().getInt(this.shrinkPerDayKey, 10);
|
||||
this.shrinkTime = LocalTime.parse(this.localConfig().getString(this.shrinkTimeKey, LocalTime.of(3, 0, 0).toString()));
|
||||
this.minimumSize = this.localConfig().getInt(this.minimumSizeKey, 10);
|
||||
|
||||
this.calendar.set(Calendar.HOUR_OF_DAY, this.shrinkTime.getHour());
|
||||
this.calendar.set(Calendar.MINUTE, this.shrinkTime.getMinute());
|
||||
this.calendar.set(Calendar.SECOND, this.shrinkTime.getSecond());
|
||||
|
||||
if(this.calendar.getTime().before(new Date(System.currentTimeMillis()))) {
|
||||
this.calendar.add(Calendar.DAY_OF_MONTH, 1);
|
||||
}
|
||||
|
||||
this.timer.schedule(new ShrinkBorderTask(), this.calendar.getTime());
|
||||
}
|
||||
|
||||
private void shrinkBorder() {
|
||||
this.overworldBorder.setSize(Math.max(this.overworldBorder.getSize()-this.shrinkPerDay, this.minimumSize));
|
||||
this.netherBorder.setSize(Math.max(this.netherBorder.getSize()-this.shrinkPerDay, this.minimumSize));
|
||||
|
||||
this.calendar.add(Calendar.DAY_OF_MONTH, 1);
|
||||
this.timer.schedule(new ShrinkBorderTask(), this.calendar.getTime());
|
||||
}
|
||||
|
||||
public void changeOption(ShrinkingBorderCommand.Argument option, int value) {
|
||||
switch (option) {
|
||||
case MINIMUM_SIZE -> {
|
||||
this.minimumSize = value;
|
||||
this.localConfig().set(this.minimumSizeKey, value);
|
||||
}
|
||||
case SHRINK_PER_DAY -> {
|
||||
this.shrinkPerDay = value;
|
||||
this.localConfig().set(this.shrinkPerDayKey, value);
|
||||
}
|
||||
}
|
||||
Configuration.saveChanges();
|
||||
}
|
||||
|
||||
public int getOption(ShrinkingBorderCommand.Argument option) {
|
||||
switch (option) {
|
||||
case MINIMUM_SIZE -> {
|
||||
return this.minimumSize;
|
||||
}
|
||||
case SHRINK_PER_DAY -> {
|
||||
return this.shrinkPerDay;
|
||||
}
|
||||
default -> {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @NotNull List<ApplianceCommand<?>> commands() {
|
||||
return List.of(new ShrinkingBorderCommand());
|
||||
}
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
package eu.mhsl.craftattack.spawn.varo.appliances.metaGameplay.shrinkingBorder;
|
||||
|
||||
import eu.mhsl.craftattack.core.appliance.ApplianceCommand;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class ShrinkingBorderCommand extends ApplianceCommand<ShrinkingBorder> {
|
||||
public enum Argument {
|
||||
SHRINK_PER_DAY("shrinkPerDay"),
|
||||
MINIMUM_SIZE("minimumSize");
|
||||
|
||||
public final String name;
|
||||
|
||||
Argument(String friendlyName) {
|
||||
this.name = friendlyName;
|
||||
}
|
||||
|
||||
static Argument fromString(String argument) {
|
||||
return Arrays.stream(values()).filter(argument1 -> argument1.name.equals(argument)).findFirst().orElseThrow();
|
||||
}
|
||||
}
|
||||
|
||||
public ShrinkingBorderCommand() {
|
||||
super("shrinkingBorder");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void execute(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||
if(args.length < 1 || args.length > 2) throw new Error("shrinkingBorder command needs one or two arguments");
|
||||
if(Arrays.stream(Argument.values()).noneMatch(argument -> argument.name.equals(args[0]))) throw new Error(String.format("argument %s does not exist", args[0]));
|
||||
Argument option = Argument.fromString(args[0]);
|
||||
|
||||
switch (args.length) {
|
||||
case 1:
|
||||
sender.sendMessage(String.format("%s is currently set to %d", args[0], this.getAppliance().getOption(option)));
|
||||
break;
|
||||
case 2:
|
||||
try {
|
||||
int value = Integer.parseInt(args[1]);
|
||||
this.getAppliance().changeOption(option, value);
|
||||
sender.sendMessage(String.format("%s is now set to %d", args[0], this.getAppliance().getOption(option)));
|
||||
} catch (NumberFormatException e) {
|
||||
throw new Error("second argument has to be an integer");
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw new Error("shrinkingBorder command needs one or two arguments");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||
if(args.length > 1) return List.of();
|
||||
return Arrays.stream(Argument.values()).map(argument -> argument.name).toList();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user