Welcome, guest! Please login or register.

    * Shoutbox

    RefreshHistory
    • FaTe_Of_GoDs: You guys have an offline client?
      Today at 03:25:13 AM
    • ASDss: where do u download source and clients now
      August 18, 2017, 10:39:31 PM
    • ASDss: yo
      August 18, 2017, 10:39:20 PM
    • dan v jad: click me 4 da fun ;)[link]
      August 18, 2017, 04:39:58 PM
    • stCky: Palidinho is your OpenGL (was it OpenGL?) stuff open source anywhere?
      August 16, 2017, 09:07:22 PM
    • Travas:BUILD THE WALL
      August 15, 2017, 09:28:49 PM
    • Travas: i have ass cancer
      August 15, 2017, 09:23:29 PM
    • stCky: what are the fudge are you tryna ask?
      August 15, 2017, 08:21:35 PM
    • bader: what are the rsps community alive ?
      August 15, 2017, 05:46:16 PM
    • bader: yo guys
      August 15, 2017, 05:46:08 PM
    • Spacehost:[link] Updated our thread :)
      August 15, 2017, 09:40:34 AM
    • Adaro: The client is in Download section at Homepage
      August 15, 2017, 01:09:20 AM
    • FaTe_Of_GoDs: where do i get the client?????????????
      August 14, 2017, 05:23:14 PM
    • stCky: can anyone help me? I cant login to the shoutbox
      August 13, 2017, 05:45:15 PM
    • drubrkletern: appeal denied
      August 13, 2017, 02:35:27 PM
    • King_Trout:[link]
      August 13, 2017, 11:17:12 AM
    • Cole1497: no sorry
      August 13, 2017, 10:27:14 AM
    • ayz: yo can anyone explain something to me
      August 13, 2017, 08:08:51 AM
    • coolking12: Hi
      August 13, 2017, 04:16:06 AM
    • stCky: n+1
      August 11, 2017, 06:09:24 PM

    Author Topic: [PI/wL] NPC Spawns from json [v1.3]  (Read 5415 times)

    0 Members and 1 Guest are viewing this topic.

    Offlinesk8rdude461

    • MOPARSCAPE WAS HACKED
    • Member
    • ****
    • *
    • Posts: 12,471
    • Thanks: +0/-0
      • View Profile
    [PI/wL] NPC Spawns from json [v1.3]
    « on: July 08, 2014, 04:36:46 PM »
    Okay so this was wrote for PI, but I'm sure almost any winterLove server could use this with little conversion.

    Code: Java
    1. packageserver.model.npcs;
    2.  
    3. importjava.io.FileNotFoundException;
    4. importjava.io.FileReader;
    5. importjava.io.IOException;
    6. importjava.util.ArrayList;
    7.  
    8. importcom.google.gson.Gson;
    9. importcom.google.gson.JsonIOException;
    10. importcom.google.gson.JsonSyntaxException;
    11. importcom.google.gson.reflect.TypeToken;
    12.  
    13. /**
    14.  * NPCSpawns, each represents a single npc spawn in the server which is held
    15.  * within the array [email protected] #spawns}.
    16.  *
    17.  * This was based off of Galkon's ItemDefinitions, therefore it will be heavily
    18.  * similar in a lot of ways.
    19.  *
    20.  * @Author Jesse (Sk8rdude461)
    21.  * @Author Galkon
    22.  */
    23. publicclass NPCSpawns {
    24.  
    25.         /**
    26.          * All the npc spawns.
    27.          */
    28.         privatestatic ArrayList<NPCSpawns> spawns;
    29.  
    30.         /**
    31.          * The Id of the npc. Commonly referred to as npcType.
    32.          */
    33.         privateint npcId;
    34.  
    35.         /**
    36.          * The home x-pos to the npc
    37.          */
    38.         privateint xPos;
    39.  
    40.         /**
    41.          * The home y-pos to the npc
    42.          */
    43.         privateint yPos;
    44.  
    45.         /**
    46.          * The height level where the npc is spawned
    47.          */
    48.         privateint height;
    49.  
    50.         /**
    51.          * The Walking type for the npc.
    52.          */
    53.         privateint walkType;
    54.  
    55.         /**
    56.          * The health(Constitution) of the npc.
    57.          */
    58.         privateint health;
    59.  
    60.         /**
    61.          * The highest possible hit for the npc.
    62.          */
    63.         privateint maxHit;
    64.  
    65.         /**
    66.          * The attack level of the npc.
    67.          */
    68.         privateint attack;
    69.  
    70.         /**
    71.          * The defence level of the npc.
    72.          */
    73.         privateint defence;
    74.  
    75.         /**
    76.          * Extra info I added so you can tell where the npc is spawned.
    77.          */
    78.         privateString name;
    79.  
    80.         public NPCSpawns(int npcid, int xPos, int yPos, int height, int walkType,
    81.                         int health, int maxHit, int attack, int defence, String name){
    82.                 this.npcId= npcid;
    83.                 this.xPos= xPos;
    84.                 this.yPos= yPos;
    85.                 this.height= height;
    86.                 this.walkType= walkType;
    87.                 this.health= health;
    88.                 this.maxHit= maxHit;
    89.                 this.attack= attack;
    90.                 this.defence= defence;
    91.                 this.name= name;
    92.         }
    93.  
    94.         publicstatic NPCSpawns defaultNPC(){
    95.                 try{
    96.                         NPCSpawns npc =new NPCSpawns(0, 0, 0, 0, 0, 0, 0, 0, 0, "New NPC");
    97.                         npc.setName("New NPC");
    98.                         System.out.println("NPC Made!");
    99.                         return npc;
    100.                 }catch(NullPointerException e){
    101.                         returnnull;
    102.                 }
    103.         }
    104.  
    105.         /**
    106.          * Gets the npc spawns
    107.          *
    108.          * @return spawns
    109.          */
    110.         publicstatic ArrayList<NPCSpawns> getNPCSpawns(){
    111.                 return spawns;
    112.         }
    113.  
    114.         /**
    115.          * Sets the npc spawns
    116.          *
    117.          * @param npcSpawns
    118.          *            the NPCSpawns to be set.
    119.          */
    120.         publicstaticvoid setNPCSpawns(ArrayList<NPCSpawns> npcSpawns){
    121.                 NPCSpawns.spawns= npcSpawns;
    122.         }
    123.  
    124.         /**
    125.          * Loading the NPCSpawns.
    126.          *
    127.          * @param file
    128.          *            The file to be read
    129.          * @throws java.io.FileNotFoundException
    130.          */
    131.         publicstaticvoid loadNPCSpawns()throws JsonIOException,
    132.                         JsonSyntaxException, FileNotFoundException{
    133.                 Gson gson =new Gson();
    134.                 long start =System.currentTimeMillis();
    135.                 System.out.println("Loading NPC spawns...");
    136.  
    137.                 try(FileReader reader =newFileReader("./Data/cfg/npc-spawns.json")){
    138.                         setNPCSpawns(gson.fromJson(reader,
    139.                                         new TypeToken<ArrayList<NPCSpawns>>(){
    140.                                         }.getType()));
    141.                 }catch(IOException e){
    142.                         System.out.println("Failed to load NPC spawns!");
    143.                 }finally{
    144.                         System.out.println("Loaded "+ NPCSpawns.getNPCSpawns().size()
    145.                                         +" npc spawns in "+(System.currentTimeMillis()- start)
    146.                                         +"ms.");
    147.                 }
    148.  
    149.         }
    150.  
    151.         /**
    152.          * @return npcId
    153.          */
    154.         publicint getNpcId(){
    155.                 return npcId;
    156.         }
    157.  
    158.         /**
    159.          * @return xPos
    160.          */
    161.         publicint getXPos(){
    162.                 return xPos;
    163.         }
    164.  
    165.         /**
    166.          * @return yPos
    167.          */
    168.         publicint getYPos(){
    169.                 return yPos;
    170.         }
    171.  
    172.         /**
    173.          * @return height
    174.          */
    175.         publicint getHeight(){
    176.                 return height;
    177.         }
    178.  
    179.         /**
    180.          * @return walkType
    181.          */
    182.         publicint getWalkType(){
    183.                 return walkType;
    184.         }
    185.  
    186.         /**
    187.          * @return health
    188.          */
    189.         publicint getHealth(){
    190.                 return health;
    191.         }
    192.  
    193.         /**
    194.          * @return maxHit
    195.          */
    196.         publicint getMaxHit(){
    197.                 return maxHit;
    198.         }
    199.  
    200.         /**
    201.          * @return attack
    202.          */
    203.         publicint getAttack(){
    204.                 return attack;
    205.         }
    206.  
    207.         /**
    208.          * @return defence
    209.          */
    210.         publicint getDefence(){
    211.                 return defence;
    212.         }
    213.  
    214.         /**
    215.          * @return name
    216.          */
    217.         publicString getName(){
    218.                 return name;
    219.         }
    220.  
    221.         publicvoid setNpcId(int npcId){
    222.                 this.npcId= npcId;
    223.         }
    224.  
    225.         publicvoid setXPos(int xPos){
    226.                 this.xPos= xPos;
    227.         }
    228.  
    229.         publicvoid setYPos(int yPos){
    230.                 this.yPos= yPos;
    231.         }
    232.  
    233.         publicvoid setHeight(int height){
    234.                 this.height= height;
    235.         }
    236.  
    237.         publicvoid setWalkType(int walkType){
    238.                 this.walkType= walkType;
    239.         }
    240.  
    241.         publicvoid setHealth(int health){
    242.                 this.health= health;
    243.         }
    244.  
    245.         publicvoid setMaxHit(int maxHit){
    246.                 this.maxHit= maxHit;
    247.         }
    248.  
    249.         publicvoid setAttack(int attack){
    250.                 this.attack= attack;
    251.         }
    252.  
    253.         publicvoid setDefence(int defence){
    254.                 this.defence= defence;
    255.         }
    256.  
    257.         publicvoid setName(String name){
    258.                 this.name= name;
    259.         }
    260. }

    Your new loadAutoSpawn():
    Code: Java
    1. publicboolean loadAutoSpawn(){
    2.         try{
    3.                 for(int i =0; i < NPCSpawns.getNPCSpawns().size(); i++){
    4.                         NPCSpawns npc = NPCSpawns.getNPCSpawns().get(i);
    5.                         newNPC(npc.getNpcId(), npc.getXPos(), npc.getYPos(),npc.getHeight(),
    6.                                                 npc.getWalkType(), npc.getHealth(), npc.getMaxHit(), npc.getAttack(),
    7.                                                 npc.getDefence());
    8.                 }
    9.                 returntrue;
    10.         }catch(Exception e){
    11.                 returnfalse;
    12.         }
    13. }

    You must load the NPCSpawns.json before you use the loadAutoSpawn() method.

    The json file looks like this (it goes in Data/cfg/ for PI, you can change it easy):
    Code: [Select]
    [
    {
    "npcId":0,
    "xPos":0,
    "yPos":0,
    "height":0,
    "walkType":0,
    "health":0,
    "maxHit":0,
    "attack":0,
    "defence":0,
    "name":"Some NPC"
    },
    {
    "npcId":0,
    "xPos":0,
    "yPos":0,
    "height":0,
    "walkType":0,
    "health":0,
    "maxHit":0,
    "attack":0,
    "defence":0,
    "name":"Another NPC"
    }
    ]
    Save it as npc-spawns.json

    Remember you can change name and directory easily.

    And don't forget to get the latest gson library from Google.

    Update
    The editor is now on v1.3. If you're still on 1.0, please get the NPCSpawns.java and loadAutoSpawn above. If you're on 1.2, just grab the new file. In this update, I packed the gson file into the NPCSpawnEditor.jar, so you don't need the /lib/ folder.

    Changes:
    • File Chooser opens in NPCSpawnEditor.jar directory
    • Added missing "Defence" field
    • Fixed bug where NPC Height was saving as NPC Health
    • NPC's can only be deleted when the NPCList is in focus.
    (click to show/hide)

    Download Now[/url]

    (click to show/hide)
    How to use:

    Well it's very straightforward. Afaik.
    Open the file using either the menu or hitting CTRL + O. Then pick an npc from the list, edit whatever you want. Hit CTRL + W to save the NPC, or press the button.
    To save the file, hit CTRL + S or click the option in the menu. Pressing help will take you to this topic.
    To remove an NPC, select it from the list and press delete or backspace (on your keyboard). It'll confirm your action, and upon hitting yes, will delete.

    Not all servers have NPC facing... If yours does not, only walking/default will work. You must add the NPC facing yourself.

    Hope this helps.


    OfflineDriman Rune

    • Member
    • ****
    • Posts: 656
    • Thanks: +0/-0
      • View Profile
    Re: [PI/wL] NPC Spawns from json
    « Reply #1 on: July 08, 2014, 05:22:57 PM »
    I don't like JSON, but thanks!

    Offlinesk8rdude461

    • MOPARSCAPE WAS HACKED
    • Member
    • ****
    • *
    • Posts: 12,471
    • Thanks: +0/-0
      • View Profile
    Re: [PI/wL] NPC Spawns from json
    « Reply #2 on: July 08, 2014, 05:30:19 PM »
    I'm sure this can be done better, but here's this:
    Code: Java
    1.         publicboolean loadAutoSpawn(){
    2.                 try{
    3.                         for(int i =0; i < NPCSpawns.getNPCSpawns().length; i++){
    4.                                 newNPC(NPCSpawns.getNPCSpawns()[i].getNpcId(), NPCSpawns.getNPCSpawns()[i].getXPos(),
    5.                                                 NPCSpawns.getNPCSpawns()[i].getYPos(),NPCSpawns.getNPCSpawns()[i].getHeight(),
    6.                                                 NPCSpawns.getNPCSpawns()[i].getWalkType(), NPCSpawns.getNPCSpawns()[i].getHealth(),
    7.                                                 NPCSpawns.getNPCSpawns()[i].getMaxHit(), NPCSpawns.getNPCSpawns()[i].getAttack(),
    8.                                                 NPCSpawns.getNPCSpawns()[i].getDefence());
    9.                         }
    10.                         returntrue;
    11.                 }catch(Exception e){
    12.                         returnfalse;
    13.                 }
    14.         }


    Offlinewildskiller

    • Member
    • ****
    • Posts: 1,434
    • Thanks: +0/-0
      • View Profile
    Re: [PI/wL] NPC Spawns from json
    « Reply #4 on: July 10, 2014, 11:37:33 AM »
    I don't like JSON, but thanks!
    It's just like using XML but it's cleaner. JSON is easily parsed, understandable, and clean. I do believe Google and Yahoo use JSON for their web services and possibly other things.

    Offlinesk8rdude461

    • MOPARSCAPE WAS HACKED
    • Member
    • ****
    • *
    • Posts: 12,471
    • Thanks: +0/-0
      • View Profile
    Re: [PI/wL] NPC Spawns from json
    « Reply #5 on: July 10, 2014, 11:47:22 AM »
    I don't like JSON, but thanks!
    It's just like using XML but it's cleaner. JSON is easily parsed, understandable, and clean. I do believe Google and Yahoo use JSON for their web services and possibly other things.
    JSON also loads incredibly fast (esp. for how large some files can get). I've got 20k item definitions loading from a json file and it takes 1 maybe 2 seconds.

    OfflineDan0194

    • Member
    • ****
    • Posts: 1,140
    • Thanks: +0/-0
      • View Profile
    Re: [PI/wL] NPC Spawns from json
    « Reply #6 on: July 12, 2014, 08:09:05 AM »
    The loading for xml kind of sucks but I find it way more flexible than json.
    lol who told you winterLove had bad codes
    Smart man.
    if you read this your a fagit

    Offlinesk8rdude461

    • MOPARSCAPE WAS HACKED
    • Member
    • ****
    • *
    • Posts: 12,471
    • Thanks: +0/-0
      • View Profile
    Re: [PI/wL] NPC Spawns from json
    « Reply #7 on: July 12, 2014, 03:56:26 PM »
    The loading for xml kind of sucks but I find it way more flexible than json.
    I've always wanted to play with XML loading. I like JSON because it's fast and it has readability (compared to reading a cfg file with 1 item name and 15 seemingly random digits after it). I just haven't found anything in my RSPS that I'd want to convert over to it (and I haven't played with xml reading in java).

    OfflineDan0194

    • Member
    • ****
    • Posts: 1,140
    • Thanks: +0/-0
      • View Profile
    Re: [PI/wL] NPC Spawns from json
    « Reply #8 on: July 12, 2014, 04:31:11 PM »
    The loading for xml kind of sucks but I find it way more flexible than json.
    I've always wanted to play with XML loading. I like JSON because it's fast and it has readability (compared to reading a cfg file with 1 item name and 15 seemingly random digits after it). I just haven't found anything in my RSPS that I'd want to convert over to it (and I haven't played with xml reading in java).

    I say there both pretty the same when it comes to readability. The speed though for json is really the only reason why I would switch over from using xml most of the time.
    lol who told you winterLove had bad codes
    Smart man.
    if you read this your a fagit

    OfflineMichael P

    • Member
    • ****
    • Posts: 332
    • Thanks: +0/-0
      • View Profile
    Re: [PI/wL] NPC Spawns from json
    « Reply #9 on: July 12, 2014, 05:06:02 PM »
    Ugh you should really load NPC definitions in it's own file rather then setting it for each spawn.

    OfflineCres

    • Member
    • ****
    • Posts: 1,389
    • Thanks: +0/-0
      • View Profile
    Re: [PI/wL] NPC Spawns from json
    « Reply #10 on: July 12, 2014, 05:30:36 PM »
    Ugh you should really load NPC definitions in it's own file rather then setting it for each spawn.

    This. Also, player saving and loading can be made much more efficiently by using JSON. Why? Because you wouldn't be looping through the character file each time to find the token you want (since JSON is/can be loaded into Maps.)
    And I can find myself alone with just my thoughts
    As time crumbles away

    Avenged Sevenfold - Darkness Sourrounding

    OfflinePure_

    • Member
    • ****
    • *
    • Posts: 4,687
    • Thanks: +0/-0
      • View Profile
    Re: [PI/wL] NPC Spawns from json
    « Reply #11 on: July 12, 2014, 05:44:22 PM »
    Serialization would trim it all down.
    i won the forum

    Offlinesk8rdude461

    • MOPARSCAPE WAS HACKED
    • Member
    • ****
    • *
    • Posts: 12,471
    • Thanks: +0/-0
      • View Profile
    Re: [PI/wL] NPC Spawns from json
    « Reply #12 on: July 13, 2014, 01:20:21 AM »
    Ugh you should really load NPC definitions in it's own file rather then setting it for each spawn.
    I directly converted the current PI spawn system to JSON.

    Ugh you should really load NPC definitions in it's own file rather then setting it for each spawn.

    This. Also, player saving and loading can be made much more efficiently by using JSON. Why? Because you wouldn't be looping through the character file each time to find the token you want (since JSON is/can be loaded into Maps.)
    Why are you getting off topic about player saving?

    OfflineCres

    • Member
    • ****
    • Posts: 1,389
    • Thanks: +0/-0
      • View Profile
    Re: [PI/wL] NPC Spawns from json
    « Reply #13 on: July 13, 2014, 01:18:37 PM »
    Ugh you should really load NPC definitions in it's own file rather then setting it for each spawn.
    I directly converted the current PI spawn system to JSON.

    Ugh you should really load NPC definitions in it's own file rather then setting it for each spawn.

    This. Also, player saving and loading can be made much more efficiently by using JSON. Why? Because you wouldn't be looping through the character file each time to find the token you want (since JSON is/can be loaded into Maps.)
    Why are you getting off topic about player saving?

    The loading for xml kind of sucks but I find it way more flexible than json.
    I've always wanted to play with XML loading. I like JSON because it's fast and it has readability (compared to reading a cfg file with 1 item name and 15 seemingly random digits after it). I just haven't found anything in my RSPS that I'd want to convert over to it (and I haven't played with xml reading in java).
    And I can find myself alone with just my thoughts
    As time crumbles away

    Avenged Sevenfold - Darkness Sourrounding

    OfflineDan0194

    • Member
    • ****
    • Posts: 1,140
    • Thanks: +0/-0
      • View Profile
    Re: [PI/wL] NPC Spawns from json
    « Reply #14 on: July 13, 2014, 01:26:33 PM »
    Ugh you should really load NPC definitions in it's own file rather then setting it for each spawn.

    This. Also, player saving and loading can be made much more efficiently by using JSON. Why? Because you wouldn't be looping through the character file each time to find the token you want (since JSON is/can be loaded into Maps.)
    Umm buddy anything file can be stored as an object and be put into an array/map.
    lol who told you winterLove had bad codes
    Smart man.
    if you read this your a fagit

    OfflineCres

    • Member
    • ****
    • Posts: 1,389
    • Thanks: +0/-0
      • View Profile
    Re: [PI/wL] NPC Spawns from json
    « Reply #15 on: July 13, 2014, 01:46:20 PM »
    Ugh you should really load NPC definitions in it's own file rather then setting it for each spawn.

    This. Also, player saving and loading can be made much more efficiently by using JSON. Why? Because you wouldn't be looping through the character file each time to find the token you want (since JSON is/can be loaded into Maps.)
    Umm buddy anything file can be stored as an object and be put into an array/map.

    That's not what I was talking about. I'm talking about the efficiency with which the contents of a file are loaded onto the program.
    Why is this important? Because the longer it takes to save/load a player, the more load this will take on the cycle. This shouldn't be a problem for a system that uses a seperate thread to save players (albeit with concurrency issues) but most servers out there don't have this.
    And I can find myself alone with just my thoughts
    As time crumbles away

    Avenged Sevenfold - Darkness Sourrounding

    OfflineDan0194

    • Member
    • ****
    • Posts: 1,140
    • Thanks: +0/-0
      • View Profile
    Re: [PI/wL] NPC Spawns from json
    « Reply #16 on: July 13, 2014, 01:53:43 PM »
    Ugh you should really load NPC definitions in it's own file rather then setting it for each spawn.

    This. Also, player saving and loading can be made much more efficiently by using JSON. Why? Because you wouldn't be looping through the character file each time to find the token you want (since JSON is/can be loaded into Maps.)
    Umm buddy anything file can be stored as an object and be put into an array/map.

    That's not what I was talking about. I'm talking about the efficiency with which the contents of a file are loaded onto the program.
    Why is this important? Because the longer it takes to save/load a player, the more load this will take on the cycle. This shouldn't be a problem for a system that uses a seperate thread to save players (albeit with concurrency issues) but most servers out there don't have this.

    Yah you shouldn't be using xml for loading/saving player files. I though you ment that because you said
    Quote
    (since JSON is/can be loaded into Maps.)

    binary though would be the best root for saving and loading it's the fastest(usually).
    lol who told you winterLove had bad codes
    Smart man.
    if you read this your a fagit

    Offlinesini

    • Member
    • ****
    • *
    • *
    • Posts: 5,785
    • Thanks: +0/-0
      • View Profile
    Re: [PI/wL] NPC Spawns from json
    « Reply #17 on: July 13, 2014, 04:25:25 PM »
     final Gson gson

    God this bothers me

    Offlinesk8rdude461

    • MOPARSCAPE WAS HACKED
    • Member
    • ****
    • *
    • Posts: 12,471
    • Thanks: +0/-0
      • View Profile
    Re: [PI/wL] NPC Spawns from json
    « Reply #18 on: July 13, 2014, 04:40:21 PM »
    final Gson gson

    God this bothers me
    Mate I'm always open to learning more proper ways to handle my code.

    OfflineDan0194

    • Member
    • ****
    • Posts: 1,140
    • Thanks: +0/-0
      • View Profile
    Re: [PI/wL] NPC Spawns from json
    « Reply #19 on: July 13, 2014, 04:51:30 PM »
    final Gson gson

    God this bothers me
    Mate I'm always open to learning more proper ways to handle my code.

    I think he's saying there is really no point of the final keyword there.
    lol who told you winterLove had bad codes
    Smart man.
    if you read this your a fagit

     

    Copyright © 2017 MoparScape. All rights reserved.
    Powered by SMFPacks SEO Pro Mod |
    SimplePortal 2.3.5 © 2008-2012, SimplePortal