Jump to content

enum random()


Booleans YAY

Recommended Posts

The issue is that the tabs don't actually switch randomly. Code is compatible but not functioning right.

private static int rngTab = random(0, 125);  
	private enum Tabs {
		ATTACK(0, Tab.ATTACK);
		private int chance;
		private Tab tab;

		public int getChance() {
			return chance;
		}

		public Tab getTab() {
			return tab;
		}

		private static Set<Tabs> tab_set = Collections.unmodifiableSet(EnumSet.allOf(Tabs.class));

		public static Optional<Tabs> forTabs(int rng) {
			return tab_set.stream().filter(Objects::nonNull).filter(tabs -> tabs.getChance() == rng).findAny();
		}

		public static void randomTabs(Script script, int chance) {
			if (rngTab == forTabs(chance).get().getChance()) {
				script.getTabs().open(forTabs(chance).get().getTab());
			}
		}

		private Tabs(int chance, Tab tab) {
			this.chance = chance;
			this.tab = tab;
		}
	}
if (getState() == BotState.FISHING) {
			Tabs.randomTabs(this, 0);
			return;
		}
Link to comment
Share on other sites

 

The issue is that the tabs don't actually switch randomly. Code is compatible but not functioning right.

private static int rngTab = random(0, 125);  
	private enum Tabs {
		ATTACK(0, Tab.ATTACK);
		private int chance;
		private Tab tab;

		public int getChance() {
			return chance;
		}

		public Tab getTab() {
			return tab;
		}

		private static Set<Tabs> tab_set = Collections.unmodifiableSet(EnumSet.allOf(Tabs.class));

		public static Optional<Tabs> forTabs(int rng) {
			return tab_set.stream().filter(Objects::nonNull).filter(tabs -> tabs.getChance() == rng).findAny();
		}

		public static void randomTabs(Script script, int chance) {
			if (rngTab == forTabs(chance).get().getChance()) {
				script.getTabs().open(forTabs(chance).get().getTab());
			}
		}

		private Tabs(int chance, Tab tab) {
			this.chance = chance;
			this.tab = tab;
		}
	}
if (getState() == BotState.FISHING) {
			Tabs.randomTabs(this, 0);
			return;
		}

 

 

I know it's overkill, but little better than using switch.

 

Here is something a tad bit more clean and re-usable/generic :)

import java.util.Random;

public class EnumUtil {
    
    public static <T extends Enum<?>> T getRandomEnumValue(Class<T> clazz, Random random) {
        int constants = clazz.getEnumConstants().length;
        if(constants == 0) {
            throw new IllegalArgumentException("Enum class has no constants");
        }
        return clazz.getEnumConstants()[random.nextInt(constants)];
    }

    public static void main(String[] args) {
        Random  random = new Random();
        Tab tab = EnumUtil.getRandomEnumValue(Tab.class, random);
    }
    
}
  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...