Hi! I’m trying to make the following work like a regular list.get(rand.nextint(list.size())); however it constantly…is trying to do only giants…every single time…and there’s no variation…whatso ever…helP?
int test = DayCounter.getWeeklyConfig();
public String Config = "Config" + test;
public void Hostiles(Location<World> spawnLocation) {
List<Class<? extends Entity>> classes = ImmutableList.of(
Monster.class
);
Collection<EntityType> cet = Sponge.getRegistry().getAllOf(EntityType.class).stream().filter(x -> classes.stream().anyMatch(y -> y.isAssignableFrom(x.getEntityClass()))).collect(Collectors.toList());
List<EntityType>list = Arrays.asList(cet.iterator().next());
Random rand = new Random();
EntityType listResult = list.get(rand.nextInt(list.size()));
String week = "WEEK " + DayCounter.getCustWeek();
if(listResult != null){
for (int i = 0; i< (ConfigurationManager.getInstance().getSpawnControl().getNode(listResult.getName(), week, "==========Natural Spawning==========", "# of Attempts: ").getInt()); i++){
Random roll = new Random();
int answer = roll.nextInt(100) + 1;
if (answer <= (ConfigurationManager.getInstance().getSpawnControl().getNode(listResult.getName(), week, "==========Natural Spawning==========", "% per Attempt: ").getInt())){
Extent extent = spawnLocation.getExtent();
Entity entity = extent.createEntity(listResult, spawnLocation.getPosition());
Entity creeper = entity;
extent.spawnEntity(creeper);
Sponge.getServer().getBroadcastChannel().send(Text.of(creeper));
}
}
}
};
Due to the fact computers work on logic and random things are not logical at all, the Random class will not actually generate random numbers. I personally create my “random” numbers using the system time, due to the fact when a user enters a command or starts the server, it is unlikely to be the same time as the previous time the server started or a user ran a command.
…ok. Now I’m not creating a random number…I’m attempting to get a randaom Entiity from within the Array…sor of like how this works.
List<String> list = Arrays.asList("blaze","cave_spider","creeper","enderman","endermite","giant","husk","magma_cube","silverfish","skeleton","slime","spider","vex","witch","wither_skeleton","zombie","zombie_pigman","NONE");
Random rand = new Random();
String listResult = list.get(rand.nextInt(list.size()));
when the String ListResult is called each time it calls a different Entity from that Array called list, sometimes its a creeper sometimes it’s a blaze…however the problem I’m having is that I from this ListResult above with the array…I cannot convert it to an EntityType for the extent.createEntity portion of the code.
the nextInt is a random number. Thats what i meant.
aaaaah gotcha. Yea…but I’m not looking for anumber…perhaps I’m missunderstandiing what you’re trying to say 
String listResult = list.get(rand.nextInt(list.size()));
So that line works by getting a “random number” and then gets the String that is in that position.
edit: i didnt see your edit on your 2nd post.
So its best how you did it originally, as in the following.
Collection<EntityType> collection
but your line of
List<EntityType> type = Arrays.asList(cet.iterator().next());
that converts your collection into a single (as in gets the first)
you will find that your cet variable is a List not a Collection, from there you can apply your “Random” to it
???ummmmm Ithink i’m missing something there…so could you give an example or explain a bit more? because I know that the
List list = Arrays.asList(“blaze”,“cave_spider”,“creeper”,“enderman”,“endermite”,“giant”,“husk”,“magma_cube”,“silverfish”,“skeleton”,“slime”,“spider”,“vex”,“witch”,“wither_skeleton”,“zombie”,“zombie_pigman”,“NONE”);
Random rand = new Random();
String listResult = list.get(rand.nextInt(list.size())); indent preformatted text by 4 spaces
works because everything is functional…so the issue is what in reguards to the
List type = Arrays.asList(cet.iterator().next());
Sorry. I was really tired yesterday.
So yeah. If you go back to the original code this line was just getting the first in the list and turning that one into a list.
List<EntityType>list = Arrays.asList(cet.iterator().next());
Due to the fact the iterator().next() is mainly used for while statements.
So if you go back to your original code again. The line
Collection<EntityType> cet = Sponge.getRegistry().getAllOf(EntityType.class).stream().filter(x -> classes.stream().anyMatch(y -> y.isAssignableFrom(x.getEntityClass()))).collect(Collectors.toList());
Is actually a List. It seems like your filtering for just monsters too. This is the line that should work in your favour.
List<Monster> cet = new ArrayList(Sponge.getRegistery(Monster.class));
That will filter out every monster (even forge monsters) and store them in a list that has the getter for integers
From there use the random getter code you shared before
2 Likes
The Random class is based upon the system time and some algorithms that make it better than just using system time to generate pseudo-random numbers.
2 Likes
K however Monster.class is not a valid for getAllof…so it woun’t work…
Monster extends entitytype. So it should. If it returns nothing then use filter to check if it is a instance of.
sorry it actually says getRegistry() in the type Sponge is not applicable for the arguments (Class|Monster|)
List<Class<? extends Entity>> classes = ImmutableList.of(Monster.class);
List<Monster> cet = new ArrayList(Sponge.getRegistry(Monster.class));
Random rand = new Random();
EntityType listResult = list.get(rand.nextInt(list.size()));
of course!!! here it is! thank you for your help
One sec. Let me make sure on my pc. (I take it you have imported monster)
Yup! Monster Class is imported
Yep. My mistake. Forgot Entity and EntityType were different (im a idiot sometimes … Most of the time). Try this (wait for edit)
Collection<EntityType> list = Sponge.getRegistry().getAllOf(EntityType.class);
List<EntityType> eList = list.stream().filter(t -> t.getEntityClass().isAssignableFrom(Monster.class)).collect(Collectors.toList());
lol not quite sure I’d say you’re an idiot…cause I’m still learning quite a bit soooo in comparison you seem quite ingenious righ tnow lol
@MoseMister You made a typo here. I believe you meant:
Sponge.getRegistry().getAllOf(Monster.class));
@Cleardragonf This is something that you probably should have caught through your compiler. Based on this thread and previous ones, I think you need to review your core java concepts before working with Sponge. The design of the Sponge API (and back implementation) is significantly more advanced that Bukkit or Spigot. It’s about 100x better when you get used to it, but you need to have a solid foundation with Java before you can really make use of it.
oh but I did…and I see taht…however it is still the same issue with that
except
The method getAllOf(Class) in the type GameRegistry is not applicable for the arguments (Class)