ill try smth
This commit is contained in:
parent
77decae10c
commit
a365f70873
28 changed files with 433 additions and 96 deletions
|
|
@ -5,15 +5,30 @@ import de.kentoj.scrow.bukkit.minigame.PlayerManager;
|
|||
import de.kentoj.scrow.bukkit.minigame.phaseflow.LinearPhaseFlow;
|
||||
import de.kentoj.scrowlib.convention.ScrowMessageStyle;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import site.lab0x13.scrow.extraction.config.ArenaConfig;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
/*
|
||||
While in the network lobby, players select a kit. then when enough players are found,
|
||||
players are spread on a map and are tasked to extract some tokens.
|
||||
extracting a token gives you points, items and a temporary health and speed-boost as well as temporary
|
||||
wallhacks. extracting a token takes a while and other players want to stop you from doing so, so you'll
|
||||
need to fight.
|
||||
*/
|
||||
|
||||
public class ExtractionGame implements Minigame {
|
||||
|
||||
private final ScrowMessageStyle style = new ScrowMessageStyle("PotatoRun", TextColor.color(0x8D7726));
|
||||
private final PlayerManager playerManager = new PlayerManager(this);
|
||||
private final LinearPhaseFlow phaseFlow = new LinearPhaseFlow("potatoRun.root");
|
||||
|
||||
private final ArenaConfig arenaConfig;
|
||||
|
||||
public ExtractionGame(ArenaConfig arenaConfig) {
|
||||
this.arenaConfig = arenaConfig;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int minParticipants() {
|
||||
return 0;
|
||||
|
|
@ -37,4 +52,8 @@ public class ExtractionGame implements Minigame {
|
|||
public PlayerManager playerManager() {
|
||||
return playerManager;
|
||||
}
|
||||
|
||||
public ArenaConfig arenaConfig() {
|
||||
return arenaConfig;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,29 +1,23 @@
|
|||
package site.lab0x13.scrow.extraction;
|
||||
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import site.lab0x13.scrow.configurator.configfile.ConfigFile;
|
||||
import site.lab0x13.scrow.configurator.configfile.ConfigFileRegistry;
|
||||
import site.lab0x13.scrow.extraction.config.ExtractionConfig;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class ExtractionPlugin extends JavaPlugin {
|
||||
|
||||
private final Logger log = LoggerFactory.getLogger(ExtractionPlugin.class);
|
||||
private static final Logger log = LoggerFactory.getLogger(ExtractionPlugin.class);
|
||||
private static Plugin _plugin;
|
||||
|
||||
public static Plugin plugin() {
|
||||
return _plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
log.info("loading");
|
||||
var cfg = ConfigFile.of(this.getDataPath().resolve("config.json"));
|
||||
ConfigFileRegistry.get().register(cfg);
|
||||
cfg.registerStorable("arenas", ExtractionConfig.arenas);
|
||||
try {
|
||||
cfg.load();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
log.info("loaded");
|
||||
_plugin = this;
|
||||
ExtractionConfig.registerAndLoad(this);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
package site.lab0x13.scrow.extraction.compass;
|
||||
|
||||
import org.bukkit.Location;
|
||||
|
||||
public interface CompassTarget {
|
||||
|
||||
String targetName();
|
||||
|
||||
Location location();
|
||||
}
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
package site.lab0x13.scrow.extraction.compass;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
|
||||
public class CompassTargetRegistry {
|
||||
|
||||
private final Map<UUID, CompassTarget> targets = new HashMap<>();
|
||||
|
||||
public void targetNearest(Player player, List<? extends CompassTarget> targetList) {
|
||||
var nearest = findNearest(player.getLocation(), targetList);
|
||||
targets.put(player.getUniqueId(), nearest);
|
||||
}
|
||||
|
||||
public void setCompassTarget(Player player, @Nullable CompassTarget compassTarget) {
|
||||
if (compassTarget == null)
|
||||
targets.remove(player.getUniqueId());
|
||||
else
|
||||
targets.put(player.getUniqueId(), compassTarget);
|
||||
}
|
||||
|
||||
public @Nullable CompassTarget getCompassTarget(Player player) {
|
||||
return targets.get(player.getUniqueId());
|
||||
}
|
||||
|
||||
private static CompassTarget findNearest(Location location, List<? extends CompassTarget> targetList) {
|
||||
checkArgument(!targetList.isEmpty(), "targetList is empty");
|
||||
|
||||
CompassTarget nearest = targetList.getFirst();
|
||||
double nearestDistanceSquared = Double.MAX_VALUE;
|
||||
for (var target : targetList) {
|
||||
var targetDistanceSquared = target.location().distanceSquared(location);
|
||||
if (targetDistanceSquared < nearestDistanceSquared) {
|
||||
nearestDistanceSquared = targetDistanceSquared;
|
||||
nearest = target;
|
||||
}
|
||||
}
|
||||
return nearest;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,11 +1,15 @@
|
|||
package site.lab0x13.scrow.extraction.config;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.Location;
|
||||
import site.lab0x13.scrow.configurator.storable.Storable;
|
||||
import site.lab0x13.scrow.extraction.extractionpoint.ExtractionPoint;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public record ArenaConfig(
|
||||
List<Storable<Location>> spawnLocations
|
||||
String id,
|
||||
Component name,
|
||||
List<Location> spawnLocations,
|
||||
List<ExtractionPoint> extractionPoints
|
||||
) {
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,18 +3,28 @@ package site.lab0x13.scrow.extraction.config;
|
|||
import site.lab0x13.scrow.configurator.storable.complex.ComplexStorable;
|
||||
import site.lab0x13.scrow.configurator.storable.complex.FieldReader;
|
||||
import site.lab0x13.scrow.configurator.storable.complex.FieldRegistry;
|
||||
import site.lab0x13.scrow.configurator.storable.impl.list.ListStorable;
|
||||
import site.lab0x13.scrow.configurator.storable.impl.MiniMessageStorable;
|
||||
import site.lab0x13.scrow.configurator.storable.list.ListStorable;
|
||||
import site.lab0x13.scrow.configurator.storable.impl.location.LocationStorable;
|
||||
import site.lab0x13.scrow.configurator.storable.primitive.StringStorable;
|
||||
|
||||
public class ArenaConfigStorable extends ComplexStorable<ArenaConfig> {
|
||||
|
||||
@Override
|
||||
protected void registerFields(FieldRegistry<ArenaConfig> registry) {
|
||||
registry.registerField("id", new StringStorable(), ArenaConfig::id);
|
||||
registry.registerField("name", new MiniMessageStorable(), ArenaConfig::name);
|
||||
registry.registerField("spawnLocations", new ListStorable<>(LocationStorable::new), ArenaConfig::spawnLocations);
|
||||
registry.registerField("extractionPoints", new ListStorable<>(ExtractionPointStorable::new), ArenaConfig::extractionPoints);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ArenaConfig construct(FieldReader<ArenaConfig> reader) {
|
||||
return new ArenaConfig(reader.read("spawnLocations"));
|
||||
return new ArenaConfig(
|
||||
reader.read("id"),
|
||||
reader.read("name"),
|
||||
reader.read("spawnLocations"),
|
||||
reader.read("extractionPoints")
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,32 @@
|
|||
package site.lab0x13.scrow.extraction.config;
|
||||
|
||||
import site.lab0x13.scrow.configurator.storable.impl.list.ListStorable;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
import site.lab0x13.scrow.configurator.configfile.ConfigFile;
|
||||
import site.lab0x13.scrow.configurator.configfile.ConfigFileRegistry;
|
||||
import site.lab0x13.scrow.configurator.storable.impl.DurationStorable;
|
||||
import site.lab0x13.scrow.configurator.storable.list.ListStorable;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public final class ExtractionConfig {
|
||||
private ExtractionConfig() {
|
||||
}
|
||||
|
||||
public static final ListStorable<ArenaConfigStorable> arenas = new ListStorable<>(ArenaConfigStorable::new);
|
||||
public static final ListStorable<ArenaConfig> arenas = new ListStorable<>(ArenaConfigStorable::new);
|
||||
public static final DurationStorable extractionDuration = new DurationStorable();
|
||||
|
||||
public static void registerAndLoad(Plugin plugin) {
|
||||
var configFile = ConfigFile.of(plugin.getDataPath().resolve("config.json"));
|
||||
ConfigFileRegistry.get().register(configFile);
|
||||
{
|
||||
configFile.registerStorable("arenas", arenas);
|
||||
configFile.registerStorable("extractionDuration", extractionDuration);
|
||||
}
|
||||
try {
|
||||
configFile.load();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("failed loading config", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,19 @@
|
|||
package site.lab0x13.scrow.extraction.config;
|
||||
|
||||
import site.lab0x13.scrow.configurator.storable.complex.ComplexStorable;
|
||||
import site.lab0x13.scrow.configurator.storable.complex.FieldReader;
|
||||
import site.lab0x13.scrow.configurator.storable.complex.FieldRegistry;
|
||||
import site.lab0x13.scrow.configurator.storable.impl.location.LocationStorable;
|
||||
import site.lab0x13.scrow.extraction.extractionpoint.ExtractionPoint;
|
||||
|
||||
public class ExtractionPointStorable extends ComplexStorable<ExtractionPoint> {
|
||||
@Override
|
||||
protected void registerFields(FieldRegistry<ExtractionPoint> registry) {
|
||||
registry.registerField("location", new LocationStorable(), ExtractionPoint::location);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ExtractionPoint construct(FieldReader<ExtractionPoint> reader) {
|
||||
return new ExtractionPoint(reader.read("location"));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
package site.lab0x13.scrow.extraction.extractionpoint;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Player;
|
||||
import site.lab0x13.scrow.extraction.compass.CompassTarget;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
public final class ExtractionPoint implements CompassTarget {
|
||||
private final Location location;
|
||||
private final Map<UUID, ExtractionProgress> extractionProgress = new HashMap<>();
|
||||
|
||||
public ExtractionPoint(Location location) {
|
||||
this.location = location;
|
||||
}
|
||||
|
||||
public ExtractionProgress getProgress(Player player) {
|
||||
return extractionProgress.computeIfAbsent(player.getUniqueId(), _ -> new ExtractionProgress());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String targetName() {
|
||||
return "Extraction Point";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Location location() {
|
||||
return location;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,89 @@
|
|||
package site.lab0x13.scrow.extraction.extractionpoint;
|
||||
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
import site.lab0x13.scrow.extraction.ExtractionPlugin;
|
||||
import site.lab0x13.scrow.extraction.config.ExtractionConfig;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import static java.util.Objects.requireNonNull;
|
||||
|
||||
/**
|
||||
* You extract by repeatedly calling {@link ExtractionProgress#advanceExtraction()}
|
||||
* with little delay in between.
|
||||
* If the delay is too long, progress is reset.
|
||||
*/
|
||||
public class ExtractionProgress {
|
||||
|
||||
/**
|
||||
* ticks(= 50ms) after which progress will be reset if {@link ExtractionProgress#advanceExtraction()}
|
||||
* isn't called.
|
||||
*/
|
||||
private static final int MAX_DELAY_TICKS = 2;
|
||||
|
||||
private final List<Consumer<Double>> changeSubscribers = new ArrayList<>();
|
||||
private final long requiredMs;
|
||||
/**
|
||||
* System.currentTimeMillis() at first click (in this attempt to extract)
|
||||
* or < 0 if extracting hasn't been attempted recently.
|
||||
*/
|
||||
private long clickingSince = -1;
|
||||
/**
|
||||
* System.currentTimeMillis() at last click
|
||||
*/
|
||||
private long lastClickMs = 0;
|
||||
|
||||
/**
|
||||
* @param extractionTime duration for which progress must be called repeatedly
|
||||
*/
|
||||
public ExtractionProgress(Duration extractionTime) {
|
||||
this.requiredMs = extractionTime.toMillis();
|
||||
}
|
||||
|
||||
public ExtractionProgress() {
|
||||
this(requireNonNull(ExtractionConfig.extractionDuration.value()));
|
||||
}
|
||||
|
||||
public void advanceExtraction() {
|
||||
if (clickingSince < 0)
|
||||
clickingSince = System.currentTimeMillis();
|
||||
lastClickMs = System.currentTimeMillis();
|
||||
notifyChangeSubscribers();
|
||||
|
||||
/* reset if not called again for a while */
|
||||
new BukkitRunnable() {
|
||||
final long lastClickMsAtStart = lastClickMs;
|
||||
@Override
|
||||
public void run() {
|
||||
if (lastClickMsAtStart == lastClickMs)
|
||||
resetProgress();
|
||||
}
|
||||
}.runTaskLater(ExtractionPlugin.plugin(), MAX_DELAY_TICKS);
|
||||
}
|
||||
|
||||
public double progress() {
|
||||
if (lastClickMs == 0)
|
||||
return 0;
|
||||
var delta = System.currentTimeMillis() - requiredMs;
|
||||
return Math.max(1.0, (double) delta / (double) requiredMs);
|
||||
}
|
||||
|
||||
public void resetProgress() {
|
||||
clickingSince = -1;
|
||||
lastClickMs = 0;
|
||||
notifyChangeSubscribers();
|
||||
}
|
||||
|
||||
public void subscribeChange(Consumer<Double> onChange) {
|
||||
changeSubscribers.add(onChange);
|
||||
}
|
||||
|
||||
private void notifyChangeSubscribers() {
|
||||
var progress = progress();
|
||||
changeSubscribers.forEach(sub -> sub.accept(progress));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
package site.lab0x13.scrow.extraction.phase;
|
||||
|
||||
import de.kentoj.scrow.bukkit.minigame.phase.Phase;
|
||||
import de.kentoj.scrow.bukkit.minigame.phaseflow.PhaseContext;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import site.lab0x13.scrow.extraction.ExtractionGame;
|
||||
import site.lab0x13.scrow.extraction.compass.CompassTargetRegistry;
|
||||
|
||||
public class MidgamePhase extends Phase<ExtractionGame> {
|
||||
|
||||
private final CompassTargetRegistry compassTargetRegistry;
|
||||
|
||||
protected MidgamePhase(PhaseContext<ExtractionGame> ctx, CompassTargetRegistry compassTargetRegistry) {
|
||||
super(ctx);
|
||||
this.compassTargetRegistry = compassTargetRegistry;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStart() {
|
||||
ctx.players().participants().forEach(player -> {
|
||||
compassTargetRegistry.targetNearest(player, ctx.game().arenaConfig().extractionPoints());
|
||||
player.getInventory().addItem(ItemStack.of(Material.COMPASS));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCancel() {
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue