PresetFlatWorldScreen.java

net.minecraft.client.gui.screens.PresetFlatWorldScreen

信息

  • 全限定名:net.minecraft.client.gui.screens.PresetFlatWorldScreen
  • 类型:public class
  • 包:net.minecraft.client.gui.screens
  • 源码路径:src/main/java/net/minecraft/client/gui/screens/PresetFlatWorldScreen.java
  • 起始行号:L48
  • 继承:Screen
  • 职责:

    TODO

字段/常量

  • SLOT_SPRITE

    • 类型: Identifier
    • 修饰符: private static final
    • 源码定位: L49
    • 说明:

      TODO

  • LOGGER

    • 类型: Logger
    • 修饰符: private static final
    • 源码定位: L50
    • 说明:

      TODO

  • SLOT_BG_SIZE

    • 类型: int
    • 修饰符: private static final
    • 源码定位: L51
    • 说明:

      TODO

  • SLOT_STAT_HEIGHT

    • 类型: int
    • 修饰符: private static final
    • 源码定位: L52
    • 说明:

      TODO

  • SLOT_BG_X

    • 类型: int
    • 修饰符: private static final
    • 源码定位: L53
    • 说明:

      TODO

  • SLOT_BG_Y

    • 类型: int
    • 修饰符: private static final
    • 源码定位: L54
    • 说明:

      TODO

  • SLOT_FG_X

    • 类型: int
    • 修饰符: private static final
    • 源码定位: L55
    • 说明:

      TODO

  • SLOT_FG_Y

    • 类型: int
    • 修饰符: private static final
    • 源码定位: L56
    • 说明:

      TODO

  • DEFAULT_BIOME

    • 类型: ResourceKey<Biome>
    • 修饰符: private static final
    • 源码定位: L57
    • 说明:

      TODO

  • UNKNOWN_PRESET

    • 类型: Component
    • 修饰符: public static final
    • 源码定位: L58
    • 说明:

      TODO

  • parent

    • 类型: CreateFlatWorldScreen
    • 修饰符: private final
    • 源码定位: L59
    • 说明:

      TODO

  • shareText

    • 类型: Component
    • 修饰符: private
    • 源码定位: L60
    • 说明:

      TODO

  • listText

    • 类型: Component
    • 修饰符: private
    • 源码定位: L61
    • 说明:

      TODO

  • list

    • 类型: PresetFlatWorldScreen.PresetsList
    • 修饰符: private
    • 源码定位: L62
    • 说明:

      TODO

  • selectButton

    • 类型: Button
    • 修饰符: private
    • 源码定位: L63
    • 说明:

      TODO

  • export

    • 类型: EditBox
    • 修饰符: private
    • 源码定位: L64
    • 说明:

      TODO

  • settings

    • 类型: FlatLevelGeneratorSettings
    • 修饰符: private
    • 源码定位: L65
    • 说明:

      TODO

内部类/嵌套类型

  • net.minecraft.client.gui.screens.PresetFlatWorldScreen.PresetsList

    • 类型: class
    • 修饰符: private
    • 源码定位: L241
    • 说明:

      TODO

  • net.minecraft.client.gui.screens.PresetFlatWorldScreen.PresetsList.Entry

    • 类型: class
    • 修饰符: public
    • 源码定位: L287
    • 说明:

      TODO

构造器

public PresetFlatWorldScreen(CreateFlatWorldScreen parent) @ L67

  • 构造器名:PresetFlatWorldScreen
  • 源码定位:L67
  • 修饰符:public

参数:

  • parent: CreateFlatWorldScreen

说明:

TODO

方法

下面的方法块按源码顺序生成。

private static FlatLayerInfo getLayerInfoFromString(HolderGetter<Block> blocks, String input, int firstFree) @ L72

  • 方法名:getLayerInfoFromString
  • 源码定位:L72
  • 返回类型:FlatLayerInfo
  • 修饰符:private static

参数:

  • blocks: HolderGetter
  • input: String
  • firstFree: int

说明:

TODO

private static List<FlatLayerInfo> getLayersInfoFromString(HolderGetter<Block> blocks, String input) @ L109

  • 方法名:getLayersInfoFromString
  • 源码定位:L109
  • 返回类型:List
  • 修饰符:private static

参数:

  • blocks: HolderGetter
  • input: String

说明:

TODO

public static FlatLevelGeneratorSettings fromString(HolderGetter<Block> blocks, HolderGetter<Biome> biomes, HolderGetter<StructureSet> structureSets, HolderGetter<PlacedFeature> placedFeatures, String definition, FlatLevelGeneratorSettings settings) @ L130

  • 方法名:fromString
  • 源码定位:L130
  • 返回类型:FlatLevelGeneratorSettings
  • 修饰符:public static

参数:

  • blocks: HolderGetter
  • biomes: HolderGetter
  • structureSets: HolderGetter
  • placedFeatures: HolderGetter
  • definition: String
  • settings: FlatLevelGeneratorSettings

说明:

TODO

private static String save(FlatLevelGeneratorSettings settings) @ L164

  • 方法名:save
  • 源码定位:L164
  • 返回类型:String
  • 修饰符:private static

参数:

  • settings: FlatLevelGeneratorSettings

说明:

TODO

protected void init() @ L180

  • 方法名:init
  • 源码定位:L180
  • 返回类型:void
  • 修饰符:protected

参数:

说明:

TODO

public boolean mouseScrolled(double x, double y, double scrollX, double scrollY) @ L210

  • 方法名:mouseScrolled
  • 源码定位:L210
  • 返回类型:boolean
  • 修饰符:public

参数:

  • x: double
  • y: double
  • scrollX: double
  • scrollY: double

说明:

TODO

public void resize(int width, int height) @ L215

  • 方法名:resize
  • 源码定位:L215
  • 返回类型:void
  • 修饰符:public

参数:

  • width: int
  • height: int

说明:

TODO

public void onClose() @ L222

  • 方法名:onClose
  • 源码定位:L222
  • 返回类型:void
  • 修饰符:public

参数:

说明:

TODO

public void extractRenderState(GuiGraphicsExtractor graphics, int mouseX, int mouseY, float a) @ L227

  • 方法名:extractRenderState
  • 源码定位:L227
  • 返回类型:void
  • 修饰符:public

参数:

  • graphics: GuiGraphicsExtractor
  • mouseX: int
  • mouseY: int
  • a: float

说明:

TODO

public void updateButtonValidity(boolean hasSelected) @ L236

  • 方法名:updateButtonValidity
  • 源码定位:L236
  • 返回类型:void
  • 修饰符:public

参数:

  • hasSelected: boolean

说明:

TODO

代码

@OnlyIn(Dist.CLIENT)
public class PresetFlatWorldScreen extends Screen {
    private static final Identifier SLOT_SPRITE = Identifier.withDefaultNamespace("container/slot");
    private static final Logger LOGGER = LogUtils.getLogger();
    private static final int SLOT_BG_SIZE = 18;
    private static final int SLOT_STAT_HEIGHT = 20;
    private static final int SLOT_BG_X = 1;
    private static final int SLOT_BG_Y = 1;
    private static final int SLOT_FG_X = 2;
    private static final int SLOT_FG_Y = 2;
    private static final ResourceKey<Biome> DEFAULT_BIOME = Biomes.PLAINS;
    public static final Component UNKNOWN_PRESET = Component.translatable("flat_world_preset.unknown");
    private final CreateFlatWorldScreen parent;
    private Component shareText;
    private Component listText;
    private PresetFlatWorldScreen.PresetsList list;
    private Button selectButton;
    private EditBox export;
    private FlatLevelGeneratorSettings settings;
 
    public PresetFlatWorldScreen(CreateFlatWorldScreen parent) {
        super(Component.translatable("createWorld.customize.presets.title"));
        this.parent = parent;
    }
 
    private static @Nullable FlatLayerInfo getLayerInfoFromString(HolderGetter<Block> blocks, String input, int firstFree) {
        List<String> parts = Splitter.on('*').limit(2).splitToList(input);
        int height;
        String blockId;
        if (parts.size() == 2) {
            blockId = parts.get(1);
 
            try {
                height = Math.max(Integer.parseInt(parts.get(0)), 0);
            } catch (NumberFormatException var11) {
                LOGGER.error("Error while parsing flat world string", (Throwable)var11);
                return null;
            }
        } else {
            blockId = parts.get(0);
            height = 1;
        }
 
        int firstAbove = Math.min(firstFree + height, DimensionType.Y_SIZE);
        int actualHeight = firstAbove - firstFree;
 
        Optional<Holder.Reference<Block>> block;
        try {
            block = blocks.get(ResourceKey.create(Registries.BLOCK, Identifier.parse(blockId)));
        } catch (Exception var10) {
            LOGGER.error("Error while parsing flat world string", (Throwable)var10);
            return null;
        }
 
        if (block.isEmpty()) {
            LOGGER.error("Error while parsing flat world string => Unknown block, {}", blockId);
            return null;
        } else {
            return new FlatLayerInfo(actualHeight, block.get().value());
        }
    }
 
    private static List<FlatLayerInfo> getLayersInfoFromString(HolderGetter<Block> blocks, String input) {
        List<FlatLayerInfo> result = Lists.newArrayList();
        String[] depths = input.split(",");
        int firstFree = 0;
 
        for (String depth : depths) {
            FlatLayerInfo layer = getLayerInfoFromString(blocks, depth, firstFree);
            if (layer == null) {
                return Collections.emptyList();
            }
 
            int maxHeight = DimensionType.Y_SIZE - firstFree;
            if (maxHeight > 0) {
                result.add(layer.heightLimited(maxHeight));
                firstFree += layer.getHeight();
            }
        }
 
        return result;
    }
 
    public static FlatLevelGeneratorSettings fromString(
        HolderGetter<Block> blocks,
        HolderGetter<Biome> biomes,
        HolderGetter<StructureSet> structureSets,
        HolderGetter<PlacedFeature> placedFeatures,
        String definition,
        FlatLevelGeneratorSettings settings
    ) {
        Iterator<String> parts = Splitter.on(';').split(definition).iterator();
        if (!parts.hasNext()) {
            return FlatLevelGeneratorSettings.getDefault(biomes, structureSets, placedFeatures);
        } else {
            List<FlatLayerInfo> layers = getLayersInfoFromString(blocks, parts.next());
            if (layers.isEmpty()) {
                return FlatLevelGeneratorSettings.getDefault(biomes, structureSets, placedFeatures);
            } else {
                Holder.Reference<Biome> defaultBiome = biomes.getOrThrow(DEFAULT_BIOME);
                Holder<Biome> biome = defaultBiome;
                if (parts.hasNext()) {
                    String biomeName = parts.next();
                    biome = Optional.ofNullable(Identifier.tryParse(biomeName))
                        .map(id -> ResourceKey.create(Registries.BIOME, id))
                        .flatMap(biomes::get)
                        .orElseGet(() -> {
                            LOGGER.warn("Invalid biome: {}", biomeName);
                            return defaultBiome;
                        });
                }
 
                return settings.withBiomeAndLayers(layers, settings.structureOverrides(), biome);
            }
        }
    }
 
    private static String save(FlatLevelGeneratorSettings settings) {
        StringBuilder builder = new StringBuilder();
 
        for (int i = 0; i < settings.getLayersInfo().size(); i++) {
            if (i > 0) {
                builder.append(",");
            }
 
            builder.append(settings.getLayersInfo().get(i));
        }
 
        builder.append(";");
        builder.append(settings.getBiome().unwrapKey().map(ResourceKey::identifier).orElseThrow(() -> new IllegalStateException("Biome not registered")));
        return builder.toString();
    }
 
    @Override
    protected void init() {
        this.shareText = Component.translatable("createWorld.customize.presets.share");
        this.listText = Component.translatable("createWorld.customize.presets.list");
        this.export = new EditBox(this.font, 50, 40, this.width - 100, 20, this.shareText);
        this.export.setMaxLength(1230);
        WorldCreationContext worldCreatingContext = this.parent.parent.getUiState().getSettings();
        RegistryAccess registryAccess = worldCreatingContext.worldgenLoadContext();
        FeatureFlagSet enabledFeatures = worldCreatingContext.dataConfiguration().enabledFeatures();
        HolderGetter<Biome> biomes = registryAccess.lookupOrThrow(Registries.BIOME);
        HolderGetter<StructureSet> structureSets = registryAccess.lookupOrThrow(Registries.STRUCTURE_SET);
        HolderGetter<PlacedFeature> placedFeatures = registryAccess.lookupOrThrow(Registries.PLACED_FEATURE);
        HolderGetter<Block> blocks = registryAccess.lookupOrThrow(Registries.BLOCK).filterFeatures(enabledFeatures);
        this.export.setValue(save(this.parent.settings()));
        this.settings = this.parent.settings();
        this.addWidget(this.export);
        this.list = this.addRenderableWidget(new PresetFlatWorldScreen.PresetsList(registryAccess, enabledFeatures));
        this.selectButton = this.addRenderableWidget(Button.builder(Component.translatable("createWorld.customize.presets.select"), button -> {
            FlatLevelGeneratorSettings generator = fromString(blocks, biomes, structureSets, placedFeatures, this.export.getValue(), this.settings);
            this.parent.setConfig(generator);
            this.minecraft.setScreen(this.parent);
        }).bounds(this.width / 2 - 155, this.height - 28, 150, 20).build());
        this.addRenderableWidget(
            Button.builder(CommonComponents.GUI_CANCEL, button -> this.minecraft.setScreen(this.parent))
                .bounds(this.width / 2 + 5, this.height - 28, 150, 20)
                .build()
        );
        this.updateButtonValidity(this.list.getSelected() != null);
    }
 
    @Override
    public boolean mouseScrolled(double x, double y, double scrollX, double scrollY) {
        return this.list.mouseScrolled(x, y, scrollX, scrollY);
    }
 
    @Override
    public void resize(int width, int height) {
        String oldEdit = this.export.getValue();
        this.init(width, height);
        this.export.setValue(oldEdit);
    }
 
    @Override
    public void onClose() {
        this.minecraft.setScreen(this.parent);
    }
 
    @Override
    public void extractRenderState(GuiGraphicsExtractor graphics, int mouseX, int mouseY, float a) {
        super.extractRenderState(graphics, mouseX, mouseY, a);
        graphics.centeredText(this.font, this.title, this.width / 2, 8, -1);
        graphics.text(this.font, this.shareText, 51, 30, -6250336);
        graphics.text(this.font, this.listText, 51, 68, -6250336);
        this.export.extractRenderState(graphics, mouseX, mouseY, a);
    }
 
    public void updateButtonValidity(boolean hasSelected) {
        this.selectButton.active = hasSelected || this.export.getValue().length() > 1;
    }
 
    @OnlyIn(Dist.CLIENT)
    private class PresetsList extends ObjectSelectionList<PresetFlatWorldScreen.PresetsList.Entry> {
        public PresetsList(RegistryAccess access, FeatureFlagSet enabledFeatures) {
            Objects.requireNonNull(PresetFlatWorldScreen.this);
            super(PresetFlatWorldScreen.this.minecraft, PresetFlatWorldScreen.this.width, PresetFlatWorldScreen.this.height - 117, 80, 24);
 
            for (Holder<FlatLevelGeneratorPreset> preset : access.lookupOrThrow(Registries.FLAT_LEVEL_GENERATOR_PRESET)
                .getTagOrEmpty(FlatLevelGeneratorPresetTags.VISIBLE)) {
                Set<Block> disabledBlocks = preset.value()
                    .settings()
                    .getLayersInfo()
                    .stream()
                    .map(p -> p.getBlockState().getBlock())
                    .filter(b -> !b.isEnabled(enabledFeatures))
                    .collect(Collectors.toSet());
                if (!disabledBlocks.isEmpty()) {
                    PresetFlatWorldScreen.LOGGER
                        .info(
                            "Discarding flat world preset {} since it contains experimental blocks {}",
                            preset.unwrapKey().map(e -> e.identifier().toString()).orElse("<unknown>"),
                            disabledBlocks
                        );
                } else {
                    this.addEntry(new PresetFlatWorldScreen.PresetsList.Entry(preset));
                }
            }
        }
 
