Thread: Attribute System - Includes Hiscore - Instant save - Display on line

Page 1 of 2 12 LastLast
Results 1 to 10 of 12
  1. #1 Attribute System - Includes Hiscore - Instant save - Display on line 
    Community Veteran

    Dexter Morgan's Avatar
    Join Date
    Nov 2008
    Age
    28
    Posts
    4,419
    Thanks given
    1,184
    Thanks received
    757
    Rep Power
    3098
    This is how I do attributes that are temporary and permanent. It uses an Enum. Please comment if you have suggestions of doing this better.

    Features include
    • Click button to force chat attribute state or amount
    • Display all attributes on a tab interface
    • Display top players online related to the attribute
    • Integer and boolean
    • Very simple, clean, and friendly


    Call usage
    Code:
    player.getAttribute().get(Attribute.EXAMPLE_1);
    Code:
    package ethos.model.players;
    
    import java.text.NumberFormat;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.HashMap;
    import java.util.List;
    import ethos.util.Misc;
    
    /**
     * Handles attributes for players
     * 
     * @author 2012  <http://www.rune-server.org/members/dexter+morgan/>
     */
    public class AttributeManager {
    
    	/**
    	 * The attribute
    	 */
    	private HashMap<Attribute, Integer> attribute = new HashMap<Attribute, Integer>();
    
    	/**
    	 * The attribute
    	 */
    	public enum Attribute {
    
    		// 6986 is the tab line id where the points are displayed
    		// true means it is saved
    		// 19000 is the hiscore line id
    		EXAMPLE_1(6986, true, 19000),
    
    		;
    
    		/**
    		 * The string id
    		 */
    		private int id;
    
    		/**
    		 * The hiscore id
    		 */
    		private int hiscore;
    
    		/**
    		 * Whether to save
    		 */
    		private boolean save;
    
    		/**
    		 * Sets the id
    		 * 
    		 * @param id the id
    		 * @param safe whether is saved in file
    		 * @param hiscore the hiscore id
    		 */
    		Attribute(int id, boolean safe, int hiscore) {
    			this.id = id;
    			this.save = save;
    			this.hiscore = hiscore;
    		}
    
    		/**
    		 * Save by default with no id 
    		 */
    		Attribute() {
    			this(0, true, 0);
    		}
    
    		/**
    		 * Specify save setting with no id
    		 */
    		Attribute(boolean safe) {
    			this(0, safe, 0);
    		}
    
    		/**
    		 * Specify id with save setting
    		 */
    		Attribute(int id, boolean safe) {
    			this(id, safe, 0);
    		}
    
    		/**
    		 * Save setting with hiscore
    		 */
    		Attribute(boolean safe, int hiscore) {
    			this(0, safe, hiscore);
    		}
    
    		/**
    		 * Sets the id
    		 * 
    		 * @return the id
    		 */
    		public int getId() {
    			return id;
    		}
    
    		/**
    		 * Sets the save
    		 * 
    		 * @return the save
    		 */
    		public boolean isSave() {
    			return save;
    		}
    
    		/**
    		 * Gets the hiscore
    		 *
    		 * @return the hiscore
    		 */
    		public int getHiscore() {
    			return hiscore;
    		}
    
    		/**
    		 * Gets the attribute by button
    		 * 
    		 * @param button the button
    		 * @return the attribute
    		 */
    		public static Attribute getForButton(int button) {
    			return Arrays.stream(values()).filter(c -> c.getHiscore() == button).findFirst()
    					.orElse(null);
    		}
    	}
    
    	/**
    	 * Sends the chat
    	 * 
    	 * @param player the player
    	 * @param button the button
    	 * @return the chat
    	 */
    	public static boolean sendChat(Player player, int button) {
    		for (Attribute attribute : Attribute.values()) {
    			if (attribute.getId() - 1 == button) {
    				String name = Misc.capitalize(attribute.name().toLowerCase());
    				String suffix = name.endsWith("s") ? "are" : "is";
    				String string =
    						"My " + name + " " + suffix + ": " + player.getAttribute().display(attribute);
    				player.getPA().sendString(attribute.getId(), player.getAttribute().display(attribute));
    				player.forcedChat(string.replaceAll("_", " "));
    				return true;
    			}
    		}
    		return false;
    	}
    
    	/**
    	 * Sending the tab
    	 * 
    	 * @param player the player
    	 */
    	public static void sendTab(Player player) {
    		for (Attribute points : Attribute.values()) {
    			if (points.getId() == 0) {
    				continue;
    			}
    			player.getPA().sendString(points.getId(), player.getAttribute().display(points));
    		}
    	}
    
    	/**
    	 * Gets the top players online
    	 * 
    	 * @param key the key
    	 * @param amount the amount
    	 * @param type the type
    	 */
    	public static List<Player> sendTopPlayersOnline(Attribute key, int amount, int type) {
    		List<Player> online = new ArrayList<Player>();
    		if (PlayerHandler.getPlayerCount() < amount) {
    			amount = PlayerHandler.getPlayerCount();
    		}
    		for (Player players : PlayerHandler.players) {
    			if (players == null) {
    				continue;
    			}
    			online.add(players);
    		}
    		Collections.sort(online, new Comparator<Player>() {
    
    			@Override
    			public int compare(Player o1, Player o2) {
    
    				double type1 = o1.getAttribute().get(key);
    				double type2 = o2.getAttribute().get(key);
    
    				if (type == 1) {
    					if (type1 == type2) {
    						return 0;
    					} else if (type1 > type2
    							&& o1.getAttribute().get(key) > o2.getAttribute().get(key)) {
    						return 1;
    					} else {
    						return -1;
    					}
    				} else {
    					if (type1 == type2) {
    						return 0;
    					} else if (type1 > type2) {
    						return -1;
    					} else {
    						return 1;
    					}
    				}
    			}
    		});
    		return online;
    	}
    
    	/**
    	 * Displays the attribute
    	 * 
    	 * @param attribute the attribute
    	 * @return the attribute
    	 */
    	public String display(Attribute attribute) {
    		return NumberFormat.getInstance().format(get(attribute));
    	}
    
    	/**
    	 * Creates a new attribute
    	 * 
    	 * @param key the attribute
    	 */
    	public void create(Attribute key) {
    		attribute.put(key, 0);
    	}
    
    	/**
    	 * Gets the attribute
    	 * 
    	 * @param key the attribute
    	 * @return the attribute
    	 */
    	public int get(Attribute key) {
    		return attribute.get(key) == null ? 0 : attribute.get(key);
    	}
    
    	/**
    	 * Checks the attribute
    	 * 
    	 * @param key the attribute
    	 * @return the attribute
    	 */
    	public boolean check(Attribute key) {
    		return attribute.get(key) == null ? false : attribute.get(key).intValue() == 1;
    	}
    
    	/**
    	 * Toggles the attribute
    	 * 
    	 * @param key the attribute
    	 */
    	public void toggle(Attribute key) {
    		set(key, !check(key));
    	}
    
    	/**
    	 * Sets the attribute
    	 * 
    	 * @param key the attribute
    	 * @param state the state
    	 */
    	public void set(Attribute key, boolean state) {
    		if (attribute.get(key) == null) {
    			create(key);
    		}
    		attribute.put(key, state ? 1 : 0);
    	}
    
    	/**
    	 * Sets the attribute
    	 * 
    	 * @param key the attribute
    	 * @param value the value
    	 */
    	public void set(Attribute key, int value) {
    		if (attribute.get(key) == null) {
    			create(key);
    		}
    		attribute.put(key, value);
    	}
    
    	/**
    	 * Increases the attribute
    	 * 
    	 * @param key the attribute
    	 */
    	public void increase(Attribute key) {
    		increase(key, 1);
    	}
    
    	/**
    	 * Increases by amount
    	 * 
    	 * @param key the attribute
    	 * @param value the value
    	 */
    	public void increase(Attribute key, int value) {
    		if (attribute.get(key) == null) {
    			create(key);
    		}
    		attribute.put(key, attribute.get(key) + value);
    	}
    
    	/**
    	 * Decreases the attribute
    	 * 
    	 * @param key the attribute
    	 * @return the attribute
    	 */
    	public boolean decrease(Attribute key) {
    		if (attribute.get(key) == null) {
    			create(key);
    		}
    		if (attribute.get(key) < 1) {
    			return false;
    		}
    		set(key, attribute.get(key) - 1);
    		return true;
    	}
    
    	/**
    	 * Decreases the attribute
    	 * 
    	 * @param key the key
    	 * @param amount the amount
    	 */
    	public void decrease(Attribute key, long amount) {
    		for (int i = 1; i <= amount; i++) {
    			if (!decrease(key)) {
    				break;
    			}
    		}
    	}
    
    	/**
    	 * Gets the attributes
    	 * 
    	 * @return the attributes
    	 */
    	public HashMap<Attribute, Integer> get() {
    		return attribute;
    	}
    }
    Saving

    Code:
    			for (Attribute points : Attribute.values()) {
    				if (!points.isSave()) {
    					continue;
    				}
    				int value = player.getPoints().get(points);
    				String name = points.name();
    				object.addProperty(name, value);
    			}
    Loading

    Code:
    			for (Attribute points : Attribute.values()) {
    				if (reader.has(points.name())) {
    					int value = reader.get(points.name()).getAsInt();
    					player.getPoints().set(points, value);
    				}
    			}
    Hiscore can be used such as

    Attached image
    Reply With Quote  
     

  2. Thankful user:


  3. #2  
    Donator
    Esper's Avatar
    Join Date
    Jun 2011
    Age
    30
    Posts
    108
    Thanks given
    13
    Thanks received
    6
    Rep Power
    1
    Thanks! I plan on doing stuffs with this.
    Reply With Quote  
     

  4. #3  
    Banned
    Join Date
    Dec 2018
    Posts
    119
    Thanks given
    1
    Thanks received
    42
    Rep Power
    0
    great stuff as allways
    Last edited by 404 Not Found; 03-06-2019 at 12:02 AM.
    Reply With Quote  
     

  5. #4  
    Registered Member
    Optimum's Avatar
    Join Date
    Apr 2012
    Posts
    3,570
    Thanks given
    871
    Thanks received
    1,745
    Rep Power
    5000
    bro wtf is going on with ur commenting?

    you forgot to comment this
    Code:
    			@Override
    			public int compare(Player o1, Player o2) {
    this code is incomplete
    Last edited by Optimum; 03-06-2019 at 12:39 AM.

    Quote Originally Posted by DownGrade View Post
    Don't let these no life creeps get to you, its always the same on here. They'd rather spend hours upon hours in the rune-server spam section then getting laid! ha ha!Its honestly pathetic i haven't seen so many lowlifes in my life its actually insane i wish that this section would just vanish its probably the only way to get these people out of the community...
    PLEASE BE AWARE OF IMPOSTERS MY DISCORD ID: 362240000760348683
    Reply With Quote  
     


  6. #5  
    Registered Member

    Join Date
    Oct 2017
    Age
    23
    Posts
    215
    Thanks given
    211
    Thanks received
    113
    Rep Power
    300
    I do have a suggestion.

    Code:
    Attribute.values()

    avoid accessing enum value's like that ^.


    Instead.

    Code:
    	private final Attribute[] infomation = Attribute.values();
    Spoiler for signature too large:
    end me
    Attached image
    Attached image
    Reply With Quote  
     


  7. #6  
    Donator

    Jason's Avatar
    Join Date
    Aug 2009
    Posts
    6,092
    Thanks given
    2,402
    Thanks received
    2,823
    Rep Power
    4550
    Quote Originally Posted by Bitshifting View Post
    I do have a suggestion.

    Code:
    Attribute.values()

    avoid accessing enum value's like that ^.


    Instead.

    Code:
    	private final Attribute[] infomation = Attribute.values();
    That is a really good point because every reference to values() returns a clone of the original.

    That being said, you shouldn't use an array because this is valid.

    Code:
    information[0] = null;
    Instead, use an immutable implementation of Collection.

    (Iirc - hope I'm not wrong haha - let me know if I am)
    Reply With Quote  
     


  8. #7  
    PokeFrontier Java Developer

    Pokemon's Avatar
    Join Date
    May 2011
    Posts
    2,726
    Thanks given
    495
    Thanks received
    807
    Rep Power
    1260
    Attached image

    https://softwareengineering.stackexc...rmine-behavior

    Code:
    if (attribute.getId() - 1 == button) {
    ?

    Code:
    public class AttributeManager {
    
    	/**
    	 * The attribute
    	 */
    	private HashMap<Attribute, Integer> attribute = new HashMap<Attribute, Integer>();
    
    	/**
    	 * The player
    	 */
    	private Player player;
    ???

    What's the point of having a specific player part of the attribute manager that from what i see just processes the top players and it's not even used in any of the code. You are using parameters for any other player var names in your methods


    Attached image


    I love
    Reply With Quote  
     

  9. Thankful users:


  10. #8  
    Registered Member
    Join Date
    Dec 2013
    Posts
    419
    Thanks given
    127
    Thanks received
    85
    Rep Power
    349
    Quality of code aside(yuck), it works and suitable for anybody that just wants to plug the system into their servers, good job
    Reply With Quote  
     

  11. #9  
    Software Developer

    Tyrant's Avatar
    Join Date
    Jul 2013
    Age
    24
    Posts
    1,562
    Thanks given
    678
    Thanks received
    423
    Rep Power
    1060
    Quote Originally Posted by Dexter Morgan View Post
    Chillout you kiddo. There was code using it but I removed the code and forgot to remove that.



    Jump on the bandwagon.


    You guys are making a big deal for 3 methods having a few unnecessary comments. I got it the first time. No need for other geniuses to get involved. The only sad thing is, now I have to go through my whole server and remove the comments..
    Why are you being offended by someone critisiming you? The code is poorly written, and if I cared enough I'd rewrite it. And also, "I removed the code and forgot to remove that.", check out on what you post before posting,
    otherwise don't be mad when people comment on that because at the end of the day it is your fault...
    Reply With Quote  
     

  12. Thankful users:


  13. #10  
    Blurite

    Corey's Avatar
    Join Date
    Feb 2012
    Age
    26
    Posts
    1,491
    Thanks given
    1,245
    Thanks received
    1,729
    Rep Power
    5000
    Quote Originally Posted by Dexter Morgan View Post
    What a way to flip the table. You seem mad..? What's up with the random shit you just said? I know pokemon, he's a cool kid.
    No you fool, I'm not offended, I actually even asked for feedback. I didn't see that and pokemon mentioned it and I removed it, simple enough. Thanks Pokemon (Y).
    What's annoying though is those who criticize and have nothing to say other than "shit code" and heroes like you. And that corey idiot too lol. Hey Im naturally passively aggressive.. So.. ye.
    I like to learn and I am sure Jason can vouch this, I worked with him in the past and learned a lot from him. I get it, it's over commented and don't use enum values().

    Can you tell me hows the code poorly written please wanna hear it from a genius person like you, and don't repeat what others have said..
    Calm down you dense motherfucker, one person mentioned your over commenting and two people not being very constructive after which you become incredibly defensive and start calling people names.
    How about instead of trying to start a fight with everyone who actually bothered to comment you instead ask "what's yuck about my code? Care to elaborate?"
    Chill brother


    As far as feedback goes, I have nothing much to add which hasn't been said already (over commenting, odd formatting which is likely accidental and not using enum.values()) other than:
    - if you're gonna comment things, make it actually meaningful; e.g. "Checks the attribute" - what is being checked?
    - maybe move the static methods elsewhere, such as where the interface is handled
    - using something like lombok (or kotlin) to remove a lot of the boilerplate (all the boring getters, setters, constructors) that comes with Java, but that's just me disliking Java
    - fastutils Object2IntMap > HashMap<Attribute, Integer>

    The system itself is pretty basic so there's not really much to review.
    Last edited by Corey; 03-10-2019 at 08:54 PM.
    Attached image
    Reply With Quote  
     

  14. Thankful users:


Page 1 of 2 12 LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Replies: 23
    Last Post: 03-09-2012, 06:13 PM
  2. Replies: 109
    Last Post: 05-22-2009, 04:42 PM
  3. Instant dds hit [ON NPC] [DELTA]
    By Coder Alex in forum Tutorials
    Replies: 8
    Last Post: 04-17-2009, 10:05 PM
  4. Replies: 5
    Last Post: 01-27-2009, 03:01 AM
  5. Replies: 1
    Last Post: 01-03-2009, 11:32 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •