Search in sources :

Example 1 with Registry

use of net.minecraft.core.Registry in project MinecraftForge by MinecraftForge.

the class ForgeTagHandler method createCustomTagTypeReaders.

/**
 * Creates a map for custom tag type to tag reader
 *
 * @apiNote Internal: For use by TagManager
 */
public static Map<ResourceLocation, TagLoader<?>> createCustomTagTypeReaders() {
    LOGGER.debug("Gathering custom tag collection reader from types.");
    ImmutableMap.Builder<ResourceLocation, TagLoader<?>> builder = ImmutableMap.builder();
    for (ResourceLocation registryName : customTagTypeNames) {
        ForgeRegistry<?> registry = RegistryManager.ACTIVE.getRegistry(registryName);
        if (registry != null && registry.getTagFolder() != null) {
            builder.put(registryName, new TagLoader<>(rl -> Optional.ofNullable(registry.getValue(rl)), "tags/" + registry.getTagFolder()));
        }
    }
    return builder.build();
}
Also used : Named(net.minecraft.tags.Tag.Named) ResourceLocation(net.minecraft.resources.ResourceLocation) IForgeRegistryEntry(net.minecraftforge.registries.IForgeRegistryEntry) ForgeRegistry(net.minecraftforge.registries.ForgeRegistry) Supplier(java.util.function.Supplier) TagLoader(net.minecraft.tags.TagLoader) Registry(net.minecraft.core.Registry) StaticTagHelper(net.minecraft.tags.StaticTagHelper) GameData(net.minecraftforge.registries.GameData) Map(java.util.Map) Nullable(javax.annotation.Nullable) ImmutableSet(com.google.common.collect.ImmutableSet) TagContainer(net.minecraft.tags.TagContainer) ImmutableMap(com.google.common.collect.ImmutableMap) StaticTags(net.minecraft.tags.StaticTags) Set(java.util.Set) Lifecycle(com.mojang.serialization.Lifecycle) ResourceKey(net.minecraft.resources.ResourceKey) IForgeRegistry(net.minecraftforge.registries.IForgeRegistry) Logger(org.apache.logging.log4j.Logger) IOptionalNamedTag(net.minecraftforge.common.Tags.IOptionalNamedTag) Optional(java.util.Optional) Collections(java.util.Collections) RegistryManager(net.minecraftforge.registries.RegistryManager) LogManager(org.apache.logging.log4j.LogManager) ResourceLocation(net.minecraft.resources.ResourceLocation) ImmutableMap(com.google.common.collect.ImmutableMap) TagLoader(net.minecraft.tags.TagLoader)

Example 2 with Registry

use of net.minecraft.core.Registry in project MinecraftForge by MinecraftForge.

the class GameData method injectSnapshot.

