Commit 55765fdb by Jonathan Thomas

Initial rarity item & entity generator class, to help classify and categorize…

Initial rarity item & entity generator class, to help classify and categorize items and entities for our ChatGPT prompts. It turns out that ChatGPT is not good at randomizing or using valid entity types or item types, so we need to inject valid choices into the prompts.
parent 718a0d32
Pipeline #11660 passed with stage
in 26 seconds
......@@ -5,17 +5,22 @@ import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerWorldEvents;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.item.Item;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.Identifier;
import net.minecraft.util.Rarity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
public class ModInit implements ModInitializer {
public static final Logger LOGGER = LoggerFactory.getLogger("mobgpt");
......@@ -116,6 +121,21 @@ public class ModInit implements ModInitializer {
// Load chat data...
LOGGER.info("LOAD chat data from NBT: " + world.getRegistryKey().getValue());
serverInstance = server;
// Example usage: Get 5 random items of RARE rarity
List<Item> rareItems = RarityItemCollector.getItemsByRarity(Rarity.RARE, 5);
System.out.println("Random Rare Items:");
for (Item item : rareItems) {
System.out.println(" - " + item);
}
// Example usage: Get 5 random entities of RARE rarity
List<EntityType> rareEntities = RarityItemCollector.getEntitiesByRarity(Rarity.RARE, 5);
System.out.println("Random Rare Entities:");
for (EntityType item : rareEntities) {
System.out.println(" - " + item);
}
});
ServerWorldEvents.UNLOAD.register((server, world) -> {
// Save chat data...
......
package com.owlmaddie;
import net.minecraft.entity.EntityType;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.Registries;
import net.minecraft.util.Rarity;
import java.util.*;
public class RarityItemCollector {
public static List<Item> getItemsByRarity(Rarity rarity, int quantity) {
List<Item> itemsOfSpecificRarity = new ArrayList<>();
for (Item item : Registries.ITEM) {
ItemStack stack = new ItemStack(item);
if (stack.getRarity().equals(rarity) &&
!item.getName().toString().contains("spawn_egg") &&
!item.getName().toString().contains("jukebox") &&
!item.getName().toString().contains("slab")) {
itemsOfSpecificRarity.add(item);
}
}
// Shuffle the list to randomize it
Collections.shuffle(itemsOfSpecificRarity);
// If the quantity requested is more than the number of available items, return them all
if (quantity >= itemsOfSpecificRarity.size()) {
return itemsOfSpecificRarity;
}
// Otherwise, return a sublist containing only the number of items requested
return itemsOfSpecificRarity.subList(0, quantity);
}
/*
Categorize all entities and return a random list filtered by rarity. Rarity is calculated mostly with
Spawn Group, with a few manual exclusions.
*/
public static List<EntityType> getEntitiesByRarity(Rarity rarity, int quantity) {
List<EntityType> categoryCommonEntities = new ArrayList<>();
List<EntityType> categoryUncommonEntities = new ArrayList<>();
List<EntityType> categoryRareEntities = new ArrayList<>();
List<EntityType> entitiesOfSpecificRarity = new ArrayList<>();
// Categorize spawn groups & entity types into rarity
Set<String> commonEntities = new HashSet<>(Arrays.asList("creature"));
Set<String> unCommonEntities = new HashSet<>(Arrays.asList("ambient", "water_ambient", "water_creature",
"axolotl", "underground_water_creature", "zombie", "spider", "skeleton", "enderman", "drowned",
"creeper", "slime", "silverfish", "cave_spider", "piglin", "witch", "zombie_villager"));
Set<String> rareEntities = new HashSet<>(Arrays.asList("monster"));
// Always exclude these
Set<String> excludedMonsters = new HashSet<>(Arrays.asList("ender_dragon", "phantom", "bat"));
// Iterate through entities
for (EntityType entityType : Registries.ENTITY_TYPE) {
String entityName = entityType.getUntranslatedName();
String spawnGroup = entityType.getSpawnGroup().asString();
if (!excludedMonsters.contains(entityName)) {
if (commonEntities.contains(spawnGroup) || commonEntities.contains(entityName)) {
categoryCommonEntities.add(entityType);
} else if (unCommonEntities.contains(spawnGroup) || unCommonEntities.contains(entityName)) {
categoryUncommonEntities.add(entityType);
} else if (rareEntities.contains(spawnGroup) || rareEntities.contains(entityName)) {
categoryRareEntities.add(entityType);
}
}
}
// Determine which list to use
if (rarity == Rarity.COMMON) {
entitiesOfSpecificRarity = categoryCommonEntities;
} else if (rarity == Rarity.UNCOMMON) {
entitiesOfSpecificRarity = categoryUncommonEntities;
} else if (rarity == Rarity.RARE) {
entitiesOfSpecificRarity = categoryRareEntities;
}
// Shuffle the list to randomize it
Collections.shuffle(entitiesOfSpecificRarity);
// If the quantity requested is more than the number of available items, return them all
if (quantity >= entitiesOfSpecificRarity.size()) {
return entitiesOfSpecificRarity;
}
// Otherwise, return a sublist containing only the number of items requested
return entitiesOfSpecificRarity.subList(0, quantity);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment