Skip to content

Commit

Permalink
Fix loading as an extension and config refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rtm516 committed Apr 2, 2021
1 parent ce6ffbd commit 3eb9f6b
Show file tree
Hide file tree
Showing 9 changed files with 425 additions and 122 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ Java:

![image](https://user-images.githubusercontent.com/5401186/80527741-c1519f00-898c-11ea-8b0a-999b455b77af.png)

## Download
You can download the plugin/extension from GitHub releases or eCloud

https://api.extendedclip.com/expansions/floodgate/ or `/papi ecloud download Floodgate` ingame

https://github.com/rtm516/FloodgatePlaceholders/releases/

## Placeholders:
- Device:
- `%floodgate_device%`
Expand Down
50 changes: 49 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.rtm516</groupId>
<artifactId>FloodgatePlaceholders</artifactId>
<version>1.2.1</version>
<version>1.3.0</version>
<name>FloodgatePlaceholders</name>

<developers>
Expand Down Expand Up @@ -50,6 +50,54 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.3</version>
<executions>
<execution>
<id>add-version</id>
<phase>process-sources</phase>
<goals>
<goal>replace</goal>
</goals>
<configuration>
<includes>
<include>${project.basedir}/src/main/java/com/rtm516/FloodgatePlaceholders/Placeholder.java</include>
</includes>
<replacements>
<replacement>
<token>String VERSION = ".*"</token>
<value>String VERSION = "${project.version}"</value>
</replacement>
<replacement>
<token>String NAME = ".*"</token>
<value>String NAME = "${project.name}"</value>
</replacement>
</replacements>
</configuration>
</execution>

<execution>
<id>remove-version</id>
<phase>process-classes</phase>
<goals>
<goal>replace</goal>
</goals>
<configuration>
<includes>
<include>${project.basedir}/src/main/java/com/rtm516/FloodgatePlaceholders/Placeholder.java</include>
</includes>
<replacements>
<replacement>
<token>String VERSION = ".*"</token>
<value>String VERSION = "DEV"</value>
</replacement>
</replacements>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
Expand Down
110 changes: 26 additions & 84 deletions src/main/java/com/rtm516/FloodgatePlaceholders/Config.java
Original file line number Diff line number Diff line change
@@ -1,111 +1,53 @@
package com.rtm516.FloodgatePlaceholders;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import lombok.Getter;
public interface Config {
boolean isSpecificDeviceDescriptors();

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.logging.Level;
import java.util.logging.Logger;
DevicePlaceholders getDevice();

@Getter
public class Config {
@JsonProperty(value = "specific-device-descriptors")
private boolean specificDeviceDescriptors;
GenericPlaceholders getLocale();

@JsonProperty(value = "device")
private DevicePlaceholders device;
GenericPlaceholders getVersion();

@JsonProperty(value = "locale")
private GenericPlaceholders locale;
GenericPlaceholders getXboxUsername();

@JsonProperty(value = "version")
private GenericPlaceholders version;
GenericPlaceholders getXboxXuid();

@JsonProperty(value = "xbox-username")
private GenericPlaceholders xboxUsername;
interface DevicePlaceholders {
String getJava();

@JsonProperty(value = "xbox-xuid")
private GenericPlaceholders xboxXuid;
String getGeneric();

@Getter
public static class DevicePlaceholders {
@JsonProperty("java")
private String java;
String getUnknown();

@JsonProperty("generic")
private String generic;
String getGoogle();

@JsonProperty("unknown")
private String unknown;
String getIOS();

@JsonProperty("google")
private String google;
String getOSX();

@JsonProperty("ios")
private String iOS;
String getAmazon();

@JsonProperty("osx")
private String OSX;
String getGearVR();

@JsonProperty("amazon")
private String amazon;
String getHololens();

@JsonProperty("gearvr")
private String gearVR;
String getUwp();

@JsonProperty("hololens")
private String hololens;
String getWin32();

@JsonProperty("uwp")
private String uwp;
String getDedicated();

@JsonProperty("win32")
private String win32;
String getPs4();

@JsonProperty("dedicated")
private String dedicated;
String getNX();

@JsonProperty("ps4")
private String ps4;

@JsonProperty("nx")
private String NX;

@JsonProperty("xbox")
private String xbox;
String getXbox();
}

@Getter
public static class GenericPlaceholders {
@JsonProperty("found")
private String found;

@JsonProperty("none")
private String none;
}
interface GenericPlaceholders {
String getFound();

public static Config load(Logger logger, Path configPath) {
Config config = null;
try {
try {
if (!configPath.toFile().exists())
Files.copy(Config.class.getClassLoader().getResourceAsStream("config.yml"), configPath);
} catch (Exception e) {
logger.log(Level.SEVERE, "Error while creating config.yml", e);
}

config = new ObjectMapper(new YAMLFactory()).readValue(Files.readAllBytes(configPath), Config.class);
} catch (Exception e) {
logger.log(Level.SEVERE, "Error while loading config.yml", e);
}

if (config == null) {
throw new RuntimeException("Failed to load config file! Try to delete the data folder of FloodgatePlaceholders");
}

return config;
String getNone();
}
}
146 changes: 146 additions & 0 deletions src/main/java/com/rtm516/FloodgatePlaceholders/ExpansionConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package com.rtm516.FloodgatePlaceholders;

public class ExpansionConfig implements Config {
private Placeholder expansion;

public ExpansionConfig(Placeholder expansion) {
this.expansion = expansion;
}

@Override
public boolean isSpecificDeviceDescriptors() {
return (Boolean) expansion.get("specific-device-descriptors", true);
}

@Override
public DevicePlaceholders getDevice() {
return new ExpansionDevicePlaceholders(this, "device");
}

@Override
public GenericPlaceholders getLocale() {
return new ExpansionGenericPlaceholders(this, "locale");
}

@Override
public GenericPlaceholders getVersion() {
return new ExpansionGenericPlaceholders(this, "version");
}

@Override
public GenericPlaceholders getXboxUsername() {
return new ExpansionGenericPlaceholders(this, "xbox-username");
}

@Override
public GenericPlaceholders getXboxXuid() {
return new ExpansionGenericPlaceholders(this, "xbox-xuid");
}

private class ExpansionDevicePlaceholders implements DevicePlaceholders {

private final ExpansionConfig config;
private final String parent;

public ExpansionDevicePlaceholders(ExpansionConfig config, String parent) {
this.config = config;
this.parent = parent;
}

@Override
public String getJava() {
return config.expansion.getString(parent + ".java", "");
}

@Override
public String getGeneric() {
return config.expansion.getString(parent + ".generic", "");
}

@Override
public String getUnknown() {
return config.expansion.getString(parent + ".unknown", "");
}

@Override
public String getGoogle() {
return config.expansion.getString(parent + ".google", "");
}

@Override
public String getIOS() {
return config.expansion.getString(parent + ".ios", "");
}

@Override
public String getOSX() {
return config.expansion.getString(parent + ".osx", "");
}

@Override
public String getAmazon() {
return config.expansion.getString(parent + ".fireos", "");
}

@Override
public String getGearVR() {
return config.expansion.getString(parent + ".gearvr", "");
}

@Override
public String getHololens() {
return config.expansion.getString(parent + ".hololens", "");
}

@Override
public String getUwp() {
return config.expansion.getString(parent + ".uwp", "");
}

@Override
public String getWin32() {
return config.expansion.getString(parent + ".win32", "");
}

@Override
public String getDedicated() {
return config.expansion.getString(parent + ".dedicated", "");
}

@Override
public String getPs4() {
return config.expansion.getString(parent + ".ps4", "");
}

@Override
public String getNX() {
return config.expansion.getString(parent + ".nx", "");
}

@Override
public String getXbox() {
return config.expansion.getString(parent + ".xbox", "");
}
}

private static class ExpansionGenericPlaceholders implements GenericPlaceholders {

private final ExpansionConfig config;
private final String parent;

public ExpansionGenericPlaceholders(ExpansionConfig config, String parent) {
this.config = config;
this.parent = parent;
}

@Override
public String getFound() {
return config.expansion.getString(parent + ".found", "");
}

@Override
public String getNone() {
return config.expansion.getString(parent + ".none", "");
}
}
}
9 changes: 6 additions & 3 deletions src/main/java/com/rtm516/FloodgatePlaceholders/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,27 @@

public class Main extends JavaPlugin {

public static final String NAME = "FloodgatePlaceholders";
public static final String VERSION = "DEV";

@Getter
private Config configuration;
private PluginConfig configuration;

@Override
public void onLoad() {
if (!getDataFolder().exists()) {
getDataFolder().mkdir();
}

configuration = Config.load(getLogger(), getDataFolder().toPath().resolve("config.yml"));
configuration = PluginConfig.load(getLogger(), getDataFolder().toPath().resolve("config.yml"));
}

@Override
public void onEnable() {
saveDefaultConfig();

if (Bukkit.getPluginManager().getPlugin("floodgate") != null && Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
new Placeholder(this).register();
new Placeholder(getConfiguration()).register();
} else {
getLogger().warning("Floodgate 2.0 or PlaceholderAPI not found! Disabling plugin.");
getServer().getPluginManager().disablePlugin(this);
Expand Down
Loading

0 comments on commit 3eb9f6b

Please sign in to comment.