Skip to content

Commit

Permalink
Copy over fields for dynamic maps
Browse files Browse the repository at this point in the history
  • Loading branch information
Redned235 committed Jul 3, 2024
1 parent 0a983b8 commit 3def938
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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!");
Expand Down Expand Up @@ -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);
}
Expand Down
24 changes: 24 additions & 0 deletions plugin/src/main/java/org/battleplugins/arena/util/FieldUtil.java
Original file line number Diff line number Diff line change
@@ -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 <T> 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);
}
}
}
}

0 comments on commit 3def938

Please sign in to comment.