Flub 1,282 Posted October 7, 2021 Report Share Posted October 7, 2021 Hey guys.. Had the need to re-write SlayerTasks.java for someone, figured I'd share it. It includes a kill requirement - if your server doesn't have that, just set the KC requirement to 0 or properly remove it if you know what you're doing package com.arlania.world.content.skill.impl.slayer; import com.arlania.model.Position; import com.arlania.model.Skill; import com.arlania.model.definitions.NpcDefinition; import com.arlania.util.Misc; import com.arlania.world.entity.impl.player.Player; import lombok.Getter; import java.util.ArrayList; import java.util.List; /** * @author Flub * https://discord.com/users/496606216047558667 */ public enum SlayerTasks { NO_TASK(null, -1, null, -1, null, 0), /** * Easy tasks */ NPC(SlayerMaster.VANNAKA, 1234, "Desc", 2100, new Position(3298, 2799, 0), 0), /** * Medium tasks */ NPC_2(SlayerMaster.DURADEL, 1234, "Desc", 6600, new Position(2724, 9821), 10), /** * Hard tasks */ NPC_3(SlayerMaster.KURADEL, 1234, "Desc", 16000, new Position(1761, 5329, 1), 1000), /** * Elite */ NPC_4(SlayerMaster.SUMONA, 1234, "Desc", 40000, new Position(3226, 2844), 2500), /** * Extreme - Bravek */ NPC_5(SlayerMaster.BRAVEK, 1234, "Desc", 100000, new Position(3176, 3029), 6500); public static List<SlayerTasks> EASY_TASKS = new ArrayList<>(); public static List<SlayerTasks> MEDIUM_TASKS = new ArrayList<>(); public static List<SlayerTasks> HARD_TASKS = new ArrayList<>(); public static List<SlayerTasks> ELITE_TASKS = new ArrayList<>(); public static List<SlayerTasks> EXTREME_TASKS = new ArrayList<>(); @Getter private SlayerMaster taskMaster; @Getter private int npcId; @Getter private String npcLocation; @Getter private int XP; @Getter private Position taskPosition; @Getter private int kcReq; SlayerTasks(SlayerMaster taskMaster, int npcId, String npcLocation, int XP, Position taskPosition, int kcReq) { this.taskMaster = taskMaster; this.npcId = npcId; this.npcLocation = npcLocation; this.XP = XP; this.taskPosition = taskPosition; this.kcReq = kcReq; } public static SlayerTasks forId(int id) { for (SlayerTasks tasks : SlayerTasks.values()) { if (tasks.ordinal() == id) { return tasks; } } return null; } /** * Add into your server start up code. * It fills the sets with up-to-date * task information. */ public static void fillSetsOnStartup() { int counter = 0; for (SlayerTasks task : SlayerTasks.values()) { counter++; if (task.getTaskMaster() == SlayerMaster.VANNAKA) EASY_TASKS.add(task); else if (task.getTaskMaster() == SlayerMaster.DURADEL) MEDIUM_TASKS.add(task); else if (task.getTaskMaster() == SlayerMaster.KURADEL) HARD_TASKS.add(task); else if (task.getTaskMaster() == SlayerMaster.SUMONA) ELITE_TASKS.add(task); else if (task.getTaskMaster() == SlayerMaster.BRAVEK) EXTREME_TASKS.add(task); System.out.println("Successfully added " + counter + " Slayer tasks"); } } private static boolean meetsTaskKC(Player player, SlayerTasks selectedTask) { return selectedTask != null && selectedTask.kcReq <= player.getNpcKillCount(selectedTask.getNpcId()); } private static boolean easyTaskAvailable(Player player) { int easyTasks = EASY_TASKS.size(); for (SlayerTasks easyTask : EASY_TASKS) { if (!meetsTaskKC(player, easyTask)) { easyTasks--; } } return easyTasks >= 1; } private static boolean medTaskAvailable(Player player) { int mediumTasks = MEDIUM_TASKS.size(); for (SlayerTasks mediumTask : MEDIUM_TASKS) { if (!meetsTaskKC(player, mediumTask)) { mediumTasks--; } } return mediumTasks >= 1; } private static boolean hardTaskAvailable(Player player) { int hardTasks = HARD_TASKS.size(); for (SlayerTasks hardTask : HARD_TASKS) { if (!meetsTaskKC(player, hardTask)) { hardTasks--; } } return hardTasks >= 1; } private static boolean eliteTaskAvailable(Player player) { int eliteTasks = ELITE_TASKS.size(); for (SlayerTasks eliteTask : ELITE_TASKS) { if (!meetsTaskKC(player, eliteTask)) { eliteTasks--; } } return eliteTasks >= 1; } private static boolean extremeTaskAvailable(Player player) { int extremeTasks = EXTREME_TASKS.size(); for (SlayerTasks extremeTask : EXTREME_TASKS) { if (!meetsTaskKC(player, extremeTask)) { extremeTasks--; } } return extremeTasks >= 1; } private static boolean repeatTask(Player player, SlayerTasks chosenTask) { return chosenTask != null && chosenTask.npcId == player.getSlayer().getLastTask().getNpcId(); } private static boolean meetsSlayerReq(Player player, SlayerTasks chosenTask) { return chosenTask.npcId > 0 && NpcDefinition.forId(chosenTask.getNpcId()).getSlayerLevel() <= player.getSkillManager().getMaxLevel(Skill.SLAYER); } private static boolean shouldGiveTask(Player player) { boolean giveTask = false; SlayerMaster currentSlayerMaster = player.getSlayer().getSlayerMaster(); switch (currentSlayerMaster) { case VANNAKA: if (easyTaskAvailable(player)) giveTask = true; else player.getPacketSender().sendMessage("No easy tasks available"); break; case DURADEL: if (medTaskAvailable(player)) giveTask = true; else player.getPacketSender().sendMessage("No medium tasks available"); break; case KURADEL: if (hardTaskAvailable(player)) giveTask = true; else player.getPacketSender().sendMessage("No hard tasks available"); break; case SUMONA: if (eliteTaskAvailable(player)) giveTask = true; else player.getPacketSender().sendMessage("No elite tasks available"); break; case BRAVEK: if (extremeTaskAvailable(player)) giveTask = true; else player.getPacketSender().sendMessage("No extreme tasks available"); break; } return giveTask; } public static int amountToKill(Player player) { int amount = Misc.random(15, 30); SlayerMaster currentSlayerMaster = player.getSlayer().getSlayerMaster(); switch (currentSlayerMaster) { case VANNAKA: amount += Misc.random(5, 10); break; case DURADEL: amount += Misc.random(5, 25); break; case KURADEL: amount += Misc.random(5, 60); break; case SUMONA: amount += Misc.random(5, 100); break; case BRAVEK: amount += Misc.random(10, 160); break; } return amount; } public static SlayerTasks getTask(Player player) { SlayerTasks selectedTask = null; SlayerMaster currentSlayerMaster = player.getSlayer().getSlayerMaster(); if (shouldGiveTask(player)) while ((!meetsTaskKC(player, selectedTask) || !meetsSlayerReq(player, selectedTask) || repeatTask(player, selectedTask))) { switch (currentSlayerMaster) { case VANNAKA: selectedTask = (SlayerTasks) Misc.randomFromList(EASY_TASKS); break; case DURADEL: selectedTask = (SlayerTasks) Misc.randomFromList(MEDIUM_TASKS); break; case KURADEL: selectedTask = (SlayerTasks) Misc.randomFromList(HARD_TASKS); break; case SUMONA: selectedTask = (SlayerTasks) Misc.randomFromList(ELITE_TASKS); break; case BRAVEK: selectedTask = (SlayerTasks) Misc.randomFromList(EXTREME_TASKS); break; } System.out.println("SELECTED TASK = " + selectedTask.name()); } return selectedTask; } @Override public String toString() { return Misc.ucFirst(name().toLowerCase().replaceAll("_", " ")); } } Adapted Slayer.java assignTask() Method to use the new data.. This is lazy work since I just changed where it gets the info from public void assignTask() { player.getPacketSender().sendRichPresenceState("Doing Slayer " + duoPartner + "!"); player.getPacketSender().sendSmallImageKey("slayer"); player.getPacketSender().sendRichPresenceSmallPictureText("Lvl: " + player.getSkillManager().getCurrentLevel(Skill.SLAYER)); boolean hasTask = getSlayerTask() != SlayerTasks.NO_TASK && player.getSlayer().getLastTask() != getSlayerTask(); if(hasTask) { player.getPacketSender().sendInterfaceRemoval(); return; } SlayerTasks chosenTask = SlayerTasks.getTask(player); player.getPacketSender().sendInterfaceRemoval(); this.amountToSlay = SlayerTasks.amountToKill(player); this.slayerTask = chosenTask; DialogueManager.start(player, SlayerDialogues.receivedTask(player, getSlayerMaster(), getSlayerTask())); PlayerPanel.refreshPanel(player); } 2 Link to comment Share on other sites More sharing options...
Masque 2,992 Posted October 7, 2021 Report Share Posted October 7, 2021 Valuable share, thank you! Link to comment Share on other sites More sharing options...
dangercroc 0 Posted October 10, 2021 Report Share Posted October 10, 2021 Damn that's insane thank you Link to comment Share on other sites More sharing options...
shadowisdom 1 Posted February 6, 2022 Report Share Posted February 6, 2022 thanks for this Link to comment Share on other sites More sharing options...
khonqr 0 Posted April 14, 2022 Report Share Posted April 14, 2022 thanks mate! Link to comment Share on other sites More sharing options...
Snooperal 0 Posted April 17, 2022 Report Share Posted April 17, 2022 looks great ty Link to comment Share on other sites More sharing options...
yoortjeeee 0 Posted April 21, 2022 Report Share Posted April 21, 2022 Thanks for this contribution. Amazing!! Link to comment Share on other sites More sharing options...
0117be 66 Posted June 2, 2022 Report Share Posted June 2, 2022 Nice contribution. Link to comment Share on other sites More sharing options...
iqux 0 Posted June 5, 2022 Report Share Posted June 5, 2022 Looks good! Link to comment Share on other sites More sharing options...
Anežka 3 Posted September 17, 2022 Report Share Posted September 17, 2022 Ahh this really isn't great enumerations are a strong way to give constants a bit more flavor context but that is really where it should end your packing so much stuff in your enum class is defeating the purpose of objective based programing. The point is to make things clean and simple without over engineering it, Java is already verbose enough think lazy when you create things it truly is the best practice. You have multiple methods doing the exact same check it can be condensed into one. public static void fillSetsOnStartup() { int counter = 0; for (SlayerTasks task : SlayerTasks.values()) { counter++; if (task.getTaskMaster() == SlayerMaster.VANNAKA) EASY_TASKS.add(task); else if (task.getTaskMaster() == SlayerMaster.DURADEL) MEDIUM_TASKS.add(task); else if (task.getTaskMaster() == SlayerMaster.KURADEL) HARD_TASKS.add(task); else if (task.getTaskMaster() == SlayerMaster.SUMONA) ELITE_TASKS.add(task); else if (task.getTaskMaster() == SlayerMaster.BRAVEK) EXTREME_TASKS.add(task); System.out.println("Successfully added " + counter + " Slayer tasks"); } } If data already exists don't duplicate it also this method and its call isn't needed to populate static collections you can simply call a static block static { } while ((!meetsTaskKC(player, selectedTask) || !meetsSlayerReq(player, selectedTask) || repeatTask(player, selectedTask))) { switch (currentSlayerMaster) { case VANNAKA: selectedTask = (SlayerTasks) Misc.randomFromList(EASY_TASKS); break; case DURADEL: selectedTask = (SlayerTasks) Misc.randomFromList(MEDIUM_TASKS); break; case KURADEL: selectedTask = (SlayerTasks) Misc.randomFromList(HARD_TASKS); break; case SUMONA: selectedTask = (SlayerTasks) Misc.randomFromList(ELITE_TASKS); break; case BRAVEK: selectedTask = (SlayerTasks) Misc.randomFromList(EXTREME_TASKS); break; } System.out.println("SELECTED TASK = " + selectedTask.name()); } Here you are using a while loop creating an imposed recursive iteration check, we have had Streams since java 8 should check into them you can filter out your data sets so they only contain what is currently usable. public static SlayerTasks forId(int id) { for (SlayerTasks tasks : SlayerTasks.values()) { if (tasks.ordinal() == id) { return tasks; } } return null; } This is opening up a chance for a null pointer exception look into Optionals also don't use ordinal() it value is exclusively order specific and any changes to the constants stack will break what is returned. SlayerTasks selectedTask = null; //this as well can give a null pointer exception. Optionals Optional<SlayerTask> o = Optional.empty(); o.isPresent(); o.isEmpty(); //optionals add a great deal of functional programming to java to make sure we don't need to use null which or and else functions. Link to comment Share on other sites More sharing options...
Gingizuo 3 Posted September 20, 2022 Report Share Posted September 20, 2022 Legend Link to comment Share on other sites More sharing options...
nsin25 0 Posted February 7, 2023 Report Share Posted February 7, 2023 Great little rewrite, gives room for some expansion, thank you! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now