Thread: [Asteria 3.0] Degradable Item.

Page 1 of 3 123 LastLast
Results 1 to 10 of 21
  1. #1 [Asteria 3.0] Degradable Item. 
    Registered Member
    Stanaveli's Avatar
    Join Date
    Aug 2014
    Posts
    1,489
    Thanks given
    179
    Thanks received
    651
    Rep Power
    1311
    So yeah, I saw a post in the help section regarding this subject and I thought why not release a snippet. What this does is chain a life state on an item. I left the functionality of decreasing the health out, because this can be done in various ways. You still need to do the actual functionality of decreasing the health yourself.

    Replace your item class with this. (What I did was make the class non-final, make some methods final which shouldn't be modified in sub-classes)

    Code:
    package net.ar.game.item;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import com.google.common.collect.Iterables;
    
    /**
     * The container class that represents an item that can be interacted with.
     * @author lare96 <http://github.com/lare96>
     */
    public class Item {
    
    	/**
    	 * The identification of this item.
    	 */
    	private int id;
    
    	/**
    	 * The quantity of this item.
    	 */
    	private int amount;
    	
    	/**
    	 * Creates a new {@link Item}.
    	 * @param id     the identification of this item.
    	 * @param amount the quantity of this item.
    	 */
    	public Item(int id, int amount) {
    		if(amount < 0)
    			amount = 0;
    		this.id = id;
    		this.amount = amount;
    	}
    
    	/**
    	 * Creates a new {@link Item} with an quantity of {@code 1}.
    	 * @param id the identification of this item.
    	 */
    	public Item(int id) {
    		this(id, 1);
    	}
    	
    	@Override
    	public final String toString() {
    		return "ITEM[id= " + id + ", amount= " + amount + ", degradable= " + degradable + "]";
    	}
    
    	@Override
    	public final int hashCode() {
    		final int prime = 31;
    		int result = 1;
    		result = prime * result + amount;
    		result = prime * result + id;
    		return result;
    	}
    
    	@Override
    	public final boolean equals(Object obj) {
    		if(this == obj)
    			return true;
    		if(obj == null)
    			return false;
    		if(!(obj instanceof Item))
    			return false;
    		Item other = (Item) obj;
    		if(amount != other.amount)
    			return false;
    		if(id != other.id)
    			return false;
    		return true;
    	}
    	
    	/**
    	 * Converts an int array into an {@link Item} array.
    	 * @param id the array to convert into an item array.
    	 * @return the item array containing the values from the int array.
    	 */
    	public static final Item[] convert(int... id) {
    		List<Item> items = new ArrayList<>();
    		for(int identifier : id) {
    			items.add(new Item(identifier));
    		}
    		return Iterables.toArray(items, Item.class);
    	}
    
    	/**
    	 * Determines if {@code item} is valid. In other words, determines if
    	 * {@code item} is not {@code null} and the {@link Item#id} and
    	 * {@link Item#amount} are above {@code 0}.
    	 * @param item the item to determine if valid.
    	 * @return {@code true} if the item is valid, {@code false} otherwise.
    	 */
    	public static final boolean valid(Item item) {
    		return item != null && item.id > 0 && item.amount > 0;
    	}
    
    	/**
    	 * A substitute for {@link Object#clone()} that creates another 'copy' of
    	 * this instance. The created copy <i>safe</i> meaning it does not hold
    	 * <b>any</b> references to the original instance.
    	 * @return the copy of this instance that does not hold any references.
    	 */
    	public Item copy() {
    		return new Item(id, amount);
    	}
    
    	/**
    	 * Increments the amount by {@code 1}.
    	 */
    	public final void incrementAmount() {
    		incrementAmountBy(1);
    	}
    
    	/**
    	 * Decrements the amount by {@code 1}.
    	 */
    	public final void decrementAmount() {
    		decrementAmountBy(1);
    	}
    
    	/**
    	 * Increments the amount by {@code amount}.
    	 * @param amount the amount to increment by.
    	 */
    	public final void incrementAmountBy(int amount) {
    		this.amount += amount;
    	}
    
    	/**
    	 * Decrements the amount by {@code amount}
    	 * @param amount the amount to decrement by.
    	 */
    	public final void decrementAmountBy(int amount) {
    		if((this.amount - amount) < 1) {
    			this.amount = 0;
    		} else {
    			this.amount -= amount;
    		}
    	}
    
    	/**
    	 * Gets the item definition for the item identifier.
    	 * @return the item definition.
    	 */
    	public final ItemDefinition getDefinition() {
    		return ItemDefinition.DEFINITIONS[id];
    	}
    
    	/**
    	 * Gets the identification of this item.
    	 * @return the identification.
    	 */
    	public final int getId() {
    		return id;
    	}
    
    	/**
    	 * Sets the identification of this item.
    	 * @param id the new identification of this item.
    	 */
    	public final void setId(int id) {
    		this.id = id;
    	}
    
    	/**
    	 * Gets the quantity of this item.
    	 * @return the quantity.
    	 */
    	public final int getAmount() {
    		return amount;
    	}
    
    	/**
    	 * Sets the quantity of this item.
    	 * @param amount the new quantity of this item.
    	 */
    	public final void setAmount(int amount) {
    		if(amount < 0)
    			amount = 0;
    		this.amount = amount;
    	}
    }
    and here is the DegradableItem class.

    Code:
    package net.ar.game.item;
    
    import java.util.function.Consumer;
    
    import net.ar.game.character.player.Player;
    
    /**
     * Represents a single degradable item.
     * @author <a href="http://www.rune-server.ee/members/stand+up/">Stand Up</a>
     */
    public final class DegradableItem extends Item {
    	
    	/**
    	 * The health of this degradable item.
    	 */
    	private final int health;
    	
    	/**
    	 * The action to complete on degradation.
    	 */
    	private final Consumer<Player> action;
    	
    	/**
    	 * Constructs a new {@link DegradableItem}
    	 * @param id		{@link #getId()}
    	 * @param health	{@link #health}.
    	 * @param action	{@link #action}.
    	 */
    	public DegradableItem(int id, int health, Consumer<Player> action) {
    		super(id, 1);
    		this.health = health;
    		this.action = action;
    		this.setDegradable(true);
    	}
    
    	/**
    	 * Constructs a new {@link DegradableItem}
    	 * @param item		the item this degradable instance is being created for.
    	 * @param amount	{@link #getAmount()}.
    	 * @param health	{@link #health}.
    	 * @param action	{@link #action}.
    	 */
    	public DegradableItem(Item item, int health, Consumer<Player> action) {
    		this(item.getId(), health, action);
    	}
    	
    	@Override
    	public Item copy() {
    		return new DegradableItem(getId(), getAmount(), action);
    	}
    
    	/**
    	 * @return the health
    	 */
    	public int getHealth() {
    		return health;
    	}
    
    	/**
    	 * @return the action
    	 */
    	public Consumer<Player> getAction() {
    		return action;
    	}
    }
    I wouldn't suggest removing the consumer, you can accept the action when the item degrades. Perhaps overload the constructor to define a standard action on degradation.
    ~No honour among thieves.



    [Only registered and activated users can see links. ]

    Reply With Quote  
     

  2. Thankful user:


  3. #2  
    Banned Market Banned Market Banned

    -3clipse-'s Avatar
    Join Date
    May 2015
    Posts
    841
    Thanks given
    101
    Thanks received
    311
    Rep Power
    389
    Nice release, Stan
    [Only registered and activated users can see links. ]
    Reply With Quote  
     

  4. #3  
    Registered Member

    Join Date
    Sep 2008
    Posts
    4,830
    Thanks given
    893
    Thanks received
    1,439
    Discord
    View profile
    Rep Power
    2924
    Yeah, this is probably one of the better discussed ways to do this also fyi LinkedLists don't allow duplicates
    "Know thy self, know thy enemy. A thousand battles, a thousand victories." - Sun Tzu
    GitHub: [Only registered and activated users can see links. ]
    Reply With Quote  
     

  5. #4  
    Registered Member
    Stanaveli's Avatar
    Join Date
    Aug 2014
    Posts
    1,489
    Thanks given
    179
    Thanks received
    651
    Rep Power
    1311
    Quote Originally Posted by Faris View Post
    Yeah, this is probably one of the better discussed ways to do this also fyi LinkedLists don't allow duplicates
    Sets are still faster.

    Imagine a player having hundreds of barrows sets (Wouldn't be surprised thinking of how some servers handle their economy) things could run slower eventually.
    ~No honour among thieves.



    [Only registered and activated users can see links. ]

    Reply With Quote  
     

  6. #5  
    Super Donator


    Join Date
    Feb 2011
    Age
    24
    Posts
    1,136
    Thanks given
    180
    Thanks received
    178
    Rep Power
    243
    Nice release
    Reply With Quote  
     

  7. #6  
    Registered Member

    Join Date
    Sep 2008
    Posts
    4,830
    Thanks given
    893
    Thanks received
    1,439
    Discord
    View profile
    Rep Power
    2924
    Quote Originally Posted by Stanaveli View Post
    Sets are still faster.

    Imagine a player having hundreds of barrows sets (Wouldn't be surprised thinking of how some servers handle their economy) things could run slower eventually.
    Actually, iterating through lists is faster than through sets, also lists use up quite a bit less memory.
    "Know thy self, know thy enemy. A thousand battles, a thousand victories." - Sun Tzu
    GitHub: [Only registered and activated users can see links. ]
    Reply With Quote  
     

  8. #7  
    Registered Member
    Stanaveli's Avatar
    Join Date
    Aug 2014
    Posts
    1,489
    Thanks given
    179
    Thanks received
    651
    Rep Power
    1311
    Quote Originally Posted by Faris View Post
    Actually, iterating through lists is faster than through sets, also lists use up quite a bit less memory.
    HashSet look-up is constant time O(1), Linear look-up from a list is linear time O(n ), I personally think look-up time is more crucial.
    ~No honour among thieves.



    [Only registered and activated users can see links. ]

    Reply With Quote  
     

  9. #8  
    Banned
    Join Date
    Dec 2015
    Posts
    75
    Thanks given
    17
    Thanks received
    10
    Rep Power
    0
    Nice release man, thanks for the contribution
    Reply With Quote  
     

  10. #9  
    Renown Programmer & Respected Member

    Ryley's Avatar
    Join Date
    Aug 2011
    Posts
    597
    Thanks given
    253
    Thanks received
    520
    Discord
    View profile
    Rep Power
    1332
    Quote Originally Posted by Stanaveli View Post
    HashSet look-up is constant time O(1), Linear look-up from a list is linear time O(n ), I personally think look-up time is more crucial.
    Except look-ups don't _really_ matter in this case because you would need the value. HashSet#get(index) does not exist. A set is a collection which treats if a == b as duplicates, so it doesn't make sense to try to get the same object you already have. (Unless you use something like Iterables.get(set, index); and if you need to do this then you should re-think your collection choice.). A Map is likely more appropriate over a set.
    Reply With Quote  
     

  11. Thankful user:


  12. #10  
    Donator

    Jason's Avatar
    Join Date
    Aug 2009
    Posts
    6,108
    Thanks given
    2,402
    Thanks received
    2,825
    Rep Power
    4604
    Just a few things I want to point out and mention. For starters the equals function in Item is over complicated. The keyword instanceof checks for a null reference so you should make use of that.

    Code:
        @Override
        public boolean equals(Object object) {
            if (object instanceof Item) {
                if (object == this) {
                    return true;
                }
                Item other = (Item) object;
    
    
                return id == other.id && amount == other.amount;
            }
            return false;
        }
    Why doesn't the Item class contain a UUID object reference? That would be extremely useful in the equals function when determining if two objects are the same. In regards to your hashCode() function, make use of Objects#hash.

    Code:
    private final int hashCode;
    
    public Item(...) {
       ...
       hashCode = Objects.hash(id, amount);
    }
    
    @Override
    public int hashCode() {
        return hashCode;
    }
    Reply With Quote  
     

Page 1 of 3 123 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. [Asteria 3.0] Hunter Item Sets.
    By Stanaveli in forum Snippets
    Replies: 1
    Last Post: 08-13-2015, 03:57 AM
  2. 718 Degradable Items
    By Peepaw in forum Help
    Replies: 2
    Last Post: 11-13-2014, 06:03 PM
  3. Degrading Items not working
    By Verisimilitude in forum Help
    Replies: 1
    Last Post: 09-29-2014, 05:06 PM
  4. [AZURE] Damaged/Degrading Items
    By DestriX in forum Snippets
    Replies: 3
    Last Post: 12-25-2012, 11:38 PM
  5. [DSPK] Degrading Items
    By gold_fish in forum Help
    Replies: 0
    Last Post: 02-21-2011, 03:33 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
  •