Skip to content

Commit

Permalink
v0.5 update
Browse files Browse the repository at this point in the history
Two config files, finally!
  • Loading branch information
Xenoyia committed May 9, 2017
1 parent 7b5459c commit 1f45488
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 36 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ buildscript {
apply plugin: 'net.minecraftforge.gradle.forge'

group = 'com.xpgaming'
version = '0.3-fix'
version = '0.5'
description = 'Fun things with the PokéDex!'

repositories {
Expand Down
40 changes: 20 additions & 20 deletions src/main/java/com/xpgaming/xPPokeDex/Claim.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public CommandResult execute(CommandSource src, CommandContext args) throws Comm
try {
numClaimed++;
EntityPlayerMP emp = (EntityPlayerMP) player;
Config.getInstance().getConfig().getNode("playerData", player.getUniqueId().toString(), "10").setValue(true);
Config.getInstance().saveAndLoadConfig();
UserData.getInstance().getConfig().getNode("playerData", player.getUniqueId().toString(), "10").setValue(true);
UserData.getInstance().saveAndLoadConfig();
int numRewards = Config.getInstance().getConfig().getNode("rewards", "reward10", "numberOfRewards").getInt();
if(numRewards > 0) {
for(int i = 1; i < numRewards+1; i++) {
Expand Down Expand Up @@ -81,8 +81,8 @@ public CommandResult execute(CommandSource src, CommandContext args) throws Comm
try {
numClaimed++;
EntityPlayerMP emp = (EntityPlayerMP) player;
Config.getInstance().getConfig().getNode("playerData", player.getUniqueId().toString(), "20").setValue(true);
Config.getInstance().saveAndLoadConfig();
UserData.getInstance().getConfig().getNode("playerData", player.getUniqueId().toString(), "20").setValue(true);
UserData.getInstance().saveAndLoadConfig();
int numRewards = Config.getInstance().getConfig().getNode("rewards", "reward20", "numberOfRewards").getInt();
if(numRewards > 0) {
for(int i = 1; i < numRewards+1; i++) {
Expand Down Expand Up @@ -118,8 +118,8 @@ public CommandResult execute(CommandSource src, CommandContext args) throws Comm
try {
numClaimed++;
EntityPlayerMP emp = (EntityPlayerMP) player;
Config.getInstance().getConfig().getNode("playerData", player.getUniqueId().toString(), "30").setValue(true);
Config.getInstance().saveAndLoadConfig();
UserData.getInstance().getConfig().getNode("playerData", player.getUniqueId().toString(), "30").setValue(true);
UserData.getInstance().saveAndLoadConfig();
int numRewards = Config.getInstance().getConfig().getNode("rewards", "reward30", "numberOfRewards").getInt();
if(numRewards > 0) {
for(int i = 1; i < numRewards+1; i++) {
Expand Down Expand Up @@ -155,8 +155,8 @@ public CommandResult execute(CommandSource src, CommandContext args) throws Comm
try {
numClaimed++;
EntityPlayerMP emp = (EntityPlayerMP) player;
Config.getInstance().getConfig().getNode("playerData", player.getUniqueId().toString(), "40").setValue(true);
Config.getInstance().saveAndLoadConfig();
UserData.getInstance().getConfig().getNode("playerData", player.getUniqueId().toString(), "40").setValue(true);
UserData.getInstance().saveAndLoadConfig();
int numRewards = Config.getInstance().getConfig().getNode("rewards", "reward40", "numberOfRewards").getInt();
if(numRewards > 0) {
for(int i = 1; i < numRewards+1; i++) {
Expand Down Expand Up @@ -192,8 +192,8 @@ public CommandResult execute(CommandSource src, CommandContext args) throws Comm
try {
numClaimed++;
EntityPlayerMP emp = (EntityPlayerMP) player;
Config.getInstance().getConfig().getNode("playerData", player.getUniqueId().toString(), "50").setValue(true);
Config.getInstance().saveAndLoadConfig();
UserData.getInstance().getConfig().getNode("playerData", player.getUniqueId().toString(), "50").setValue(true);
UserData.getInstance().saveAndLoadConfig();
int numRewards = Config.getInstance().getConfig().getNode("rewards", "reward50", "numberOfRewards").getInt();
if(numRewards > 0) {
for(int i = 1; i < numRewards+1; i++) {
Expand Down Expand Up @@ -229,8 +229,8 @@ public CommandResult execute(CommandSource src, CommandContext args) throws Comm
try {
numClaimed++;
EntityPlayerMP emp = (EntityPlayerMP) player;
Config.getInstance().getConfig().getNode("playerData", player.getUniqueId().toString(), "60").setValue(true);
Config.getInstance().saveAndLoadConfig();
UserData.getInstance().getConfig().getNode("playerData", player.getUniqueId().toString(), "60").setValue(true);
UserData.getInstance().saveAndLoadConfig();
int numRewards = Config.getInstance().getConfig().getNode("rewards", "reward60", "numberOfRewards").getInt();
if(numRewards > 0) {
for(int i = 1; i < numRewards+1; i++) {
Expand Down Expand Up @@ -266,8 +266,8 @@ public CommandResult execute(CommandSource src, CommandContext args) throws Comm
try {
numClaimed++;
EntityPlayerMP emp = (EntityPlayerMP) player;
Config.getInstance().getConfig().getNode("playerData", player.getUniqueId().toString(), "70").setValue(true);
Config.getInstance().saveAndLoadConfig();
UserData.getInstance().getConfig().getNode("playerData", player.getUniqueId().toString(), "70").setValue(true);
UserData.getInstance().saveAndLoadConfig();
int numRewards = Config.getInstance().getConfig().getNode("rewards", "reward70", "numberOfRewards").getInt();
if(numRewards > 0) {
for(int i = 1; i < numRewards+1; i++) {
Expand Down Expand Up @@ -303,8 +303,8 @@ public CommandResult execute(CommandSource src, CommandContext args) throws Comm
try {
numClaimed++;
EntityPlayerMP emp = (EntityPlayerMP) player;
Config.getInstance().getConfig().getNode("playerData", player.getUniqueId().toString(), "80").setValue(true);
Config.getInstance().saveAndLoadConfig();
UserData.getInstance().getConfig().getNode("playerData", player.getUniqueId().toString(), "80").setValue(true);
UserData.getInstance().saveAndLoadConfig();
int numRewards = Config.getInstance().getConfig().getNode("rewards", "reward80", "numberOfRewards").getInt();
if(numRewards > 0) {
for(int i = 1; i < numRewards+1; i++) {
Expand Down Expand Up @@ -340,8 +340,8 @@ public CommandResult execute(CommandSource src, CommandContext args) throws Comm
try {
numClaimed++;
EntityPlayerMP emp = (EntityPlayerMP) player;
Config.getInstance().getConfig().getNode("playerData", player.getUniqueId().toString(), "90").setValue(true);
Config.getInstance().saveAndLoadConfig();
UserData.getInstance().getConfig().getNode("playerData", player.getUniqueId().toString(), "90").setValue(true);
UserData.getInstance().saveAndLoadConfig();
int numRewards = Config.getInstance().getConfig().getNode("rewards", "reward90", "numberOfRewards").getInt();
if(numRewards > 0) {
for(int i = 1; i < numRewards+1; i++) {
Expand Down Expand Up @@ -378,8 +378,8 @@ public CommandResult execute(CommandSource src, CommandContext args) throws Comm
numClaimed++;
EntityPlayerMP emp = (EntityPlayerMP) player;
Sponge.getServer().getBroadcastChannel().send(Text.of("\u00A7f[\u00A7bPokeDex\u00A7f] \u00A7f"+player.getName()+" \u00A7bhas completed their Pokedex!"));
Config.getInstance().getConfig().getNode("playerData", player.getUniqueId().toString(), "100").setValue(true);
Config.getInstance().saveAndLoadConfig();
UserData.getInstance().getConfig().getNode("playerData", player.getUniqueId().toString(), "100").setValue(true);
UserData.getInstance().saveAndLoadConfig();
int numRewards = Config.getInstance().getConfig().getNode("rewards", "rewardfinal", "numberOfRewards").getInt();
if(numRewards > 0) {
for(int i = 1; i < numRewards+1; i++) {
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/com/xpgaming/xPPokeDex/Config.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.xpgaming.xPPokeDex;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;

import com.google.common.collect.Lists;
Expand All @@ -9,9 +10,11 @@
import com.pixelmonmod.pixelmon.config.PixelmonItemsPokeballs;
import com.pixelmonmod.pixelmon.items.PixelmonItem;
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
import ninja.leaping.configurate.hocon.HoconConfigurationLoader;
import ninja.leaping.configurate.loader.ConfigurationLoader;
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.config.ConfigDir;
import org.spongepowered.api.config.DefaultConfig;
import org.spongepowered.api.data.key.Keys;
import org.spongepowered.api.data.manipulator.mutable.item.EnchantmentData;
Expand All @@ -32,12 +35,11 @@ public class Config {
public static Config getInstance() {
return instance;
}
@Inject
@DefaultConfig(sharedRoot = false) private File configDir;

private ConfigurationLoader<CommentedConfigurationNode> configLoader;
private CommentedConfigurationNode config;
private File configFile = new File(this.configDir, "config.conf");
@Inject @ConfigDir(sharedRoot = false) private File configDir;
String path = "config"+File.separator+"xPPokeDex";
private File configFile = new File(path, "config.conf");
private ConfigurationLoader<CommentedConfigurationNode> configLoader = HoconConfigurationLoader.builder().setFile(configFile).build();

public ItemType Type(String item) {
ItemType i = Sponge.getGame().getRegistry().getType(ItemType.class,item).get();
Expand All @@ -46,10 +48,10 @@ public ItemType Type(String item) {

public void configCreate() throws ObjectMappingException {
try {
if(!configFile.getParentFile().exists()) configFile.getParentFile().mkdir();
configFile.createNewFile();
configLoad();
CommentedConfigurationNode rewards = config.getNode("rewards").setComment("xP// PokeDex coded by Xenoyia! Check out mc.xpgaming.com!");
CommentedConfigurationNode pd = config.getNode("playerData").setComment("Do not mess with this unless you know what you are doing!");
ItemStack reward10 = ItemStack.builder()
.itemType(Type("pixelmon:tm114"))
.quantity(1)
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/com/xpgaming/xPPokeDex/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.file.Path;
import java.sql.SQLException;
import java.util.Optional;

@Plugin(id = Main.id, name = Main.name, version = "0.4", dependencies = {@Dependency(id = "pixelmon")})
@Plugin(id = Main.id, name = Main.name, version = "0.5", dependencies = {@Dependency(id = "pixelmon")})
public class Main {
private static Main instance = new Main();
public static Main getInstance() {
Expand Down Expand Up @@ -117,18 +118,18 @@ public void addMoney(Player p, int amount) {
.executor(new PokedexAdmin())
.build();

@Inject
@DefaultConfig(sharedRoot = true)
private File configFile;
String path = "config"+File.separator+"xPPokeDex";

@Inject
@DefaultConfig(sharedRoot = true)
ConfigurationLoader<CommentedConfigurationNode> configLoader;
File configFile = new File(path,"config.conf");
ConfigurationLoader<CommentedConfigurationNode> configLoader = HoconConfigurationLoader.builder().setFile(configFile).build();
File dataFile = new File(path, "data.conf");
ConfigurationLoader<CommentedConfigurationNode> dataLoader = HoconConfigurationLoader.builder().setFile(dataFile).build();

@Listener
public void onGameInitialization(GameInitializationEvent event) {
Config.getInstance().setup(configFile, configLoader);
log.info("Loaded v0.4!");
UserData.getInstance().setup(dataFile, dataLoader);
log.info("Loaded v0.5!");
Sponge.getCommandManager().register(this, pokedex, "pokedex", "pd", "dex");
Sponge.getCommandManager().register(this, pokedexAdmin, "pokedexadmin", "pda", "dexadmin");
Sponge.getServiceManager().provide(EconomyService.class);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/xpgaming/xPPokeDex/Reload.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
public class Reload implements CommandExecutor {
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
Config.getInstance().configLoad();
UserData.getInstance().dataLoad();
src.sendMessage(Text.of("\u00A7f[\u00A7bPokeDex\u00A7f] \u00A7b\u00A7lSuccessfully reloaded the config!"));
return CommandResult.success();
}
Expand Down
88 changes: 88 additions & 0 deletions src/main/java/com/xpgaming/xPPokeDex/UserData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.xpgaming.xPPokeDex;

import com.google.common.reflect.TypeToken;
import com.google.inject.Inject;
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
import ninja.leaping.configurate.hocon.HoconConfigurationLoader;
import ninja.leaping.configurate.loader.ConfigurationLoader;
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.config.ConfigDir;
import org.spongepowered.api.config.DefaultConfig;
import org.spongepowered.api.item.ItemType;
import org.spongepowered.api.item.inventory.ItemStack;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;

public class UserData {
private static UserData instance = new UserData();

public static UserData getInstance() {
return instance;
}

private CommentedConfigurationNode data;
@Inject @ConfigDir(sharedRoot = false) private File configDir;
String path = "config"+File.separator+"xPPokeDex";
private File dataFile = new File(path, "data.conf");
private ConfigurationLoader<CommentedConfigurationNode> dataLoader = HoconConfigurationLoader.builder().setFile(dataFile).build();

public void dataCreate() throws ObjectMappingException {
try {
if(!dataFile.getParentFile().exists()) dataFile.getParentFile().mkdirs();
dataFile.createNewFile();
dataLoad();
CommentedConfigurationNode pd = data.getNode("playerData").setComment("Do not mess with this unless you know what you are doing!");
dataSave();
} catch (IOException e) {
e.printStackTrace();
}
}

public void setup(File dataFile, ConfigurationLoader<CommentedConfigurationNode> dataLoader) {
this.dataLoader = dataLoader;
this.dataFile = dataFile;
if (!dataFile.exists()) {
try {
dataCreate();
} catch (ObjectMappingException e) {
e.printStackTrace();
}
} else
dataLoad();
}

public CommentedConfigurationNode getConfig() {
return data;
}

public void dataLoad() {
if (!dataFile.exists()) {
try {
dataCreate();
} catch (ObjectMappingException e) {
e.printStackTrace();
}
} else
try {
data = dataLoader.load();
} catch (IOException e) {
e.printStackTrace();
}
}

public void dataSave() {
try {
dataLoader.save(data);
} catch (IOException e) {
e.printStackTrace();
}
}

public void saveAndLoadConfig() {
dataSave();
dataLoad();
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/xpgaming/xPPokeDex/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,6 @@ public double calcPercent(EntityPlayerMP entity) {
}

public boolean hasClaimed(Player p, String pct) {
return Config.getInstance().getConfig().getNode("playerData", p.getUniqueId().toString(), pct).getBoolean();
return UserData.getInstance().getConfig().getNode("playerData", p.getUniqueId().toString(), pct).getBoolean();
}
}

0 comments on commit 1f45488

Please sign in to comment.