        public void setSelected(PresetFlatWorldScreen.PresetsList.@Nullable Entry selected) {
            super.setSelected(selected);
            PresetFlatWorldScreen.this.updateButtonValidity(selected != null);
        }
 
        @Override
        public boolean keyPressed(KeyEvent event) {
            if (super.keyPressed(event)) {
                return true;
            } else {
                if (event.isSelection() && this.getSelected() != null) {
                    this.getSelected().select();
                }
 
                return false;
            }
        }
 
        @OnlyIn(Dist.CLIENT)
        public class Entry extends ObjectSelectionList.Entry<PresetFlatWorldScreen.PresetsList.Entry> {
            private final FlatLevelGeneratorPreset preset;
            private final Component name;
 
            public Entry(Holder<FlatLevelGeneratorPreset> preset) {
                Objects.requireNonNull(PresetsList.this);
                super();
                this.preset = preset.value();
                this.name = preset.unwrapKey()
                    .map(key -> (Component)Component.translatable(key.identifier().toLanguageKey("flat_world_preset")))
                    .orElse(PresetFlatWorldScreen.UNKNOWN_PRESET);
            }
 
            @Override
            public void extractContent(GuiGraphicsExtractor graphics, int mouseX, int mouseY, boolean hovered, float a) {
                this.blitSlot(graphics, this.getContentX(), this.getContentY(), this.preset.displayItem().value());
                graphics.text(PresetFlatWorldScreen.this.font, this.name, this.getContentX() + 18 + 5, this.getContentY() + 6, -1);
            }
 
            @Override
            public boolean mouseClicked(MouseButtonEvent event, boolean doubleClick) {
                this.select();
                return super.mouseClicked(event, doubleClick);
            }
 
            private void select() {
                PresetsList.this.setSelected(this);
                PresetFlatWorldScreen.this.settings = this.preset.settings();
                PresetFlatWorldScreen.this.export.setValue(PresetFlatWorldScreen.save(PresetFlatWorldScreen.this.settings));
                PresetFlatWorldScreen.this.export.moveCursorToStart(false);
            }
 
            private void blitSlot(GuiGraphicsExtractor graphics, int x, int y, Item item) {
                this.blitSlotBg(graphics, x + 1, y + 1);
                graphics.fakeItem(new ItemStack(item), x + 2, y + 2);
            }
 
            private void blitSlotBg(GuiGraphicsExtractor graphics, int x, int y) {
                graphics.blitSprite(RenderPipelines.GUI_TEXTURED, PresetFlatWorldScreen.SLOT_SPRITE, x, y, 18, 18);
            }
 
            @Override
            public Component getNarration() {
                return Component.translatable("narrator.select", this.name);
            }
        }
    }
}

引用的其他类

  • GuiGraphicsExtractor

    • 引用位置: 参数
  • Button

    • 引用位置: 字段/方法调用
    • 关联成员: Button.builder()
  • EditBox

    • 引用位置: 字段/构造调用
    • 关联成员: EditBox()
  • CreateFlatWorldScreen

    • 引用位置: 参数/字段
  • Screen

    • 引用位置: 继承
  • HolderGetter

    • 引用位置: 参数
  • Component

    • 引用位置: 字段/方法调用
    • 关联成员: Component.translatable()
  • Identifier

    • 引用位置: 字段/方法调用
    • 关联成员: Identifier.parse(), Identifier.tryParse(), Identifier.withDefaultNamespace()
  • ResourceKey

    • 引用位置: 字段/方法调用
    • 关联成员: ResourceKey.create()
  • ItemStack

    • 引用位置: 构造调用
    • 关联成员: ItemStack()
  • Biome

    • 引用位置: 参数/字段
  • Block

    • 引用位置: 参数
  • FlatLayerInfo

    • 引用位置: 构造调用/返回值
    • 关联成员: FlatLayerInfo()
  • FlatLevelGeneratorSettings

    • 引用位置: 参数/字段/方法调用/返回值
    • 关联成员: FlatLevelGeneratorSettings.getDefault()
  • PlacedFeature

    • 引用位置: 参数
  • StructureSet

    • 引用位置: 参数