Changes to static references

This commit is contained in:
Sekwah 2018-06-06 22:29:02 +01:00 committed by Sekwah
parent c69c66c76c
commit afe32f5086
7 changed files with 62 additions and 28 deletions

View File

@ -21,24 +21,27 @@ import com.sekwah.advancedportals.core.util.Lang;
import com.sekwah.advancedportals.coreconnector.ConnectorDataCollector;
import com.sekwah.advancedportals.coreconnector.command.CommandRegister;
import java.io.File;
public class AdvancedPortalsCore {
private static AdvancedPortalsCore instance;
private final CommandRegister commandRegister;
private final DataStorage dataStorage;
private final InfoLogger infoLogger;
private final int mcMinorVer;
private final ConnectorDataCollector dataCollector;
private WarpEffectRegistry warpEffectRegistry;
private Injector injector = Guice.createInjector(new RepositoryModule(this));
private WarpEffectRegistry warpEffectRegistry = injector.getInstance(WarpEffectRegistry.class);
private TagRegistry<AdvancedPortal> portalTagRegistry;
private TagRegistry<Destination> destiTagRegistry;
private Injector injector = Guice.createInjector(new RepositoryModule(this));
private CoreListeners coreListeners = injector.getInstance(CoreListeners.class);
private final DataStorage dataStorage = injector.getInstance(DataStorage.class);
private CommandWithSubCommands portalCommand;
private CommandWithSubCommands destiCommand;
@ -52,14 +55,14 @@ public class AdvancedPortalsCore {
public static final String lastTranslationUpdate = "1.0.0";
/**
* @param dataStorage - The implementation of data storage for the specific platform
* @param dataStorageLoc - Where the files will be located
* @param infoLogger - The implementation of the logger for the specific platform
* @param commandRegister - Handles the command registry, different on each platform
* @param mcVer Minecraft version e.g. 1.12.2
*/
public AdvancedPortalsCore(DataStorage dataStorage, InfoLogger infoLogger, CommandRegister commandRegister,
ConnectorDataCollector dataCollector, int[] mcVer) {
this.dataStorage = dataStorage;
public AdvancedPortalsCore(File dataStorageLoc, InfoLogger infoLogger, CommandRegister commandRegister,
ConnectorDataCollector dataCollector, int[] mcVer) {
this.dataStorage.setStorageLocation(dataStorageLoc);
this.infoLogger = infoLogger;
instance = this;
this.commandRegister = commandRegister;
@ -110,9 +113,8 @@ public class AdvancedPortalsCore {
}
private void onEnable() {
this.warpEffectRegistry = new WarpEffectRegistry();
this.portalTagRegistry = new TagRegistry<>();
this.destiTagRegistry = new TagRegistry<>();
this.portalTagRegistry = new TagRegistry<>(this);
this.destiTagRegistry = new TagRegistry<>(this);
this.dataStorage.copyDefaultFile("lang/en_GB.lang", false);

View File

@ -2,6 +2,8 @@ package com.sekwah.advancedportals.core.api.registry;
import com.sekwah.advancedportals.core.AdvancedPortalsCore;
import com.sekwah.advancedportals.core.api.commands.SubCommand;
import com.sekwah.advancedportals.core.api.portal.AdvancedPortal;
import com.sekwah.advancedportals.core.util.InfoLogger;
import java.util.ArrayList;
import java.util.Collections;
@ -24,6 +26,8 @@ public class SubCommandRegistry {
*/
protected ArrayList<String> subCommands = new ArrayList<>();
private InfoLogger infoLogger = AdvancedPortalsCore.getInstance().getInfoLogger();
/**
* @param arg argument needed to activate
* @param subCommand
@ -32,12 +36,12 @@ public class SubCommandRegistry {
public boolean registerSubCommand(String arg, SubCommand subCommand) {
if (subCommand == null) {
AdvancedPortalsCore.getInfoLogger().logWarning("The subcommand '" + arg + "' cannot be null.");
this.infoLogger.logWarning("The subcommand '" + arg + "' cannot be null.");
return false;
}
if(this.subCommandMap.containsKey(arg)){
AdvancedPortalsCore.getInfoLogger().logWarning("The subcommand '" + arg + "' already exists.");
this.infoLogger.logWarning("The subcommand '" + arg + "' already exists.");
return false;
}

View File

@ -16,6 +16,8 @@ import java.util.Map;
*/
public class TagRegistry<T> {
private final AdvancedPortalsCore portalsCore;
/**
* List of tag names which should be in order alphabetically
*/
@ -28,6 +30,10 @@ public class TagRegistry<T> {
private Map<String, TagHandler.Creation<T>> creationHandlers = new HashMap();
private Map<String, TagHandler.TagStatus<T>> statusHandlers = new HashMap();
public TagRegistry(AdvancedPortalsCore portalsCore) {
this.portalsCore = portalsCore;
}
/**
*
* @param arg
@ -79,12 +85,12 @@ public class TagRegistry<T> {
*/
public boolean registerTag(String tag) {
if (tag.contains(" ")) {
AdvancedPortalsCore.getInfoLogger().logWarning("The tag '"
this.portalsCore.getInfoLogger().logWarning("The tag '"
+ tag + "' is invalid as it contains spaces.");
return false;
}
if (this.tags.contains(tag)) {
AdvancedPortalsCore.getInfoLogger().logWarning("The tag "
this.portalsCore.getInfoLogger().logWarning("The tag "
+ tag + " has already been registered.");
return false;
}
@ -129,7 +135,7 @@ public class TagRegistry<T> {
public boolean registerTag(String tag, Object tagHandler) {
if (tag == null) {
AdvancedPortalsCore.getInfoLogger().logWarning("A tag cannot be null.");
this.portalsCore.getInfoLogger().logWarning("A tag cannot be null.");
return false;
}

View File

@ -1,5 +1,7 @@
package com.sekwah.advancedportals.core.api.registry;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import com.sekwah.advancedportals.core.AdvancedPortalsCore;
import com.sekwah.advancedportals.core.api.effect.WarpEffect;
@ -16,6 +18,10 @@ public class WarpEffectRegistry {
private Map<String, WarpEffect> soundEffects = new HashMap();
@Inject
@Named("portals-core")
private AdvancedPortalsCore portalsCore;
/**
* Register a new warp effect.
*
@ -36,7 +42,7 @@ public class WarpEffectRegistry {
list = this.visualEffects;
break;
default:
AdvancedPortalsCore.getInfoLogger().logWarning(type.toString()
this.portalsCore.getInfoLogger().logWarning(type.toString()
+ " effect type not recognised");
return false;
}
@ -57,7 +63,7 @@ public class WarpEffectRegistry {
list = this.visualEffects;
break;
default:
AdvancedPortalsCore.getInfoLogger().logWarning(type.toString()
this.portalsCore.getInfoLogger().logWarning(type.toString()
+ " effect type not recognised");
return null;
}
@ -65,7 +71,7 @@ public class WarpEffectRegistry {
return list.get(name);
}
else{
AdvancedPortalsCore.getInfoLogger().logWarning("No effect of type:"
this.portalsCore.getInfoLogger().logWarning("No effect of type:"
+ type.toString() + " was registered with the name: " + name);
return null;
}

View File

@ -19,6 +19,9 @@ import java.util.Map;
* Excluding the temp data like selections
*/
public final class PortalServices {
public void loadPortals(AdvancedPortalsCore advancedPortalsCore) {
}

View File

@ -2,20 +2,33 @@ package com.sekwah.advancedportals.core.data;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.inject.name.Named;
import com.sekwah.advancedportals.core.AdvancedPortalsCore;
import com.sekwah.advancedportals.core.api.portal.AdvancedPortal;
import javax.inject.Inject;
import javax.xml.crypto.Data;
import java.io.*;
import java.lang.reflect.Type;
public class DataStorage {
private Gson gson;
private Gson gson = new GsonBuilder().setPrettyPrinting().create();
private File dataFolder;
public DataStorage(File dataFolder) {
this.dataFolder = dataFolder;
gson = new GsonBuilder().setPrettyPrinting().create();
@Inject
@Named("portals-core")
private AdvancedPortalsCore portalsCore;
/**
* Only lets it be set once
* @param location
*/
public void setStorageLocation(File location) {
if(this.dataFolder == null) {
this.dataFolder = location;
}
}
/**
@ -99,15 +112,15 @@ public class DataStorage {
outStream.close();
} catch (NullPointerException e) {
e.printStackTrace();
AdvancedPortalsCore.getInfoLogger().logWarning("Could not load " + fileLoc + ". The file does" +
this.portalsCore.getInfoLogger().logWarning("Could not load " + fileLoc + ". The file does" +
"not exist or there has been an error reading the file.");
return false;
} catch (FileNotFoundException e) {
e.printStackTrace();
AdvancedPortalsCore.getInfoLogger().logWarning("Could not create " + fileLoc);
this.portalsCore.getInfoLogger().logWarning("Could not create " + fileLoc);
} catch (IOException e) {
e.printStackTrace();
AdvancedPortalsCore.getInfoLogger().logWarning("File error reading " + fileLoc);
this.portalsCore.getInfoLogger().logWarning("File error reading " + fileLoc);
}
}
return true;
@ -139,7 +152,7 @@ public class DataStorage {
} catch (NullPointerException e) {
System.out.println("OUTPUT 4");
e.printStackTrace();
AdvancedPortalsCore.getInfoLogger().logWarning("Could not load " + location + ". The file does" +
this.portalsCore.getInfoLogger().logWarning("Could not load " + location + ". The file does" +
"not exist or there has been an error reading the file.");
return null;
}

View File

@ -76,7 +76,7 @@ public class Lang {
}
} catch (NullPointerException e) {
e.printStackTrace();
AdvancedPortalsCore.getInfoLogger().logWarning("Could not load " + fileName + ".lang The file does" +
AdvancedPortalsCore.getInstance().getInfoLogger().logWarning("Could not load " + fileName + ".lang The file does" +
"not exist or there has been an error reading the file. Canceled loading language file.");
}
}