Let's create a custom Dragon Tracker Map item for your Minecraft mod. This will involve creating a new class that extends the MapItem and implements the necessary functionality to track dragons and toggle between showing all dragons or only tamed ones.
Here's a complete implementation:
package net.mattias.bdt.core.item.custom;
import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.MapItem;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.saveddata.maps.MapDecoration;
import net.minecraft.world.level.saveddata.maps.MapItemSavedData;
import net.minecraftforge.registries.ForgeRegistries;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class DragonTrackerMapItem extends MapItem {
private static final Set<EntityType<?>> trackableEntities = new HashSet<>();
private static final String TAG_TAMED_FILTER = "TamedFilter";
public DragonTrackerMapItem() {
super(new Item.Properties().stacksTo(1));
}
// Method to register trackable entity types
public static void registerTrackableEntity(EntityType<?> entityType) {
trackableEntities.add(entityType);
}
// Toggle tamed-only filter when right-clicked
@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
ItemStack stack = player.getItemInHand(hand);
if (!level.isClientSide) {
CompoundTag tag = stack.getOrCreateTag();
boolean currentFilter = tag.getBoolean(TAG_TAMED_FILTER);
tag.putBoolean(TAG_TAMED_FILTER, !currentFilter);
player.displayClientMessage(
Component.literal("Dragon Tracker: Showing " +
(!currentFilter ? "tamed dragons only" : "all dragons")),
true
);
}
return InteractionResultHolder.success(stack);
}
// Update map data to show dragon locations
@Override
public void update(Level level, Entity viewer, MapItemSavedData data) {
if (level.isClientSide || !(level instanceof ServerLevel serverLevel)) {
return;
}
ItemStack stack = viewer instanceof Player player ?
findMapInInventory(player) : ItemStack.EMPTY;
if (stack.isEmpty()) {
return;
}
boolean tamedOnly = stack.getOrCreateTag().getBoolean(TAG_TAMED_FILTER);
// Clear existing decorations
data.decorations.clear();
// Find all trackable entities in the loaded chunks
int mapScale = 1 << data.scale;
BlockPos centerPos = new BlockPos(data.x, 0, data.z);
int searchRadius = 128 * mapScale; // Adjust search radius based on map scale
List<LivingEntity> foundEntities = new ArrayList<>();
// Search for entities in the area
for (Entity entity : serverLevel.getEntities().getAll()) {
if (entity instanceof LivingEntity livingEntity &&
isTrackableEntity(entity) &&
entity.distanceToSqr(data.x, entity.getY(), data.z) <= searchRadius * searchRadius) {
if (!tamedOnly || (tamedOnly && isTamed(livingEntity))) {
foundEntities.add(livingEntity);
}
}
}
// Add map decorations for each found entity
int decorationId = 0;
for (LivingEntity entity : foundEntities) {
// Calculate map coordinates (0-127 range)
float mapX = (float) ((entity.getX() - centerPos.getX()) / mapScale + 64.0);
float mapZ = (float) ((entity.getZ() - centerPos.getZ()) / mapScale + 64.0);
// Clamp coordinates to map bounds
mapX = Math.clamp(mapX, 0, 127);
mapZ = Math.clamp(mapZ, 0, 127);
// Create decoration
MapDecoration.Type decorationType = isTamed(entity) ?
MapDecoration.Type.TARGET_X : MapDecoration.Type.TARGET_POINT;
data.decorations.put(
"dragon_" + decorationId++,
new MapDecoration(
decorationType,
(byte) mapX,
(byte) mapZ,
(byte) 0, // Rotation
Component.literal(getEntityName(entity))
)
);
}
}
// Helper method to find map in player inventory
private ItemStack findMapInInventory(Player player) {
for (ItemStack stack : player.getInventory().items) {
if (stack.getItem() instanceof DragonTrackerMapItem) {
return stack;
}
}
return ItemStack.EMPTY;
}
// Check if entity is trackable
private boolean isTrackableEntity(Entity entity) {
return trackableEntities.contains(entity.getType());
}
// Check if entity is tamed (you might need to adjust this based on your dragon mod)
private boolean isTamed(LivingEntity entity) {
// This is a placeholder - you'll need to implement the actual taming check
// based on your dragon mod's implementation
if (entity instanceof net.minecraft.world.entity.TamableAnimal tamable) {
return tamable.isTame();
}
// Add custom taming check for your dragon mod here
return false;
}
// Get display name for entity
private String getEntityName(Entity entity) {
return ForgeRegistries.ENTITY_TYPES.getKey(entity.getType()).getPath();
}
// Add tooltip information
@Override
public void appendHoverText(ItemStack stack, @Nullable Level level, List<Component> tooltip, TooltipFlag flag) {
super.appendHoverText(stack, level, tooltip, flag);
boolean tamedOnly = stack.getOrCreateTag().getBoolean(TAG_TAMED_FILTER);
tooltip.add(Component.literal("Filter: " + (tamedOnly ? "Tamed Only" : "All Dragons")));
tooltip.add(Component.literal("Right-click to toggle filter"));
}
// Create a new dragon tracker map
public static ItemStack create(Level level, int x, int z, byte scale, boolean trackingPosition, boolean unlimitedTracking) {
ItemStack stack = new ItemStack(ModItems.DRAGON_TRACKER_MAP.get());
MapItemSavedData data = MapItemSavedData.createFresh(x, z, scale, trackingPosition, unlimitedTracking, level.dimension());
int mapId = level.getFreeMapId();
level.setMapData(MapItem.makeKey(mapId), data);
stack.getOrCreateTag().putInt("map", mapId);
return stack;
}
}
To use this item, you'll need to:
- Register the item in your ModItems class:
public class ModItems {
public static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, YourMod.MOD_ID);
public static final RegistryObject<Item> DRAGON_TRACKER_MAP = ITEMS.register("dragon_tracker_map",
() -> new DragonTrackerMapItem());
}
- Register the entities you want to track (do this in your mod's initialization):
public class YourMod {
public static void setup(FMLCommonSetupEvent event) {
// Register dragon entity types to track
DragonTrackerMapItem.registerTrackableEntity(YourDragonEntityTypes.DRAGON_TYPE.get());
// Add more dragon types as needed
}
}
- Add a recipe or other way to obtain the item (example using a crafting recipe):
public class ModRecipes {
public static void registerRecipes(Consumer<FinishedRecipe> consumer) {
ShapedRecipeBuilder.shaped(ModItems.DRAGON_TRACKER_MAP.get())
.pattern("DDD")
.pattern("DMD")
.pattern("DDD")
.define('D', Items.DRAGON_BREATH)
.define('M', Items.MAP)
.unlockedBy("has_dragon_breath", has(Items.DRAGON_BREATH))
.save(consumer);
}
}
Important notes:
-
You'll need to adjust the isTamed() method to work with your specific dragon mod's taming system. The current implementation includes a basic check for vanilla tamable animals, but you'll need to add support for your dragon mod's taming system.
-
The map uses different decoration types for tamed (TARGET_X) and wild (TARGET_POINT) dragons. You can customize these or add custom map decorations if needed.
-
The search radius and map scale can be adjusted by modifying the values in the update() method.
-
You might want to add custom textures for your map item and add localization for the tooltips.
-
The map will automatically update to show dragon positions as the player moves around, similar to how a regular map updates.
To use the item in-game:
- Craft or obtain the dragon tracker map
- Hold it in your hand to see nearby dragons
- Right-click to toggle between showing all dragons and tamed dragons only
- The tooltip will show the current filter mode
Remember to add appropriate imports and handle any mod-specific dependencies for your dragon mod. You might also want to add sound effects or particle effects when toggling the filter for better user feedback.