Skip to content

Commit

Permalink
Merge branch 'api-11' into update/1.20.6
Browse files Browse the repository at this point in the history
  • Loading branch information
ImMorpheus committed May 9, 2024
2 parents 664e736 + f49679a commit 2ded02a
Show file tree
Hide file tree
Showing 21 changed files with 508 additions and 395 deletions.
@@ -0,0 +1,41 @@
/*
* This file is part of Sponge, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.common.accessor.core;

import net.minecraft.core.MappedRegistry;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(MappedRegistry.class)
public interface MappedRegistryAccessor<T> {

@Accessor("frozen")
boolean accessor$frozen();

@Accessor("frozen")
@Mutable
void accessor$frozen(boolean frozen);
}
Expand Up @@ -24,13 +24,21 @@
*/
package org.spongepowered.common.accessor.world.level;

import net.minecraft.world.Difficulty;
import net.minecraft.world.level.LevelSettings;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(LevelSettings.class)
public interface LevelSettingsAccessor {

@Accessor("difficulty")
@Mutable void accessor$difficulty(final Difficulty difficulty);

@Accessor("hardcore")
@Mutable void accessor$harcore(final boolean hardcore);


@Accessor("allowCommands")
@Mutable void accessor$allowCommands(final boolean allowCommands);
}
409 changes: 205 additions & 204 deletions src/accessors/resources/mixins.sponge.accessors.json

Large diffs are not rendered by default.

@@ -0,0 +1,33 @@
/*
* This file is part of Sponge, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.common.bridge.core;

import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;

public interface MappedRegistryBridge<T> {

void bridge$forceRemoveValue(ResourceKey<Registry<T>> key);
}
Expand Up @@ -54,7 +54,7 @@ public interface LevelStemBridge {

@Nullable Boolean bridge$hardcore();

@Nullable Boolean bridge$commands();
@Nullable Boolean bridge$allowCommands();

@Nullable Boolean bridge$pvp();

Expand Down
Expand Up @@ -57,8 +57,6 @@ public interface PrimaryLevelDataBridge {

UUID bridge$uniqueId();

void bridge$setUniqueId(UUID uniqueId);

/**
* Gets whether the world data supports custom difficulties,
* @return
Expand Down Expand Up @@ -105,19 +103,17 @@ public interface PrimaryLevelDataBridge {

void bridge$populateFromLevelStem(LevelStem dimension);

void bridge$setMapUUIDIndex(BiMap<Integer, UUID> index);

BiMap<Integer, UUID> bridge$getMapUUIDIndex();

int bridge$getIndexForUniqueId(UUID uuid);

Optional<UUID> bridge$getUniqueIdForIndex(int ownerIndex);

void bridge$readSpongeLevelData(Dynamic<Tag> impl$spongeLevelData);

CompoundTag bridge$writeSpongeLevelData();

void bridge$hardcore(boolean hardcore);

void bridge$allowCommands(boolean commands);

void bridge$readSpongeLevelData(Dynamic<Tag> impl$spongeLevelData);

CompoundTag bridge$writeSpongeLevelData();
}
Expand Up @@ -63,7 +63,7 @@ public static void register(final DataProviderRegistrator registrator) {
.create(Keys.HARDCORE)
.get(LevelStemBridge::bridge$hardcore)
.create(Keys.COMMANDS)
.get(LevelStemBridge::bridge$commands)
.get(LevelStemBridge::bridge$allowCommands)
.create(Keys.PVP)
.get(LevelStemBridge::bridge$pvp)
.create(Keys.VIEW_DISTANCE)
Expand Down
Expand Up @@ -107,7 +107,7 @@ public static final class FactoryImpl implements DataPackType.Factory {
false);

private final SpongeDataPackType<JsonElement, @NonNull WorldTemplate> world = SpongeDataPackType.basic(WorldTemplate.class,
"dimension", SpongeWorldTemplate::serialize, SpongeWorldTemplate::decode,
"dimension", SpongeWorldTemplate::encode, SpongeWorldTemplate::decode,
false);

private final SpongeDataPackType<JsonElement, @NonNull BiomeTemplate> biome = SpongeDataPackType.basic(BiomeTemplate.class,
Expand Down
Expand Up @@ -72,7 +72,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Predicate;

/**
Expand Down Expand Up @@ -246,7 +245,7 @@ private static Lens lensPlayerContainer(Object inventory, int size, SlotLensProv

public static class BasicSlotLensProvider implements SlotLensProvider {

private static Map<Integer, BasicSlotLens> basicSlotLenses = new ConcurrentHashMap<>();
private Map<Integer, BasicSlotLens> basicSlotLenses = new HashMap<>();

public final int base;
public final int size;
Expand All @@ -262,7 +261,7 @@ public BasicSlotLensProvider(int base, int size) {

@Override
public SlotLens getSlotLens(int index) {
return BasicSlotLensProvider.basicSlotLenses.computeIfAbsent(index, BasicSlotLens::new);
return this.basicSlotLenses.computeIfAbsent(index, BasicSlotLens::new);
}

@Override
Expand Down
Expand Up @@ -40,7 +40,9 @@
import org.spongepowered.api.registry.RegistryRoots;
import org.spongepowered.api.registry.RegistryType;
import org.spongepowered.api.registry.ValueNotFoundException;
import org.spongepowered.common.accessor.core.MappedRegistryAccessor;
import org.spongepowered.common.accessor.resources.ResourceKeyAccessor;
import org.spongepowered.common.bridge.core.MappedRegistryBridge;
import org.spongepowered.common.bridge.core.WritableRegistryBridge;

import java.util.Map;
Expand Down Expand Up @@ -82,9 +84,7 @@ public RegistryHolderLogic(final RegistryAccess dynamicAccess) {
final WritableRegistry root = (WritableRegistry) this.roots.get(new ResourceLocation("minecraft", "root"));
// Add the dynamic registries. These are server-scoped in Vanilla

dynamicAccess.registries().forEach(entry -> {
root.register(entry.key(), entry.value(), RegistrationInfo.BUILT_IN);
});
dynamicAccess.registries().forEach(entry -> root.register(entry.key(), entry.value(), RegistrationInfo.BUILT_IN));
root.freeze();
}

Expand Down Expand Up @@ -133,15 +133,7 @@ public <T> Registry<T> createRegistry(final RegistryType<T> type, final @Nullabl

public <T> Registry<T> createRegistry(final RegistryType<T> type, final @Nullable Supplier<Map<ResourceKey, T>> defaultValues,
final boolean isDynamic) {
return this.createRegistry(type, InitialRegistryData.noIds(defaultValues), this.registrySupplier(isDynamic, null));
}

public <T> Registry<T> createRegistry(final RegistryType<T> type, final RegistryLoader<T> loader) {
return this.createRegistry(type, loader, false);
}

public <T> Registry<T> createRegistry(final RegistryType<T> type, final RegistryLoader<T> loader, final boolean isDynamic) {
return this.createRegistry(type, loader, this.registrySupplier(isDynamic, null));
return this.createRegistry(type, InitialRegistryData.noIds(defaultValues), this.registrySupplier(isDynamic, null), false);
}

@SuppressWarnings("unchecked")
Expand All @@ -164,13 +156,14 @@ public <T> Function<net.minecraft.resources.ResourceKey<net.minecraft.core.Regis
}

public <T> Registry<T> createRegistry(final RegistryType<T> type, final @Nullable InitialRegistryData<T> defaultValues,
final Function<net.minecraft.resources.ResourceKey<net.minecraft.core.Registry<T>>, net.minecraft.core.Registry<T>> registrySupplier) {
final Function<net.minecraft.resources.ResourceKey<net.minecraft.core.Registry<T>>, net.minecraft.core.Registry<T>> registrySupplier, final boolean replace) {
final net.minecraft.core.Registry<net.minecraft.core.Registry<?>> root = this.roots.get(Objects.requireNonNull(type, "type").root());
if (root == null) {
throw new ValueNotFoundException(String.format("No '%s' root registry has been defined", type.root()));
}
net.minecraft.core.Registry<?> registry = root.get((ResourceLocation) (Object) type.location());
if (registry != null) {
final boolean exists = registry != null;
if (!replace && exists) {
throw new DuplicateRegistrationException(String.format("Registry '%s' in root '%s' has already been defined", type.location(), type.root()));
}
final net.minecraft.resources.ResourceKey<net.minecraft.core.Registry<T>> key;
Expand Down Expand Up @@ -202,10 +195,21 @@ public <T> Registry<T> createRegistry(final RegistryType<T> type, final @Nullabl
}
});
}

// This is so wrong and dirty and only because we don't have layered registries...
final boolean frozen = ((MappedRegistryAccessor<T>) root).accessor$frozen();

if (replace && exists) {
((MappedRegistryAccessor<T>) root).accessor$frozen(false);
((MappedRegistryBridge<T>) root).bridge$forceRemoveValue(key);
}

((WritableRegistry) root).register(key, registry, RegistrationInfo.BUILT_IN);
if (registry instanceof CallbackRegistry) {
((CallbackRegistry<?>) registry).setCallbackEnabled(true);
}
((MappedRegistryAccessor<T>) root).accessor$frozen(frozen);

return (Registry<T>) registry;
}

Expand Down
Expand Up @@ -104,10 +104,10 @@ public static void registerGlobalRegistriesDimensionLayer(final SpongeRegistryHo
}
final RegistryAccess.ImmutableRegistryAccess builtInRegistryAccess = new RegistryAccess.ImmutableRegistryAccess(BuiltInRegistries.REGISTRY.stream().toList());
final CommandBuildContext cbCtx = CommandBuildContext.simple(builtInRegistryAccess, featureFlags);
holder.createFrozenRegistry(RegistryTypes.COMMAND_TREE_NODE_TYPE, CommandRegistryLoader.clientCompletionKey(cbCtx));
holder.createFrozenRegistry(RegistryTypes.REGISTRY_KEYED_VALUE_PARAMETER, CommandRegistryLoader.valueParameter(cbCtx));
holder.createOrReplaceFrozenRegistry(RegistryTypes.COMMAND_TREE_NODE_TYPE, CommandRegistryLoader.clientCompletionKey(cbCtx));
holder.createOrReplaceFrozenRegistry(RegistryTypes.REGISTRY_KEYED_VALUE_PARAMETER, CommandRegistryLoader.valueParameter(cbCtx));

holder.createFrozenRegistry(RegistryTypes.FLAT_GENERATOR_CONFIG, SpongeRegistryLoader.flatGeneratorConfig(registryAccess));
holder.createOrReplaceFrozenRegistry(RegistryTypes.FLAT_GENERATOR_CONFIG, SpongeRegistryLoader.flatGeneratorConfig(registryAccess));
}

public static void registerServerRegistries(final RegistryHolder holder) {
Expand Down
Expand Up @@ -47,21 +47,13 @@ default void setRootMinecraftRegistry(Registry<Registry<?>> registry) {
}

default <T> org.spongepowered.api.registry.Registry<T> createRegistry(final RegistryType<T> type, final @Nullable InitialRegistryData<T> defaultValues,
final boolean isDynamic, final @Nullable BiConsumer<net.minecraft.resources.ResourceKey<T>, T> callback) {
return this.registryHolder().createRegistry(type, defaultValues, this.registryHolder().registrySupplier(isDynamic, callback));
}

default <T> org.spongepowered.api.registry.Registry<T> createRegistry(final RegistryType<T> type, final @Nullable Map<ResourceKey, T> defaultValues) {
return this.createRegistry(type, defaultValues != null ? () -> defaultValues : null, false);
}

default <T> org.spongepowered.api.registry.Registry<T> createRegistry(final RegistryType<T> type, final @Nullable Supplier<Map<ResourceKey, T>> defaultValues) {
return this.createRegistry(type, defaultValues, false);
final boolean isDynamic, final @Nullable BiConsumer<net.minecraft.resources.ResourceKey<T>, T> callback, final boolean replace) {
return this.registryHolder().createRegistry(type, defaultValues, this.registryHolder().registrySupplier(isDynamic, callback), replace);
}

default <T> org.spongepowered.api.registry.Registry<T> createRegistry(final RegistryType<T> type, final @Nullable Supplier<Map<ResourceKey, T>> defaultValues,
final boolean isDynamic) {
return this.createRegistry(type, InitialRegistryData.noIds(defaultValues), isDynamic, null);
return this.createRegistry(type, InitialRegistryData.noIds(defaultValues), isDynamic, null, false);
}

default <T> org.spongepowered.api.registry.Registry<T> createRegistry(final RegistryType<T> type, final RegistryLoader<T> loader) {
Expand All @@ -76,9 +68,17 @@ default <T> org.spongepowered.api.registry.Registry<T> createFrozenRegistry(fina
return registry;
}

default <T> org.spongepowered.api.registry.Registry<T> createOrReplaceFrozenRegistry(final RegistryType<T> type, final RegistryLoader<T> loader) {
final org.spongepowered.api.registry.Registry<T> registry = this.createRegistry(type, loader, false, null, true);
if (registry instanceof MappedRegistry<?> toFreeze) {
toFreeze.freeze();
}
return registry;
}

default <T> org.spongepowered.api.registry.Registry<T> createRegistry(final RegistryType<T> type, final RegistryLoader<T> loader,
final boolean isDynamic) {
return this.createRegistry(type, loader, isDynamic, null);
return this.createRegistry(type, loader, isDynamic, null, false);
}

@Override
Expand Down
Expand Up @@ -96,7 +96,7 @@ public BuilderImpl() {

@Override
public Function<ProcessorListTemplate, ProcessorList> valueExtractor() {
return ProcessorListTemplate::processorList;
return value -> (ProcessorList) ((SpongeProcessorListTemplate) value).representedProcessors;
}

@Override
Expand All @@ -105,12 +105,6 @@ public Builder fromValues(final List<Processor> processorList) {
return this;
}

@Override
public Builder from(final ProcessorListTemplate value) {
this.processorList = ((SpongeProcessorListTemplate) value).representedProcessors;
return this;
}

@Override
public Builder fromValue(final ProcessorList processorList) {
this.processorList = (StructureProcessorList) processorList;
Expand Down
Expand Up @@ -304,7 +304,7 @@ private CompletableFuture<ServerWorld> loadWorld0(final net.minecraft.resources.
final ChunkProgressListener chunkStatusListener = ((MinecraftServerAccessor) this.server).accessor$progressListenerFactory().create(11);
final ServerLevel world;
try {
world = this.createLevel(registryKey, levelStem, worldKey, worldTypeKey.orElse(null), chunkStatusListener);
world = this.createNonDefaultLevel(registryKey, levelStem, worldKey, worldTypeKey.orElse(null), chunkStatusListener);
} catch (final IOException e) {
return FutureUtil.completedWithException(new RuntimeException(String.format("Failed to create level data for world '%s'!", worldKey), e));
}
Expand All @@ -330,13 +330,14 @@ private LevelSettings createLevelSettings(final PrimaryLevelData defaultLevelDat
final GameType gameType = levelStemBridge.bridge$gameMode();
final Boolean hardcore = levelStemBridge.bridge$hardcore();
final Difficulty difficulty = levelStemBridge.bridge$difficulty();
final Boolean commands = levelStemBridge.bridge$commands();
return new LevelSettings(directoryName,
final Boolean allowCommands = levelStemBridge.bridge$allowCommands();
return new LevelSettings(
directoryName,
gameType == null ? defaultLevelData.getGameType() : gameType,
hardcore == null ? defaultLevelData.isHardcore() : hardcore,
difficulty == null ? defaultLevelData.getDifficulty() : difficulty,
commands == null ? defaultLevelData.isAllowCommands() : commands,
new GameRules(),
allowCommands == null ? defaultLevelData.isAllowCommands() : allowCommands,
defaultLevelData.getGameRules().copy(),
defaultLevelData.getDataConfiguration());
}

Expand Down Expand Up @@ -729,7 +730,7 @@ public void loadLevel() {
this.prepareWorld(world);
} else {
try {
final ServerLevel world = this.createLevel(registryKey, template, worldKey, worldTypeKey.orElse(null), chunkStatusListener);
final ServerLevel world = this.createNonDefaultLevel(registryKey, template, worldKey, worldTypeKey.orElse(null), chunkStatusListener);
// Ensure that the world border is registered.
world.getWorldBorder().applySettings(((PrimaryLevelData) world.getLevelData()).getWorldBorder());
this.prepareWorld(world);
Expand Down Expand Up @@ -788,7 +789,8 @@ private PrimaryLevelData loadLevelData(final RegistryAccess.Frozen access, final
return levelData == null ? null : (PrimaryLevelData) levelData.worldData();
}

private ServerLevel createLevel(
// Do not call this for the default world, that is handled very special in loadLevel()
private ServerLevel createNonDefaultLevel(
final net.minecraft.resources.ResourceKey<Level> registryKey,
final LevelStem levelStem,
final ResourceKey worldKey,
Expand Down

0 comments on commit 2ded02a

Please sign in to comment.