#try catch (Throwable $ex)???

1 messages · Page 1 of 1 (latest)

light tangle
#

Hey, Id like to know when this try catch loop ends because I have no clue? please note that I am really new to programming and just trying to understand the code/ changing the outcome/ format of the yml file ^^.
Feel free to ping or dm me if you help me or I should clarify things. ❤️

/*    */     try {
/* 30 */       File file = new File(this.instance.getDataFolder().getAbsoluteFile() + "/player-data/" + player.getUniqueId() + ".yml");                                  
/* 31 */       YamlConfiguration yamlConfiguration = YamlConfiguration.loadConfiguration(file);
/* 32 */       PlayerData playerData = getPlayerData(player.getUniqueId());
/* 33 */       if (!yamlConfiguration.contains("Balance")) {
/* 34 */         playerData.setBalance(this.instance.getSettingsFile().getConfig().getLong("Settings.Default Balance"));
/*    */       } else {
/* 36 */         playerData.setBalance(yamlConfiguration.getLong("Balance"));
/*    */       } 
/*    */       
/* 39 */       if (!yamlConfiguration.contains("Pay Toggle")) {
/* 40 */         playerData.setPayEnabled(true);
/*    */       } else {
/* 42 */         playerData.setPayEnabled(yamlConfiguration.getBoolean("Pay Toggle"));
/*    */       } 
/*    */       
/* 45 */       if (!yamlConfiguration.contains("Purchases")) {
/* 46 */         playerData.setPurchasedItemCount(0);
/*    */       } else {
/* 48 */         playerData.setPurchasedItemCount(yamlConfiguration.getInt("Purchases"));
/*    */       } 
/*    */       
/* 51 */       yamlConfiguration.save(file);
/*    */     } catch (Throwable $ex) {
/*    */       throw $ex;
/*    */     } 
/*    */   }```
dry summit
#

Theres no point in having a catch also throw the thing youre caching

#

youre basically saying "try this but if theres an error, catch the error, but send the error again"

#

The loop ends when there is an error, and the code jumps to whatever is in the catch

light tangle
#

So its just an infinite loop?

dry summit
#

You also cannot do this, it will give you a compile error

#

To throw exceptions you have to have a condition at which that exception is throw.
E.g.

boolean hasPantsOn = true;
if (hasPantsOn ) {
  System.out.println("You have pants on!");
} else {
  throw new NoPantsException();
}
brazen hazel
#

Too much $ex in here

light tangle
#

Its not my code. I took it from a working plugin and want to modify it to my needs

strong obsidian
#

ayo wtf is this

cerulean kraken
#

check if the file exists too

#

dunno why im here

cerulean kraken
balmy hull
#

lmfao

dry summit
light tangle
#

I want to get a second economy besides vault/ essentials. I got one thats called shards and now I need to change the layout of the yml file it saves the payerdata/ money to (make it comparable with a different plugin). You said that the code from this plugin is bad, im open to change to a different one.

#
Balance: 1000
Purchases: 0
Pay Toggle: true
#

Is how the plugin outputs

#
Tokens:
    balance: 0
#

This is how it should be

dry summit
light tangle
#

I want 2 ecosystems

#

one is essensials and the other one is shards

#

I just want to change the ouput file of the shards

#

I talked about essensials because I dont essentially want to use shards, its just the first thing I came across

dry summit
#

I’m unfamiliar with the plug-in. Is the yml format u want based on a config or does the plugin save the data to a yml file?

light tangle
#

as I said, if you know a plugin that would do the job better I would use that ^^

dry summit
#

you would probably have to modify the save and load functions within the plugin itself

#

can u send the jar for the plugin?

light tangle
#

sure

#

should be in me/skript/shaeds/playerdata/PlayerDataManager

dry summit
#

found it

#

so if you just want it to save and load the balance in the yml file you need to change both of the save and loads.
UNTESTED:

public void loadPlayerData(Player player) {
    try {
      File file = new File(this.instance.getDataFolder().getAbsoluteFile() + "/player-data/" + player.getUniqueId() + ".yml");
      YamlConfiguration yamlConfiguration = YamlConfiguration.loadConfiguration(file);
      PlayerData playerData = getPlayerData(player.getUniqueId());
      if (!yamlConfiguration.contains("Tokens.balance")) {
        playerData.setBalance(this.instance.getSettingsFile().getConfig().getLong("Settings.Default Balance"));
      } else {
        playerData.setBalance(yamlConfiguration.getLong("Tokens.balance"));
      }
      yamlConfiguration.save(file);
    } catch (Throwable $ex) {
      throw $ex;
    } 
  }
  
  public void savePlayerData(Player player) {
    try {
      File file = new File(this.instance.getDataFolder().getAbsoluteFile() + "/player-data/" + player.getUniqueId() + ".yml");
      YamlConfiguration yamlConfiguration = YamlConfiguration.loadConfiguration(file);
      PlayerData playerData = getPlayerData(player);
      yamlConfiguration.set("Tokens.balance", Long.valueOf(playerData.getBalance()));
      yamlConfiguration.save(file);
    } catch (Throwable $ex) {
      throw $ex;
    } 
  }
#

Probably something like that

#

basically just changes the path of where balance is stored

light tangle
#

ya, I did that already

dry summit
#

yeah then that should work

light tangle
#

but it currently outputs ```
Balance: 1000
Purchases: 0
Pay Toggle: true

#

and I need it to be formated like that: Tokens: balance: 0

dry summit
#

prob something within the settings file then

light tangle
#

also, do you think I can just use the same plugin with different folder and name to have another tokensystem? xD

light tangle
#

settings file

dry summit
#
if (!yamlConfiguration.contains("Tokens.balance")) {
  playerData.setBalance(this.instance.getSettingsFile().getConfig().getLong("Settings.Default Balance"));
}

Will save to the default path

#

it uses some "instance#getSettingsFile()" object

#

unsure what that is but it seems to save to the normal path

light tangle
#

this.settingsFile = new FileManager(this, "settings.yml", getDataFolder().getAbsolutePath());

#

you mean that?

dry summit
#

yeah just looked through it again, its not determining the path its the default value

#

weird that its not saving it properly

light tangle
#

the path is is in the code I think

dry summit
#

yeah weird idk

light tangle
#

I changed it somewhere, idk where right now xD

#

do you know how I can change the output of the plugin so it fits my needs. tbh Im pretty lost in this plugin

#

its weard xD

#

this gets created YamlConfiguration yamlConfiguration = new YamlConfiguration(); maybe its responsible for the layout

dry summit
#

Maybe idk

sick rover
#

who the heck uses $ex as variable name lmao