Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
<version>1.14.4-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<!--This adds the JetBrains annotation artifact to the build for better code styling -->
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>16.0.2</version>
<scope>provided</scope>
</dependency>
</dependencies>

<properties>
Expand Down
38 changes: 24 additions & 14 deletions src/main/java/edu/whimc/portals/Destination.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ public class Destination {

public static final String NONE = "none";

private Main plugin;

private static List<Destination> destinations = new ArrayList<>();

private String name;
Expand All @@ -21,16 +19,23 @@ public class Destination {

private boolean isValid = true;

public static Destination createDestination(Main plugin, String name, Location location) {
return new Destination(plugin, name, location, location.getWorld().getName(), true);
public static Destination createDestination(String name, Location location) {
if (location.getWorld() == null) {
Main.getInstance().getLogger().severe("Could not find world within location when creating a Destination");
return null;
}
Destination destination = new Destination(name, location, location.getWorld().getName(), true);
destination.register();
return destination;
}

public static Destination loadDestination(Main plugin, String name, Location location, String worldName) {
return new Destination(plugin, name, location, worldName, false);
public static Destination loadDestination(String name, Location location, String worldName) {
Destination destination = new Destination(name, location, worldName, false);
destination.register();
return destination;
}

private Destination(Main plugin, String name, Location location, String worldName, boolean isNew) {
this.plugin = plugin;
private Destination(String name, Location location, String worldName, boolean isNew) {
this.name = name;
this.location = location;
this.worldName = worldName;
Expand All @@ -40,11 +45,12 @@ private Destination(Main plugin, String name, Location location, String worldNam
}

if (isNew) {
plugin.getLocationSaver().saveLocation(location, "Destinations." + name);
Main.getInstance().getLocationSaver().saveLocation(location, "Destinations." + name);
}
}

private void register() {
destinations.add(this);

}

public static List<Destination> getDestinations() {
Expand Down Expand Up @@ -88,10 +94,14 @@ public Location getLocation() {
}

public void setLocation(Location location) {
if (location.getWorld() == null) {
Main.getInstance().getLogger().severe("Could not find world within location when setting location of destination");
return;
}
this.location = location;
this.worldName = location.getWorld().getName();
this.isValid = true;
plugin.getLocationSaver().saveLocation(location, "Destinations." + name);
Main.getInstance().getLocationSaver().saveLocation(location, "Destinations." + name);
}

public boolean isValid() {
Expand All @@ -109,9 +119,9 @@ public void remove() {
}

destinations.remove(this);
plugin.getPortalData().removeKey("Destinations." + this.name);
plugin.getPortalData().saveConfig();
plugin.getPortalData().reloadConfig();
Main.getInstance().getPortalData().removeKey("Destinations." + this.name);
Main.getInstance().getPortalData().saveConfig();
Main.getInstance().getPortalData().reloadConfig();
}

public String getWorldName() {
Expand Down
35 changes: 25 additions & 10 deletions src/main/java/edu/whimc/portals/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.command.PluginCommand;
import org.bukkit.permissions.Permission;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
Expand All @@ -21,14 +22,18 @@
public class Main extends JavaPlugin {

public static final String PERM_PREFIX = "whimc-portals";
public static final Material TOOL_MATERIAL = Material.WOODEN_SWORD;

private MyConfigManager manager;
private static MyConfig portalData;
private LocationSaver locationSaver;

public static Main getInstance() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not the biggest fan of statically accessing the main class. I know this is a bit contradictory given my comment about making Portal/Destination managers, but that's just the personal style I use.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason why I like it is for those managing classes can be accessed with that single static method, so there are practically no other public static fields and everything is retrieved from the same place, and for logging because I don't want to be five references removed from the Main class and have to pass the plugin through everything just to access the logger (or managers within Main). I guess it just depends on how many static fields you want, I'm generally for few as possible.

return Main.getPlugin(Main.class);
}

@Override
public void onEnable() {
manager = new MyConfigManager(this);
MyConfigManager manager = new MyConfigManager(this);
portalData = manager.getNewConfig("portalData.yml");
locationSaver = new LocationSaver(this);

Expand Down Expand Up @@ -58,13 +63,23 @@ private void registerStuff() {
pm.registerEvents(new PortalBlockChangeListener(), this);
pm.registerEvents(new PortalDamageListener(), this);

PortalCommand pc = new PortalCommand(this);
getCommand("portal").setExecutor(pc);
getCommand("portal").setTabCompleter(pc);
PortalCommand pc = new PortalCommand();
PluginCommand ppc = getCommand("portal");
if (ppc == null) {
getLogger().severe("The portal command was not properly registered.");
} else {
ppc.setExecutor(pc);
ppc.setTabCompleter(pc);
}

DestinationCommand dc = new DestinationCommand(this);
getCommand("destination").setExecutor(dc);
getCommand("destination").setTabCompleter(dc);
DestinationCommand dc = new DestinationCommand();
PluginCommand dpc = getCommand("destination");
if (dpc == null) {
getLogger().severe("The destination command was not properly registered");
} else {
dpc.setExecutor(dc);
dpc.setTabCompleter(dc);
}
}

private void initializeConfig() {
Expand All @@ -78,7 +93,7 @@ private void initializeConfig() {
path = "Destinations." + key + ".world";
destWorldName = portalData.getString(path);

Destination.loadDestination(this, key, destLoc, destWorldName);
Destination.loadDestination(key, destLoc, destWorldName);
}
}

Expand All @@ -103,7 +118,7 @@ private void initializeConfig() {
String fillerName = portalData.getString("Portals." + key + ".filler", "");
Material filler = Material.matchMaterial(fillerName);

Portal.loadPortal(this, key, permission, portalWorldName, pos1, pos2, dest, filler);
Portal.loadPortal(key, permission, portalWorldName, pos1, pos2, dest, filler);
}
}
}
Expand Down
Loading