@SuppressWarnings({ "unchecked", "rawtypes" })
public static Multimap<ResourceLocation, ResourceLocation> injectSnapshot(Map<ResourceLocation, ForgeRegistry.Snapshot> snapshot, boolean injectFrozenData, boolean isLocalWorld) {
    LOGGER.info(REGISTRIES, "Injecting existing registry data into this {} instance", EffectiveSide.get());
    RegistryManager.ACTIVE.registries.forEach((name, reg) -> reg.validateContent(name));
    RegistryManager.ACTIVE.registries.forEach((name, reg) -> reg.dump(name));
    RegistryManager.ACTIVE.registries.forEach((name, reg) -> reg.resetDelegates());
    // Update legacy names
    snapshot = snapshot.entrySet().stream().sorted(// FIXME Registries need dependency ordering, this makes sure blocks are done before items (for ItemCallbacks) but it's lazy as hell
    Map.Entry.comparingByKey()).collect(Collectors.toMap(e -> RegistryManager.ACTIVE.updateLegacyName(e.getKey()), Map.Entry::getValue, (k1, k2) -> k1, LinkedHashMap::new));
    if (isLocalWorld) {
        List<ResourceLocation> missingRegs = snapshot.keySet().stream().filter(name -> !RegistryManager.ACTIVE.registries.containsKey(name)).collect(Collectors.toList());
        if (missingRegs.size() > 0) {
            String header = "Forge Mod Loader detected missing/unknown registrie(s).\n\n" + "There are " + missingRegs.size() + " missing registries in this save.\n" + "If you continue the missing registries will get removed.\n" + "This may cause issues, it is advised that you create a world backup before continuing.\n\n";
            StringBuilder text = new StringBuilder("Missing Registries:\n");
            for (ResourceLocation s : missingRegs) text.append(s).append("\n");
            LOGGER.warn(REGISTRIES, header);
            LOGGER.warn(REGISTRIES, text.toString());
        }
    }
    RegistryManager STAGING = new RegistryManager("STAGING");
    final Map<ResourceLocation, Map<ResourceLocation, Integer[]>> remaps = Maps.newHashMap();
    final LinkedHashMap<ResourceLocation, Map<ResourceLocation, Integer>> missing = Maps.newLinkedHashMap();
    // Load the snapshot into the "STAGING" registry
    snapshot.forEach((key, value) -> {
        final Class<? extends IForgeRegistryEntry> clazz = RegistryManager.ACTIVE.getSuperType(key);
        remaps.put(key, Maps.newLinkedHashMap());
        missing.put(key, Maps.newLinkedHashMap());
        loadPersistentDataToStagingRegistry(RegistryManager.ACTIVE, STAGING, remaps.get(key), missing.get(key), key, value, clazz);
    });
    snapshot.forEach((key, value) -> {
        value.dummied.forEach(dummy -> {
            Map<ResourceLocation, Integer> m = missing.get(key);
            ForgeRegistry<?> reg = STAGING.getRegistry(key);
            // Currently missing locally, we just inject and carry on
            if (m.containsKey(dummy)) {
                if (reg.markDummy(dummy, m.get(dummy)))
                    m.remove(dummy);
            } else if (isLocalWorld) {
                LOGGER.debug(REGISTRIES, "Registry {}: Resuscitating dummy entry {}", key, dummy);
            } else {
                // The server believes this is a dummy block identity, but we seem to have one locally. This is likely a conflict
                // in mod setup - Mark this entry as a dummy
                int id = reg.getID(dummy);
                LOGGER.warn(REGISTRIES, "Registry {}: The ID {} @ {} is currently locally mapped - it will be replaced with a dummy for this session", dummy, key, id);
                reg.markDummy(dummy, id);
            }
        });
    });
    int count = missing.values().stream().mapToInt(Map::size).sum();
    if (count > 0) {
        LOGGER.debug(REGISTRIES, "There are {} mappings missing - attempting a mod remap", count);
        Multimap<ResourceLocation, ResourceLocation> defaulted = ArrayListMultimap.create();
        Multimap<ResourceLocation, ResourceLocation> failed = ArrayListMultimap.create();
        missing.entrySet().stream().filter(e -> e.getValue().size() > 0).forEach(m -> {
            ResourceLocation name = m.getKey();
            ForgeRegistry<?> reg = STAGING.getRegistry(name);
            RegistryEvent.MissingMappings<?> event = reg.getMissingEvent(name, m.getValue());
            MinecraftForge.EVENT_BUS.post(event);
            List<MissingMappings.Mapping<?>> lst = event.getAllMappings().stream().filter(e -> e.getAction() == MissingMappings.Action.DEFAULT).sorted((a, b) -> a.toString().compareTo(b.toString())).collect(Collectors.toList());
            if (!lst.isEmpty()) {
                LOGGER.error(REGISTRIES, () -> LogMessageAdapter.adapt(sb -> {
                    sb.append("Unidentified mapping from registry ").append(name).append('\n');
                    lst.stream().sorted().forEach(map -> sb.append('\t').append(map.key).append(": ").append(map.id).append('\n'));
                }));
            }
            event.getAllMappings().stream().filter(e -> e.getAction() == MissingMappings.Action.FAIL).forEach(fail -> failed.put(name, fail.key));
            final Class<? extends IForgeRegistryEntry> clazz = RegistryManager.ACTIVE.getSuperType(name);
            processMissing(clazz, name, STAGING, event, m.getValue(), remaps.get(name), defaulted.get(name), failed.get(name), !isLocalWorld);
        });
        if (!defaulted.isEmpty() && !isLocalWorld)
            return defaulted;
        if (!defaulted.isEmpty()) {
            String header = "Forge Mod Loader detected missing registry entries.\n\n" + "There are " + defaulted.size() + " missing entries in this save.\n" + "If you continue the missing entries will get removed.\n" + "A world backup will be automatically created in your saves directory.\n\n";
            StringBuilder buf = new StringBuilder();
            defaulted.asMap().forEach((name, entries) -> {
                buf.append("Missing ").append(name).append(":\n");
                entries.stream().sorted((o1, o2) -> o1.compareNamespaced(o2)).forEach(rl -> buf.append("    ").append(rl).append("\n"));
                buf.append("\n");
            });
            LOGGER.warn(REGISTRIES, header);
            LOGGER.warn(REGISTRIES, buf.toString());
        }
        if (!defaulted.isEmpty()) {
            if (isLocalWorld)
                LOGGER.error(REGISTRIES, "There are unidentified mappings in this world - we are going to attempt to process anyway");
        }
    }
    if (injectFrozenData) {
        // If we're loading from disk, we can actually substitute air in the block map for anything that is otherwise "missing". This keeps the reference in the map, in case
        // the block comes back later
        missing.forEach((name, m) -> {
            if (m.isEmpty())
                return;
            ForgeRegistry<?> reg = STAGING.getRegistry(name);
            m.forEach((rl, id) -> reg.markDummy(rl, id));
        });
        // If we're loading up the world from disk, we want to add in the new data that might have been provisioned by mods
        // So we load it from the frozen persistent registry
        RegistryManager.ACTIVE.registries.forEach((name, reg) -> {
            final Class<? extends IForgeRegistryEntry> clazz = RegistryManager.ACTIVE.getSuperType(name);
            loadFrozenDataToStagingRegistry(STAGING, name, remaps.get(name), clazz);
        });
    }
    // Validate that all the STAGING data is good
    STAGING.registries.forEach((name, reg) -> reg.validateContent(name));
    // Load the STAGING registry into the ACTIVE registry
    // for (Map.Entry<ResourceLocation, IForgeRegistry<? extends IForgeRegistryEntry<?>>> r : RegistryManager.ACTIVE.registries.entrySet())
    RegistryManager.ACTIVE.registries.forEach((key, value) -> {
        final Class<? extends IForgeRegistryEntry> registrySuperType = RegistryManager.ACTIVE.getSuperType(key);
        loadRegistry(key, STAGING, RegistryManager.ACTIVE, registrySuperType, true);
    });
    RegistryManager.ACTIVE.registries.forEach((name, reg) -> {
        reg.bake();
        // Dump the active registry
        reg.dump(name);
    });
    // Tell mods that the ids have changed
    fireRemapEvent(remaps, false);
    // The id map changed, ensure we apply object holders
    ObjectHolderRegistry.applyObjectHolders();
    // Return an empty list, because we're good
    return ArrayListMultimap.create();
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation) SensorType(net.minecraft.world.entity.ai.sensing.SensorType) EffectiveSide(net.minecraftforge.fml.util.thread.EffectiveSide) TreeDecoratorType(net.minecraft.world.level.levelgen.feature.treedecorators.TreeDecoratorType) Item(net.minecraft.world.item.Item) IModStateTransition(net.minecraftforge.fml.IModStateTransition) MenuType(net.minecraft.world.inventory.MenuType) FoliagePlacerType(net.minecraft.world.level.levelgen.feature.foliageplacers.FoliagePlacerType) Activity(net.minecraft.world.entity.schedule.Activity) Registry(net.minecraft.core.Registry) RegistryEvent(net.minecraftforge.event.RegistryEvent) StatType(net.minecraft.stats.StatType) ModLoadingContext(net.minecraftforge.fml.ModLoadingContext) com.google.common.collect(com.google.common.collect) StateDefinition(net.minecraft.world.level.block.state.StateDefinition) DefaultedRegistry(net.minecraft.core.DefaultedRegistry) MemoryModuleType(net.minecraft.world.entity.ai.memory.MemoryModuleType) StaticTags(net.minecraft.tags.StaticTags) PoiType(net.minecraft.world.entity.ai.village.poi.PoiType) DefaultAttributes(net.minecraft.world.entity.ai.attributes.DefaultAttributes) LogMessageAdapter(net.minecraftforge.common.util.LogMessageAdapter) Collectors(java.util.stream.Collectors) Potion(net.minecraft.world.item.alchemy.Potion) Schedule(net.minecraft.world.entity.schedule.Schedule) IdMapper(net.minecraft.core.IdMapper) DebugLevelSource(net.minecraft.world.level.levelgen.DebugLevelSource) Logger(org.apache.logging.log4j.Logger) Stream(java.util.stream.Stream) BlockEntityType(net.minecraft.world.level.block.entity.BlockEntityType) MappedRegistry(net.minecraft.core.MappedRegistry) SoundEvent(net.minecraft.sounds.SoundEvent) AirBlock(net.minecraft.world.level.block.AirBlock) Motive(net.minecraft.world.entity.decoration.Motive) Enchantment(net.minecraft.world.item.enchantment.Enchantment) java.util(java.util) ForgeWorldPreset(net.minecraftforge.common.world.ForgeWorldPreset) EntityType(net.minecraft.world.entity.EntityType) BlockState(net.minecraft.world.level.block.state.BlockState) CompletableFuture(java.util.concurrent.CompletableFuture) Biome(net.minecraft.world.level.biome.Biome) Function(java.util.function.Function) GlobalLootModifierSerializer(net.minecraftforge.common.loot.GlobalLootModifierSerializer) BlockStateProviderType(net.minecraft.world.level.levelgen.feature.stateproviders.BlockStateProviderType) StructureFeature(net.minecraft.world.level.levelgen.feature.StructureFeature) VillagerProfession(net.minecraft.world.entity.npc.VillagerProfession) BiConsumer(java.util.function.BiConsumer) Fluid(net.minecraft.world.level.material.Fluid) EntityDataSerializer(net.minecraft.network.syncher.EntityDataSerializer) EnhancedRuntimeException(net.minecraftforge.fml.util.EnhancedRuntimeException) Nullable(javax.annotation.Nullable) Keys(net.minecraftforge.registries.ForgeRegistries.Keys) BlockItem(net.minecraft.world.item.BlockItem) Executor(java.util.concurrent.Executor) RecipeSerializer(net.minecraft.world.item.crafting.RecipeSerializer) ParticleType(net.minecraft.core.particles.ParticleType) WorldCarver(net.minecraft.world.level.levelgen.carver.WorldCarver) Lifecycle(com.mojang.serialization.Lifecycle) MissingMappings(net.minecraftforge.event.RegistryEvent.MissingMappings) Field(java.lang.reflect.Field) REGISTRIES(net.minecraftforge.registries.ForgeRegistry.REGISTRIES) ResourceKey(net.minecraft.resources.ResourceKey) Material(net.minecraft.world.level.material.Material) Attribute(net.minecraft.world.entity.ai.attributes.Attribute) Feature(net.minecraft.world.level.levelgen.feature.Feature) MinecraftForge(net.minecraftforge.common.MinecraftForge) Validate(org.apache.commons.lang3.Validate) StartupMessageManager(net.minecraftforge.fml.StartupMessageManager) ForgeTagHandler(net.minecraftforge.common.ForgeTagHandler) Block(net.minecraft.world.level.block.Block) ChunkStatus(net.minecraft.world.level.chunk.ChunkStatus) LogManager(org.apache.logging.log4j.LogManager) MobEffect(net.minecraft.world.effect.MobEffect) RegistryEvent(net.minecraftforge.event.RegistryEvent) ResourceLocation(net.minecraft.resources.ResourceLocation)

