Jump to content

enum random()


Recommended Posts

Posted

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;
		}
Posted

 

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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