Skip to content

Commit

Permalink
Placeholder additions and fix issues with storing/restoring in multip…
Browse files Browse the repository at this point in the history
…le places
  • Loading branch information
Redned235 committed Dec 11, 2024
1 parent c8fd344 commit 29bce0b
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import org.battleplugins.arena.ArenaPlayer;
import org.battleplugins.arena.BattleArena;
import org.battleplugins.arena.competition.Competition;
import org.battleplugins.arena.competition.LiveCompetition;
import org.battleplugins.arena.competition.map.CompetitionMap;
import org.battleplugins.arena.competition.map.LiveCompetitionMap;
import org.battleplugins.arena.competition.phase.CompetitionPhaseType;
import org.battleplugins.arena.resolver.Resolver;
import org.battleplugins.arena.resolver.ResolverKey;
Expand All @@ -13,6 +16,8 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class BattleArenaExpansion extends PlaceholderExpansion {
private final BattleArena plugin;

Expand Down Expand Up @@ -68,6 +73,31 @@ public BattleArenaExpansion(BattleArena plugin) {

// Remaining text in split array
String placeholder = String.join("_", split).substring(arenaName.length() + 1);
if (placeholder.startsWith("map")) {
placeholder = placeholder.substring("map_".length());

// Next value after map_ is the actual placeholder
String mapName = placeholder.split("_")[0];
List<Competition<?>> competitions = this.plugin.getCompetitions(arena, mapName);
if (competitions.isEmpty()) {
return null;
}

placeholder = placeholder.substring(mapName.length() + 1);

// Just get the first competition for now
Competition<?> competition = competitions.get(0);
if (!(competition instanceof LiveCompetition<?> liveCompetition)) {
return null;
}

Resolver resolver = liveCompetition.resolve();
ResolverKey<?> resolverKey = ResolverKeys.get(placeholder.replace("_", "-"));
if (resolverKey != null && resolver.has(resolverKey)) {
return resolver.resolveToString(resolverKey);
}
}

switch (placeholder) {
case "active_competitions": {
return String.valueOf(this.plugin.getCompetitions(arena).size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ public static Optional<ArenaPlayer> arenaPlayer(Player player) {
*/
@Nullable
public static ArenaPlayer getArenaPlayer(Player player) {
if (!player.hasMetadata(ARENA_PLAYER_META_KEY)) {
if (player == null || !player.hasMetadata(ARENA_PLAYER_META_KEY)) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public List<Competition<?>> getCompetitions(Arena arena) {
public List<Competition<?>> getCompetitions(Arena arena, String name) {
List<Competition<?>> competitions = this.getCompetitions(arena);
return competitions.stream()
.filter(competition -> competition.getMap().getName().equals(name))
.filter(competition -> competition.getMap().getName().equalsIgnoreCase(name))
.toList();
}

Expand Down Expand Up @@ -105,7 +105,7 @@ public CompletableFuture<CompetitionResult> getOrCreateCompetition(Arena arena,
continue;
}

if ((name == null || map.getName().equals(name))) {
if ((name == null || map.getName().equalsIgnoreCase(name))) {
Competition<?> competition = map.createDynamicCompetition(arena);
if (competition == null) {
this.plugin.warn("Failed to create dynamic competition for map {} in arena {}!", map.getName(), arena.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.BitSet;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -45,8 +46,8 @@ public class PlayerStorage {
private final Collection<PotionEffect> effects = new ArrayList<>();

private Location lastLocation;
private boolean stored;

private final BitSet stored = new BitSet();

public PlayerStorage(ArenaPlayer player) {
this.player = player;
Expand All @@ -58,15 +59,16 @@ public PlayerStorage(ArenaPlayer player) {
* @param toStore the types to store
*/
public void store(Set<Type> toStore, boolean clearState) {
if (this.stored) {
return;
}

for (Type type : toStore) {
if (this.stored.get(type.ordinal())) {
BattleArena.getInstance().warn("Type {} is already stored for player {}.", type, this.player.getPlayer().getName());
continue;
}

type.store(this);
this.stored.set(type.ordinal());
}

this.stored = true;
if (clearState) {
this.clearState(toStore);
}
Expand Down Expand Up @@ -147,26 +149,26 @@ private void storeLocation() {
* @param toRestore the types to restore
*/
public void restore(Set<Type> toRestore) {
if (!this.stored) {
return;
}

for (Type type : toRestore) {
if (!this.stored.get(type.ordinal())) {
BattleArena.getInstance().warn("Type {} is not stored for player {}.", type, this.player.getPlayer().getName());
continue;
}

type.restore(this);
this.stored.clear(type.ordinal());
}

// Reset everything we have in this class
this.inventory = null;
this.attributes.clear();
this.health = 0;
this.hunger = 0;
this.totalExp = 0;
this.exp = 0;
this.expLevels = 0;
this.effects.clear();
this.lastLocation = null;

this.stored = false;
if (toRestore.contains(Type.INVENTORY)) this.inventory = null;
if (toRestore.contains(Type.ATTRIBUTES)) this.attributes.clear();
if (toRestore.contains(Type.HEALTH)) this.health = 0;
if (toRestore.contains(Type.HEALTH)) this.hunger = 0;
if (toRestore.contains(Type.EXPERIENCE)) this.totalExp = 0;
if (toRestore.contains(Type.EXPERIENCE)) this.exp = 0;
if (toRestore.contains(Type.EXPERIENCE)) this.expLevels = 0;
if (toRestore.contains(Type.EFFECTS)) this.effects.clear();
if (toRestore.contains(Type.LOCATION)) this.lastLocation = null;
}

private void restoreAll() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.battleplugins.arena.competition.PlayerStorage;
import org.battleplugins.arena.resolver.Resolvable;

import java.util.Locale;
import java.util.Map;
import java.util.Set;

Expand All @@ -20,7 +21,7 @@ public void call(ArenaPlayer arenaPlayer, Resolvable resolvable) {
String[] types = this.get(TYPES_KEY).split(",");
PlayerStorage.Type[] toStore = new PlayerStorage.Type[types.length];
for (int i = 0; i < types.length; i++) {
toStore[i] = PlayerStorage.Type.valueOf(types[i].toUpperCase());
toStore[i] = PlayerStorage.Type.valueOf(types[i].toUpperCase(Locale.ROOT));
}

arenaPlayer.getStorage().restore(Set.of(toStore));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.battleplugins.arena.competition.PlayerStorage;
import org.battleplugins.arena.resolver.Resolvable;

import java.util.Locale;
import java.util.Map;
import java.util.Set;

Expand All @@ -21,7 +22,7 @@ public void call(ArenaPlayer arenaPlayer, Resolvable resolvable) {
String[] types = this.get(TYPES_KEY).split(",");
PlayerStorage.Type[] toStore = new PlayerStorage.Type[types.length];
for (int i = 0; i < types.length; i++) {
toStore[i] = PlayerStorage.Type.valueOf(types[i].toUpperCase());
toStore[i] = PlayerStorage.Type.valueOf(types[i].toUpperCase(Locale.ROOT));
}

boolean clearState = Boolean.parseBoolean(this.getOrDefault(CLEAR_STATE, "true"));
Expand Down
33 changes: 15 additions & 18 deletions plugin/src/main/java/org/battleplugins/arena/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import org.battleplugins.arena.BattleArena;
import org.battleplugins.arena.config.ArenaOption;
import org.battleplugins.arena.messages.Messages;
import org.bukkit.Bukkit;

import java.io.File;
import java.io.IOException;
Expand All @@ -12,7 +11,10 @@
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.security.MessageDigest;
import java.time.Duration;
import java.util.Arrays;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -174,7 +176,10 @@ public static void copyDirectories(File jarFile, Path outputPath, String directo
Path relativePath = directoryPath.relativize(path);
Path targetPath = outputPath.resolve(relativePath.toString());
if (Files.exists(targetPath)) {
return;
// Check hashes - if different, copy from jar
if (Arrays.equals(getHash(path), getHash(targetPath))) {
return;
}
}

for (String ignoredFile : ignoredFiles) {
Expand All @@ -185,7 +190,7 @@ public static void copyDirectories(File jarFile, Path outputPath, String directo

try {
Files.createDirectories(targetPath.getParent());
Files.copy(path, targetPath);
Files.copy(path, targetPath, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
BattleArena.getInstance().error("Failed to copy module {}!", path.getFileName(), e);
}
Expand All @@ -197,23 +202,15 @@ public static void copyDirectories(File jarFile, Path outputPath, String directo
}
}

public static String getNmsPackage() {
String NMS;
private static byte[] getHash(Path path) {
byte[] sha256;

try {
// This fails for versions without an NMS package
NMS = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3];
} catch (ArrayIndexOutOfBoundsException ex) {
NMS = createCompatPackage();
sha256 = MessageDigest.getInstance("SHA-256").digest(Files.readAllBytes(path));
} catch (Exception e) {
throw new RuntimeException("Could not calculate pack hash", e);
}
return NMS;
}

private static String createCompatPackage() {
String fullVersion = Bukkit.getBukkitVersion();
String[] parts = fullVersion.split("-");
String version = parts[0];
version = version.replace(".", "_");
version = "v" + version;
return version;
return sha256;
}
}

0 comments on commit 29bce0b

Please sign in to comment.