Example 3 with Registry

use of net.minecraft.core.Registry in project MinecraftForge by MinecraftForge.

the class DimensionsCommand method register.

static ArgumentBuilder<CommandSourceStack, ?> register() {
    return Commands.literal("dimensions").requires(// permission
    cs -> cs.hasPermission(0)).executes(ctx -> {
        ctx.getSource().sendSuccess(new TranslatableComponent("commands.forge.dimensions.list"), true);
        final Registry<DimensionType> reg = ctx.getSource().registryAccess().registryOrThrow(Registry.DIMENSION_TYPE_REGISTRY);
        Map<ResourceLocation, List<ResourceLocation>> types = new HashMap<>();
        for (ServerLevel dim : ctx.getSource().getServer().getAllLevels()) {
            types.computeIfAbsent(reg.getKey(dim.dimensionType()), k -> new ArrayList<>()).add(dim.dimension().location());
        }
        types.keySet().stream().sorted().forEach(key -> {
            ctx.getSource().sendSuccess(new TextComponent(key + ": " + types.get(key).stream().map(ResourceLocation::toString).sorted().collect(Collectors.joining(", "))), false);
        });
        return 0;
    });
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation) CommandSourceStack(net.minecraft.commands.CommandSourceStack) Commands(net.minecraft.commands.Commands) HashMap(java.util.HashMap) DimensionType(net.minecraft.world.level.dimension.DimensionType) ServerLevel(net.minecraft.server.level.ServerLevel) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Registry(net.minecraft.core.Registry) TextComponent(net.minecraft.network.chat.TextComponent) List(java.util.List) Map(java.util.Map) TranslatableComponent(net.minecraft.network.chat.TranslatableComponent) ArgumentBuilder(com.mojang.brigadier.builder.ArgumentBuilder) TextComponent(net.minecraft.network.chat.TextComponent) DimensionType(net.minecraft.world.level.dimension.DimensionType) ServerLevel(net.minecraft.server.level.ServerLevel) TranslatableComponent(net.minecraft.network.chat.TranslatableComponent) HashMap(java.util.HashMap) ResourceLocation(net.minecraft.resources.ResourceLocation) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with Registry

use of net.minecraft.core.Registry in project SpongeCommon by SpongePowered.

the class LevelChunkMixin_API method biomeStream.

@Override
public VolumeStream<WorldChunk, Biome> biomeStream(final Vector3i min, final Vector3i max, final StreamOptions options) {
    VolumeStreamUtils.validateStreamArgs(Objects.requireNonNull(min, "min"), Objects.requireNonNull(max, "max"), Objects.requireNonNull(options, "options"));
    final boolean shouldCarbonCopy = options.carbonCopy();
    final Vector3i size = max.sub(min).add(1, 1, 1);
    @MonotonicNonNull final ObjectArrayMutableBiomeBuffer backingVolume;
    if (shouldCarbonCopy) {
        final Registry<net.minecraft.world.level.biome.Biome> biomeRegistry = this.level.registryAccess().registry(Registry.BIOME_REGISTRY).map(wr -> ((Registry<net.minecraft.world.level.biome.Biome>) wr)).orElse(BuiltinRegistries.BIOME);
        backingVolume = new ObjectArrayMutableBiomeBuffer(min, size, VolumeStreamUtils.nativeToSpongeRegistry(biomeRegistry));
    } else {
        backingVolume = null;
    }
    return VolumeStreamUtils.<WorldChunk, Biome, net.minecraft.world.level.biome.Biome, ChunkAccess, BlockPos>generateStream(options, // Ref
    (WorldChunk) this, (LevelChunk) (Object) this, // Entity Accessor
    VolumeStreamUtils.getBiomesForChunkByPos((LevelReader) (Object) this, min, max), // IdentityFunction
    (pos, biome) -> {
        if (shouldCarbonCopy) {
            backingVolume.setBiome(pos, biome);
        }
    }, // Biome by block position
    (key, biome) -> key, // Filtered Position Entity Accessor
    (blockPos, world) -> {
        final net.minecraft.world.level.biome.Biome biome = shouldCarbonCopy ? backingVolume.getNativeBiome(blockPos.getX(), blockPos.getY(), blockPos.getZ()) : ((LevelReader) world.world()).getBiome(blockPos);
        return new Tuple<>(blockPos, biome);
    });
}
Also used : Arrays(java.util.Arrays) WorldChunk(org.spongepowered.api.world.chunk.WorldChunk) Registry(net.minecraft.core.Registry) MonotonicNonNull(org.checkerframework.checker.nullness.qual.MonotonicNonNull) Biome(org.spongepowered.api.world.biome.Biome) Mixin(org.spongepowered.asm.mixin.Mixin) ChunkBiomeContainerAccessor(org.spongepowered.common.accessor.world.level.chunk.ChunkBiomeContainerAccessor) Map(java.util.Map) DifficultyInstance(net.minecraft.world.DifficultyInstance) BuiltinRegistries(net.minecraft.data.BuiltinRegistries) ObjectArrayMutableBiomeBuffer(org.spongepowered.common.world.volume.buffer.biome.ObjectArrayMutableBiomeBuffer) Predicate(java.util.function.Predicate) Collection(java.util.Collection) StreamOptions(org.spongepowered.api.world.volume.stream.StreamOptions) Remap(org.spongepowered.asm.mixin.Interface.Remap) UUID(java.util.UUID) LevelReader(net.minecraft.world.level.LevelReader) Final(org.spongepowered.asm.mixin.Final) Collectors(java.util.stream.Collectors) ObjectArrayMutableEntityBuffer(org.spongepowered.common.world.volume.buffer.entity.ObjectArrayMutableEntityBuffer) ChunkAccess(net.minecraft.world.level.chunk.ChunkAccess) EntityUtil(org.spongepowered.common.entity.EntityUtil) BlockState(org.spongepowered.api.block.BlockState) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) BlockPos(net.minecraft.core.BlockPos) ChunkBiomeContainer(net.minecraft.world.level.chunk.ChunkBiomeContainer) VolumeStreamUtils(org.spongepowered.common.world.volume.VolumeStreamUtils) HeightTypes(org.spongepowered.api.world.HeightTypes) Shadow(org.spongepowered.asm.mixin.Shadow) Optional(java.util.Optional) Player(org.spongepowered.api.entity.living.player.Player) Level(net.minecraft.world.level.Level) BlockEntity(org.spongepowered.api.block.entity.BlockEntity) LevelChunk(net.minecraft.world.level.chunk.LevelChunk) LevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge) NonNull(org.checkerframework.checker.nullness.qual.NonNull) DataContainer(org.spongepowered.api.data.persistence.DataContainer) ArrayMutableBlockBuffer(org.spongepowered.common.world.volume.buffer.block.ArrayMutableBlockBuffer) AABB(org.spongepowered.api.util.AABB) ClassInstanceMultiMap(net.minecraft.util.ClassInstanceMultiMap) ArrayList(java.util.ArrayList) BlockChangeFlag(org.spongepowered.api.world.BlockChangeFlag) ImmutableList(com.google.common.collect.ImmutableList) ObjectArrayMutableBlockEntityBuffer(org.spongepowered.common.world.volume.buffer.blockentity.ObjectArrayMutableBlockEntityBuffer) Nullable(org.checkerframework.checker.nullness.qual.Nullable) Ticks(org.spongepowered.api.util.Ticks) VolumeStream(org.spongepowered.api.world.volume.stream.VolumeStream) LevelBridge(org.spongepowered.common.bridge.world.level.LevelBridge) Tuple(net.minecraft.util.Tuple) Entity(org.spongepowered.api.entity.Entity) WorldLike(org.spongepowered.api.world.WorldLike) ChunkPos(net.minecraft.world.level.ChunkPos) Implements(org.spongepowered.asm.mixin.Implements) SpongeChunkLayout(org.spongepowered.common.world.storage.SpongeChunkLayout) Vector3d(org.spongepowered.math.vector.Vector3d) Heightmap(net.minecraft.world.level.levelgen.Heightmap) VecHelper(org.spongepowered.common.util.VecHelper) EntityType(org.spongepowered.api.entity.EntityType) SpongeTicks(org.spongepowered.common.util.SpongeTicks) Collections(java.util.Collections) Interface(org.spongepowered.asm.mixin.Interface) Intrinsic(org.spongepowered.asm.mixin.Intrinsic) Vector3i(org.spongepowered.math.vector.Vector3i) ObjectArrayMutableBiomeBuffer(org.spongepowered.common.world.volume.buffer.biome.ObjectArrayMutableBiomeBuffer) LevelReader(net.minecraft.world.level.LevelReader) ChunkAccess(net.minecraft.world.level.chunk.ChunkAccess) Biome(org.spongepowered.api.world.biome.Biome) MonotonicNonNull(org.checkerframework.checker.nullness.qual.MonotonicNonNull) WorldChunk(org.spongepowered.api.world.chunk.WorldChunk) Vector3i(org.spongepowered.math.vector.Vector3i) BlockPos(net.minecraft.core.BlockPos) Tuple(net.minecraft.util.Tuple)

