Thread: [614/Matrix] Godsword Creation

Page 1 of 2 12 LastLast
Results 1 to 10 of 12
  1. #1 [614/Matrix] Godsword Creation 
    Extreme Donator


    Join Date
    Jun 2018
    Posts
    58
    Thanks given
    100
    Thanks received
    44
    Rep Power
    1323
    Edit: After reading what people have said, I did go way too far with the amount of enums I used. It could've just been simplified into one enum and handled the same. Here are the updates.
    I appreciate all the constructive criticism and feedback. I do disagree taking a more "OOP" approach by creating a new instance of GodswordHandler for simple use. The use of static is a better approach than creating a new instance of the class.

    Credits and a big thanks to Metorrite for a way to handle it differently.

    Spoiler for GodswordHandler.java:
    Code:
    package com.rs.game.player.content.godsword;
    
    import static com.rs.game.player.content.godsword.Godsword.ARMADYL_GODSWORD;
    
    import java.util.Arrays;
    import java.util.EnumSet;
    import java.util.List;
    import java.util.Optional;
    
    import com.rs.game.Animation;
    import com.rs.game.item.Item;
    import com.rs.game.player.Player;
    import com.rs.game.player.Skills;
    
    /**
     * 
     * @author Config / Mikey
     *
     */
    public class GodswordHandler {
    
    	private static final EnumSet<Godsword> godswordSet = EnumSet.allOf(Godsword.class);
    	private static final List<Item> SHARDS = Arrays.asList(new Item(11710), new Item(11712), new Item(11714));
    	public static final int GODSWORD_BLADE = 11690;
    
    	public static Godsword getComponent(int itemId) {
    		return godswordSet.stream().filter(x -> x.getGodswordId() == itemId).findFirst().orElse(null);
    
    	}
    
    	public static boolean isShard(int itemId) {
    		return SHARDS.contains(new Item(itemId));
    	}
    
    	public static void createGodswordBlade(Player player) {
    		if (player.getSkills().getLevel(Skills.SMITHING) < 80 && player.getInventory().containsItem(2347, 1)) {
    			player.getPackets()
    					.sendGameMessage("You must have 80 Smithing and all three shards to create a Godsword blade");
    			return;
    		}
    		player.lock(2);
    		player.setNextAnimation(new Animation(898));
    		player.getInventory().removeItems(SHARDS);
    		player.getInventory().addItem(new Item(GODSWORD_BLADE));
    		player.getSkills().addXp(Skills.SMITHING, 200);
    		player.getPackets().sendGameMessage("You have created a Godsword blade!");
    
    	}
    
    	public static void createGodsword(Player player, int itemId) {
    		Optional<Godsword> component = Optional.ofNullable(godswordSet.stream()
    				.filter(x -> x.getGodswordId() == itemId || x.getHiltId() == itemId).findAny().orElse(null));
    		player.getInventory().deleteItem(new Item(GODSWORD_BLADE));
    		player.getInventory().deleteItem(new Item(component.get().getHiltId()));
    		player.getInventory().addItem(new Item(component.get().getGodswordId()));
    		player.getDialogueManager().startDialogue(
    				"ItemMessage", "Congratulations, you have created "
    						+ (component.get().equals(ARMADYL_GODSWORD) ? "an " : "a ") + component.get() + ".",
    				component.get().getGodswordId());
    	}
    
    	public static void dismantleGodsword(Player player, Godsword godsword, int slot) {
    		if (player.getInventory().getFreeSlots() < 1) {
    			player.getPackets().sendGameMessage("You do not have enough inventory space.");
    			return;
    		}
    		player.getInventory().deleteItem(new Item(godsword.getGodswordId()));
    		player.getInventory().addItem(new Item(godsword.getHiltId()));
    		player.getInventory().addItem(new Item(GODSWORD_BLADE));
    		player.getInventory().refresh(slot);
    	}
    }


    Spoiler for Godsword.java:
    Code:
    package com.rs.game.player.content.godsword;
    
    import com.rs.game.item.Item;
    
    /**
     * 
     * @author Config
     *
     */
    public enum Godsword {
    	ARMADYL_GODSWORD(11694, 11702),
    	BANDOS_GODSWORD(11696, 11704), 
    	SARADOMIN_GODSWORD(11698, 11706), 
    	ZAMORAK_GODSWORD(11700, 11708);
    
    	private int godswordId;
    	private int hiltId;
    
    	private Godsword(int godswordId, int hiltId) {
    		this.godswordId = godswordId;
    		this.hiltId = hiltId;
    	}
    
    	public int getGodswordId() {
    		return this.godswordId;
    	}
    	
    	public int getHiltId() {
    		return this.hiltId;
    	}
    
    	@Override
    	public String toString() {
    		String godswordName = this.name().replace("_", " ");
    		return godswordName.substring(0, 1) + godswordName.substring(1).toLowerCase();
    	}
    }
    Last edited by mieky; 07-15-2018 at 10:27 AM.
    Reply With Quote  
     

  2. Thankful user:


  3. #2  
    Success is the worst teacher

    Santa Hat's Avatar
    Join Date
    Oct 2012
    Age
    27
    Posts
    3,334
    Thanks given
    807
    Thanks received
    1,185
    Rep Power
    190
    A boat load of code for such a simple process, nice use of OO but isn't really needed for this task. But nice job


    Reply With Quote  
     

  4. Thankful users:


  5. #3  
    Registered Member

    Join Date
    Sep 2014
    Posts
    2,080
    Thanks given
    3,003
    Thanks received
    1,114
    Rep Power
    5000
    Quote Originally Posted by Santa Hat View Post
    A boat load of code for such a simple process, nice use of OO but isn't really needed for this task. But nice job
    IKR, shoulda used the 50 if statements every other Matrix has aye.

    ps; we all know im the king mikey!
    Reply With Quote  
     

  6. Thankful users:


  7. #4  
    Extreme Donator


    Join Date
    Jun 2018
    Posts
    58
    Thanks given
    100
    Thanks received
    44
    Rep Power
    1323
    Quote Originally Posted by Santa Hat View Post
    A boat load of code for such a simple process, nice use of OO but isn't really needed for this task. But nice job
    Thanks! Unlike what Quacked(nub ) said, my priority was to avoid piling in if-statements and providing clean, readable code.
    Reply With Quote  
     

  8. #5  
    Registered Member
    Join Date
    Jul 2016
    Posts
    69
    Thanks given
    8
    Thanks received
    17
    Rep Power
    64
    You might want to use some for each loops, they tend to be more readable in cases like this. Moreover I think you make a little bit too much enums. I think there are alternatives that fit a use case like this way better. Making everything static isn't a great example of OOP too.
    Reply With Quote  
     

  9. #6  
    rage against the dying of the light


    Join Date
    Sep 2016
    Posts
    293
    Thanks given
    103
    Thanks received
    92
    Rep Power
    197
    Quote Originally Posted by Curiousity View Post
    I think you make a little bit too much enums.
    I agree that it does look odd to have enumerations in this case, but they do hold a higher advantage over constants in many cases

    Quote Originally Posted by Curiousity View Post
    Making everything static isn't a great example of OOP too.
    Static methods seem fitting assuming the GodswordHandler class will never need to be instantiated and the fields are defined without taking (unnecessary) additional memory
    Reply With Quote  
     

  10. Thankful user:


  11. #7  
    Registered Member
    Join Date
    Jul 2016
    Posts
    69
    Thanks given
    8
    Thanks received
    17
    Rep Power
    64
    Quote Originally Posted by Cipher View Post
    Static methods seem fitting assuming the GodswordHandler class will never need to be instantiated and the fields are defined without taking (unnecessary) additional memory
    Basically you are explaining what static means right here. Though I really do think this should be made OOP, the argument you give here can always be given to write statically.
    Reply With Quote  
     

  12. #8  
    Registered Member

    Join Date
    Dec 2009
    Posts
    774
    Thanks given
    367
    Thanks received
    455
    Rep Power
    927
    BrO WHERE IS THE HAMMER?
    link removed
    Reply With Quote  
     

  13. Thankful user:


  14. #9  
    rage against the dying of the light


    Join Date
    Sep 2016
    Posts
    293
    Thanks given
    103
    Thanks received
    92
    Rep Power
    197
    Quote Originally Posted by Curiousity View Post
    Basically you are explaining what static means right here. Though I really do think this should be made OOP, the argument you give here can always be given to write statically.
    I defined static because I was clarifying my point, but I would agree that there is a case to be made against the object-oriented nature of static fields and methods as they represent a global state and violate data encapsulation principles in an object. However, the variables and methods included in OP's post are instance-independent and will never need to be changed, which I believe proves acceptable use of the static modifier
    Reply With Quote  
     

  15. Thankful user:


  16. #10  
    Registered Member
    Stimulant's Avatar
    Join Date
    Jan 2013
    Age
    27
    Posts
    1,457
    Thanks given
    248
    Thanks received
    187
    Rep Power
    578
    So much classes, it can be done in a few lines.
    Reply With Quote  
     

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. 667 | 718 Godsword Creation
    By Ifti in forum Snippets
    Replies: 13
    Last Post: 07-21-2014, 12:27 PM
  2. [637/639] Godsword Creation and Dismantling
    By Gershon in forum Snippets
    Replies: 2
    Last Post: 11-05-2013, 01:55 PM
  3. [No Doze] Godsword Creation
    By LeBron James in forum Snippets
    Replies: 5
    Last Post: 08-14-2012, 06:47 PM
  4. [614]All Godsword Specials
    By Jerry` in forum Configuration
    Replies: 14
    Last Post: 09-18-2010, 09:46 PM
  5. Replies: 8
    Last Post: 09-01-2010, 10:24 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •