DungeonsXL/core/src/main/java/de/erethon/dungeonsxl/global/UnloadedProtection.java

81 lines
2.9 KiB
Java

/*
* Copyright (C) 2012-2022 Frank Baumann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.erethon.dungeonsxl.global;
import de.erethon.dungeonsxl.DungeonsXL;
import de.erethon.bedrock.chat.MessageUtil;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import org.bukkit.World;
import org.bukkit.configuration.ConfigurationSection;
/**
* @author Daniel Saukel
*/
public class UnloadedProtection<T extends GlobalProtection> {
private DungeonsXL plugin;
private GlobalProtectionCache cache;
private Constructor constructor;
private String worldName;
private int id;
private ConfigurationSection config;
private UnloadedProtection() {}
public static <T extends GlobalProtection> UnloadedProtection create(DungeonsXL plugin, Class<T> type, String worldName, int id, ConfigurationSection config) {
UnloadedProtection protection = new UnloadedProtection();
protection.plugin = plugin;
try {
protection.constructor = type.getConstructor(DungeonsXL.class, World.class, int.class, ConfigurationSection.class);
} catch (NoSuchMethodException | SecurityException exception) {
return null;
}
protection.worldName = worldName;
protection.id = id;
protection.config = config;
protection.cache = plugin.getGlobalProtectionCache();
return protection;
}
public T load(World world) {
if (!world.getName().equals(worldName)) {
throw new IllegalArgumentException("World mismatch: Expected " + worldName + ", but received " + world);
}
T protection = null;
try {
protection = (T) constructor.newInstance(plugin, world, id, config);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException exception) {
MessageUtil.log(plugin, "Could not find or invoke " + constructor);
exception.printStackTrace();
}
if (protection != null) {
cache.getUnloadedProtections().remove(this);
}
return protection;
}
@Override
public String toString() {
return "UnloadedProtection{type=" + constructor.getDeclaringClass().getName() + "; " + "world=" + worldName + "}";
}
}