From 3def938fc20a99d35c7661e2fd3cd433224fbeb9 Mon Sep 17 00:00:00 2001 From: RednedEpic Date: Tue, 2 Jul 2024 22:59:30 -0500 Subject: [PATCH] Copy over fields for dynamic maps --- .../competition/map/LiveCompetitionMap.java | 11 +++++++++ .../battleplugins/arena/util/FieldUtil.java | 24 +++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 plugin/src/main/java/org/battleplugins/arena/util/FieldUtil.java diff --git a/plugin/src/main/java/org/battleplugins/arena/competition/map/LiveCompetitionMap.java b/plugin/src/main/java/org/battleplugins/arena/competition/map/LiveCompetitionMap.java index a56550f3..bb28faf8 100644 --- a/plugin/src/main/java/org/battleplugins/arena/competition/map/LiveCompetitionMap.java +++ b/plugin/src/main/java/org/battleplugins/arena/competition/map/LiveCompetitionMap.java @@ -12,6 +12,7 @@ import org.battleplugins.arena.config.ParseException; import org.battleplugins.arena.config.PostProcessable; import org.battleplugins.arena.util.BlockUtil; +import org.battleplugins.arena.util.FieldUtil; import org.battleplugins.arena.util.VoidChunkGenerator; import org.bukkit.Bukkit; import org.bukkit.World; @@ -68,6 +69,10 @@ public LiveCompetitionMap(String name, Arena arena, MapType type, String world, @Override public void postProcess() { + if (this.mapWorld != null) { + return; // Map was already set in createDynamicCompetition + } + this.mapWorld = Bukkit.getWorld(this.world); if (this.mapWorld == null) { throw new IllegalStateException("World " + this.world + " for map " + this.name + " in arena " + this.arena.getName() + " does not exist!"); @@ -242,7 +247,13 @@ public final Competition createDynamicCompetition(Arena arena) { } LiveCompetitionMap copy = arena.getMapFactory().create(this.name, arena, this.type, worldName, this.bounds, this.spawns); + // Copy additional fields for custom maps + if (copy.getClass() != LiveCompetitionMap.class) { + FieldUtil.copyFields(this, copy); + } + copy.mapWorld = world; + copy.postProcess(); return copy.createCompetition(arena); } diff --git a/plugin/src/main/java/org/battleplugins/arena/util/FieldUtil.java b/plugin/src/main/java/org/battleplugins/arena/util/FieldUtil.java new file mode 100644 index 00000000..cda342c4 --- /dev/null +++ b/plugin/src/main/java/org/battleplugins/arena/util/FieldUtil.java @@ -0,0 +1,24 @@ +package org.battleplugins.arena.util; + +import org.battleplugins.arena.config.ArenaOption; + +import java.lang.reflect.Field; + +public class FieldUtil { + + public static void copyFields(T oldInstance, T newInstance) { + for (Field field : oldInstance.getClass().getDeclaredFields()) { + if (!field.isAnnotationPresent(ArenaOption.class)) { + continue; + } + + field.setAccessible(true); + + try { + field.set(newInstance, field.get(oldInstance)); + } catch (IllegalAccessException e) { + throw new RuntimeException("Failed to copy field " + field.getName(), e); + } + } + } +}