Example 5 with Registry

use of net.minecraft.core.Registry in project SpongeCommon by SpongePowered.

the class TimingsExport method reportTimings.

/**
 * Builds an XML report of the timings to be uploaded for parsing.
 */
static void reportTimings() {
    if (TimingsExport.requestingReport.isEmpty()) {
        return;
    }
    TimingsReportListener listeners = new TimingsReportListener(TimingsExport.requestingReport);
    TimingsExport.requestingReport.clear();
    long now = System.currentTimeMillis();
    final long lastReportDiff = now - TimingsExport.lastReport;
    if (lastReportDiff < 60000) {
        listeners.send(Component.text("Please wait at least 1 minute in between Timings reports. (" + (int) ((60000 - lastReportDiff) / 1000) + " seconds)", NamedTextColor.RED));
        listeners.done();
        return;
    }
    final long lastStartDiff = now - TimingsManager.timingStart;
    if (lastStartDiff < 180000) {
        listeners.send(Component.text("Please wait at least 3 minutes before generating a Timings report. Unlike Timings v1, v2 benefits from longer timings and is not as useful with short timings. (" + (int) ((180000 - lastStartDiff) / 1000) + " seconds)", NamedTextColor.RED));
        listeners.done();
        return;
    }
    listeners.send(Component.text("Preparing Timings Report...", NamedTextColor.GREEN));
    TimingsExport.lastReport = now;
    Platform platform = SpongeCommon.game().platform();
    JsonObjectBuilder builder = JSONUtil.objectBuilder().add("version", platform.container(IMPLEMENTATION).metadata().version()).add("maxplayers", SpongeCommon.game().server().maxPlayers()).add("start", TimingsManager.timingStart / 1000).add("end", System.currentTimeMillis() / 1000).add("sampletime", (System.currentTimeMillis() - TimingsManager.timingStart) / 1000);
    if (!TimingsManager.privacy) {
        builder.add("server", TimingsExport.getServerName()).add("motd", PlainTextComponentSerializer.plainText().serialize(Sponge.server().motd())).add("online-mode", Sponge.server().isOnlineModeEnabled()).add("icon", SpongeCommon.server().getStatus().getFavicon());
    }
    final Runtime runtime = Runtime.getRuntime();
    RuntimeMXBean runtimeBean = ManagementFactory.getRuntimeMXBean();
    builder.add("system", JSONUtil.objectBuilder().add("timingcost", TimingsExport.getCost()).add("name", System.getProperty("os.name")).add("version", System.getProperty("os.version")).add("jvmversion", System.getProperty("java.version")).add("arch", System.getProperty("os.arch")).add("maxmem", runtime.maxMemory()).add("cpu", runtime.availableProcessors()).add("runtime", ManagementFactory.getRuntimeMXBean().getUptime()).add("flags", TimingsExport.RUNTIME_FLAG_JOINER.join(runtimeBean.getInputArguments())).add("gc", JSONUtil.mapArrayToObject(ManagementFactory.getGarbageCollectorMXBeans(), (input) -> {
        return JSONUtil.singleObjectPair(input.getName(), JSONUtil.arrayOf(input.getCollectionCount(), input.getCollectionTime()));
    })));
    Set<BlockEntityType> blockEntityTypeSet = Sets.newHashSet();
    Set<EntityType<?>> entityTypeSet = Sets.newHashSet();
    int size = TimingsManager.HISTORY.size();
    TimingHistory[] history = new TimingHistory[size + 1];
    int i = 0;
    for (TimingHistory timingHistory : TimingsManager.HISTORY) {
        blockEntityTypeSet.addAll(timingHistory.tileEntityTypeSet);
        entityTypeSet.addAll(timingHistory.entityTypeSet);
        history[i++] = timingHistory;
    }
    // Current snapshot
    history[i] = new TimingHistory();
    blockEntityTypeSet.addAll(history[i].tileEntityTypeSet);
    entityTypeSet.addAll(history[i].entityTypeSet);
    JsonObjectBuilder handlersBuilder = JSONUtil.objectBuilder();
    for (TimingIdentifier.TimingGroup group : TimingIdentifier.GROUP_MAP.values()) {
        for (TimingHandler id : group.handlers) {
            if (!id.timed && !id.isSpecial()) {
                continue;
            }
            handlersBuilder.add(id.id, JSONUtil.arrayOf(group.id, id.name));
        }
    }
    builder.add("idmap", JSONUtil.objectBuilder().add("groups", JSONUtil.mapArrayToObject(TimingIdentifier.GROUP_MAP.values(), (group) -> JSONUtil.singleObjectPair(group.id, group.name))).add("handlers", handlersBuilder).add("worlds", JSONUtil.mapArrayToObject(TimingHistory.worldMap.entrySet(), (entry) -> JSONUtil.singleObjectPair(entry.getValue(), entry.getKey()))).add("blockentity", JSONUtil.mapArrayToObject(blockEntityTypeSet, (blockEntityType) -> {
        final ResourceKey resourceKey = Sponge.game().registry(RegistryTypes.BLOCK_ENTITY_TYPE).valueKey(blockEntityType);
        return JSONUtil.singleObjectPair(TimingsPls.getBlockEntityId(blockEntityType), resourceKey);
    })).add("entity", JSONUtil.mapArrayToObject(entityTypeSet, (entityType) -> {
        final ResourceKey resourceKey = (ResourceKey) (Object) Registry.ENTITY_TYPE.getKey((net.minecraft.world.entity.EntityType<?>) entityType);
        return JSONUtil.singleObjectPair(TimingsPls.getEntityId(entityType), resourceKey);
    })));
    // Information about loaded plugins
    builder.add("plugins", JSONUtil.mapArrayToObject(SpongeCommon.game().pluginManager().plugins(), (plugin) -> {
        @Nullable final URL homepageUrl = plugin.metadata().links().homepage().orElse(null);
        final String homepage = homepageUrl != null ? homepageUrl.toString() : "<not specified>";
        return JSONUtil.objectBuilder().add(plugin.metadata().id(), JSONUtil.objectBuilder().add("version", plugin.metadata().version()).add("description", plugin.metadata().description().orElse("")).add("website", homepage).add("authors", TimingsExport.AUTHOR_LIST_JOINER.join(plugin.metadata().contributors()))).build();
    }));
    // Information on the users Config
    builder.add("config", JSONUtil.objectBuilder().add("sponge", TimingsExport.serializeConfigNode(SpongeConfigs.getCommon().getNode())));
    new TimingsExport(listeners, builder.build(), history).start();
}
Also used : HttpURLConnection(java.net.HttpURLConnection) JsonObject(com.google.gson.JsonObject) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URL(java.net.URL) ConfigurationNode(org.spongepowered.configurate.ConfigurationNode) ClickEvent(net.kyori.adventure.text.event.ClickEvent) BlockEntityType(org.spongepowered.api.block.entity.BlockEntityType) RconConnection(org.spongepowered.api.network.RconConnection) IMPLEMENTATION(org.spongepowered.api.Platform.Component.IMPLEMENTATION) Platform(org.spongepowered.api.Platform) JsonElement(com.google.gson.JsonElement) Registry(net.minecraft.core.Registry) InetAddress(java.net.InetAddress) Lists(com.google.common.collect.Lists) Component(net.kyori.adventure.text.Component) ResourceKey(org.spongepowered.api.ResourceKey) ManagementFactory(java.lang.management.ManagementFactory) Nullable(org.checkerframework.checker.nullness.qual.Nullable) JsonObjectBuilder(co.aikar.timings.util.JSONUtil.JsonObjectBuilder) OutputStream(java.io.OutputStream) RuntimeMXBean(java.lang.management.RuntimeMXBean) JSONUtil(co.aikar.timings.util.JSONUtil) Launch(org.spongepowered.common.launch.Launch) Sponge(org.spongepowered.api.Sponge) Set(java.util.Set) IOException(java.io.IOException) SpongeCommon(org.spongepowered.common.SpongeCommon) RegistryTypes(org.spongepowered.api.registry.RegistryTypes) Sets(com.google.common.collect.Sets) NamedTextColor(net.kyori.adventure.text.format.NamedTextColor) JsonArray(com.google.gson.JsonArray) List(java.util.List) Audience(net.kyori.adventure.audience.Audience) PlainTextComponentSerializer(net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer) SpongeConfigs(org.spongepowered.common.applaunch.config.core.SpongeConfigs) Entry(java.util.Map.Entry) EntityType(org.spongepowered.api.entity.EntityType) GZIPOutputStream(java.util.zip.GZIPOutputStream) Joiner(com.google.common.base.Joiner) InputStream(java.io.InputStream) BlockEntityType(org.spongepowered.api.block.entity.BlockEntityType) Platform(org.spongepowered.api.Platform) RuntimeMXBean(java.lang.management.RuntimeMXBean) URL(java.net.URL) ResourceKey(org.spongepowered.api.ResourceKey) BlockEntityType(org.spongepowered.api.block.entity.BlockEntityType) EntityType(org.spongepowered.api.entity.EntityType) JsonObjectBuilder(co.aikar.timings.util.JSONUtil.JsonObjectBuilder)

Aggregations

Registry (net.minecraft.core.Registry)6 List (java.util.List)3 Map (java.util.Map)3 Collectors (java.util.stream.Collectors)3 ResourceLocation (net.minecraft.resources.ResourceLocation)3 Lifecycle (com.mojang.serialization.Lifecycle)2 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 Optional (java.util.Optional)2 Set (java.util.Set)2 Stream (java.util.stream.Stream)2 Nullable (javax.annotation.Nullable)2 MappedRegistry (net.minecraft.core.MappedRegistry)2 ResourceKey (net.minecraft.resources.ResourceKey)2 StaticTags (net.minecraft.tags.StaticTags)2 LogManager (org.apache.logging.log4j.LogManager)2 Logger (org.apache.logging.log4j.Logger)2 JSONUtil (co.aikar.timings.util.JSONUtil)1 JsonObjectBuilder (co.aikar.timings.util.JSONUtil.JsonObjectBuilder)1 Joiner (com.google.common.base.Joiner)1