Skip to content

Commit

Permalink
Merge branch 'api-8' into fix/worldmanager
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/main/java/org/spongepowered/common/world/server/SpongeWorldManager.java
  • Loading branch information
Yeregorix committed Jun 19, 2022
2 parents 47bcae7 + 9acf6fd commit 00cc9f3
Show file tree
Hide file tree
Showing 22 changed files with 535 additions and 23 deletions.
2 changes: 1 addition & 1 deletion SpongeAPI
Original file line number Diff line number Diff line change
@@ -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.forge.bridge.minecraftforge.fml;

import org.spongepowered.common.bridge.core.RegistryBridge;

public interface ForgeRegistryBridge<T> {

RegistryBridge<T> bridge$parent();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* 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.forge.mixin.core.minecraftforge.core;

import com.google.common.collect.Maps;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.registries.ForgeRegistry;
import net.minecraftforge.registries.IForgeRegistryEntry;
import org.spongepowered.api.ResourceKey;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.common.SpongeCommon;
import org.spongepowered.common.bridge.core.RegistryBridge;
import org.spongepowered.common.registry.SpongeRegistryEntry;
import org.spongepowered.common.registry.SpongeRegistryType;
import org.spongepowered.forge.bridge.minecraftforge.fml.ForgeRegistryBridge;

import java.util.Map;
import java.util.StringJoiner;

@Mixin(ForgeRegistry.class)
public abstract class ForgeRegistryMixin_Forge<V extends IForgeRegistryEntry<V>> implements ForgeRegistryBridge<V> {

// @formatter:off
@Shadow @Final private net.minecraft.resources.ResourceKey<Registry<V>> key;
// @formatter:on

private final Map<ResourceKey, RegistryBridge<V>> forge$parents = Maps.newHashMap();
private boolean forge$warnedIfNoParent;

@Inject(method = "add(ILnet/minecraftforge/registries/IForgeRegistryEntry;Ljava/lang/String;)I", at = @At("TAIL"))
public void forge$writeToParent(final int id, final V value, final String owner, final CallbackInfoReturnable<Integer> cir) {
final ResourceKey root = (ResourceKey) (Object) this.key.getRegistryName();
final ResourceKey location = (ResourceKey) (Object) this.key.location();

if (!this.forge$warnedIfNoParent && this.forge$parents.isEmpty()) {
// We only care about minecraft namespaced registries, as that is what we've got parents for.
if (location.namespace().equalsIgnoreCase("minecraft")) {
SpongeCommon.logger().error(String.format(
"No parent registry found for %s, things might not work correctly!",
new StringJoiner("/").add(root.formatted()).add(location.formatted())
));
}
this.forge$warnedIfNoParent = true;
}

final SpongeRegistryEntry<V> entry = new SpongeRegistryEntry<>(new SpongeRegistryType<>(root, location),
(ResourceKey) (Object) value.getRegistryName(), value);

this.forge$parents.values().forEach(registry -> registry.bridge$register(entry));
}

@SuppressWarnings("unchecked")
@Inject(method = "setSlaveMap", at = @At("TAIL"))
public void forge$establishParent(final ResourceLocation name, final Object obj, final CallbackInfo ci) {
if (obj instanceof RegistryBridge) {
this.forge$parents.put((ResourceKey) (Object) name, (RegistryBridge<V>) obj);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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.forge.mixin.core.minecraftforge.core;

import net.minecraftforge.registries.IForgeRegistryEntry;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.common.mixin.core.core.WritableRegistryMixin;

@Mixin(targets = {"net.minecraftforge.registries.NamespacedWrapper", "net.minecraftforge.registries.NamespacedDefaultedWrapper"})
public abstract class NamespacedWrapperMixin_Forge<T extends IForgeRegistryEntry<T>> extends WritableRegistryMixin<T> {}
2 changes: 2 additions & 0 deletions forge/src/mixins/resources/mixins.spongeforge.core.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
"api.event.message.PlayerChatEventMixin_Forge",
"commands.CommandsMixin_Forge",
"minecraftforge.MinecraftForgeMixin_Forge",
"minecraftforge.core.ForgeRegistryMixin_Forge",
"minecraftforge.core.NamespacedWrapperMixin_Forge",
"minecraftforge.event.ServerChatEventMixin_Forge",
"minecraftforge.event.entity.EntityTravelToDimensionEventMixin_Forge",
"minecraftforge.event.entity.player.PlayerEvent_PlayerChangedDimensionEventMixin_Forge",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public int run(final CommandContext<CommandSourceStack> context) throws CommandS
final CommandResult result = Objects.requireNonNull(this.executor.execute(spongeCommandContext),
"A CommandResult was expected, but the command returned null instead. Report this to the plugin author!");
if (!result.isSuccess()) {
throw new SpongeCommandResultException(result);
throw SpongeCommandResultException.createException(result);
}
return result.result();
} catch (final CommandException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,27 @@

import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.common.adventure.SpongeAdventure;
import org.spongepowered.common.command.result.SpongeCommandResult;

/**
* Used as a vehicle to transfer an error result down to the command
* manager from a Brig supported layer.
*/
public final class SpongeCommandResultException extends CommandSyntaxException {

private final static net.minecraft.network.chat.Component EMPTY = new TextComponent("");
private final static Component EMPTY = new TextComponent("");
private final CommandResult result;

public SpongeCommandResultException(final CommandResult result) {
super(new SimpleCommandExceptionType(SpongeCommandResultException.EMPTY), SpongeCommandResultException.EMPTY);
public static SpongeCommandResultException createException(final CommandResult result) {
return new SpongeCommandResultException(result, result.errorMessage().map(SpongeAdventure::asVanilla).orElse(SpongeCommandResultException.EMPTY));
}

private SpongeCommandResultException(final CommandResult result, final Component error) {
super(new SimpleCommandExceptionType(error), error);
this.result = result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
import org.spongepowered.common.bridge.commands.CommandsBridge;
import org.spongepowered.common.command.SpongeCommandCompletion;
import org.spongepowered.common.command.brigadier.dispatcher.SpongeCommandDispatcher;
import org.spongepowered.common.command.exception.SpongeCommandResultException;
import org.spongepowered.common.command.exception.SpongeCommandSyntaxException;
import org.spongepowered.common.command.registrar.BrigadierCommandRegistrar;
import org.spongepowered.common.command.registrar.SpongeParameterizedCommandRegistrar;
Expand Down Expand Up @@ -341,7 +342,7 @@ public CommandResult process(final CommandCause cause, final String arguments)
}
final Object source = cause.cause().root();

final CommandResult result;
CommandResult result;
try (final CommandPhaseContext context = GeneralPhase.State.COMMAND
.createPhaseContext(PhaseTracker.getInstance())
.source(source)
Expand All @@ -355,6 +356,8 @@ public CommandResult process(final CommandCause cause, final String arguments)
context.buildAndSwitch();
try {
result = this.processCommand(cause, mapping, arguments, command, args);
} catch (final SpongeCommandResultException resultException) {
result = resultException.result();
} catch (final CommandException exception) {
final CommandResult errorResult = CommandResult.builder().result(0)
.error(exception.componentMessage()).build();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* 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.data.persistence;

import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.nbt.TagParser;
import org.spongepowered.api.data.persistence.DataContainer;
import org.spongepowered.api.data.persistence.DataView;
import org.spongepowered.api.data.persistence.InvalidDataException;
import org.spongepowered.api.data.persistence.InvalidDataFormatException;
import org.spongepowered.api.data.persistence.StringDataFormat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.stream.Collectors;

public final class SNBTDataFormat implements StringDataFormat {

private static BufferedReader createBufferedReader(final Reader reader) {
if (reader instanceof BufferedReader) {
return (BufferedReader) reader;
}

return new BufferedReader(reader);
}

@Override
public DataContainer read(final String input) throws InvalidDataException, IOException {
try {
return NBTTranslator.INSTANCE.translate(TagParser.parseTag(input));
} catch (final CommandSyntaxException e) {
throw new InvalidDataException(e);
}
}

@Override
public DataContainer readFrom(final Reader input) throws InvalidDataException {
try {
return NBTTranslator.INSTANCE.translate(
TagParser.parseTag(SNBTDataFormat.createBufferedReader(input).lines().collect(Collectors.joining("\n"))));
} catch (final CommandSyntaxException e) {
throw new InvalidDataException(e);
}
}

@Override
public DataContainer readFrom(final InputStream input) throws InvalidDataFormatException {
try {
return NBTTranslator.INSTANCE.translate(TagParser.parseTag(
new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)).lines().collect(Collectors.joining("\n"))));
} catch (final CommandSyntaxException e) {
throw new InvalidDataException(e);
}
}

@Override
public String write(final DataView data) throws IOException {
return NBTTranslator.INSTANCE.translate(data).toString();
}

@Override
public void writeTo(final Writer output, final DataView data) throws IOException {
output.write(NBTTranslator.INSTANCE.translate(data).toString());
}

@Override
public void writeTo(final OutputStream output, final DataView data) throws IOException {
output.write(NBTTranslator.INSTANCE.translate(data).toString().getBytes(StandardCharsets.UTF_8));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@
import org.spongepowered.common.data.persistence.HoconDataFormat;
import org.spongepowered.common.data.persistence.JsonDataFormat;
import org.spongepowered.common.data.persistence.NBTDataFormat;
import org.spongepowered.common.data.persistence.SNBTDataFormat;
import org.spongepowered.common.data.type.SpongeBodyPart;
import org.spongepowered.common.data.type.SpongeCatType;
import org.spongepowered.common.data.type.SpongeHorseColor;
Expand Down Expand Up @@ -1001,6 +1002,7 @@ public static RegistryLoader<DataFormat> dataFormat() {
return RegistryLoader.of(l -> {
l.add(DataFormats.JSON, k -> new JsonDataFormat());
l.add(DataFormats.HOCON, k -> new HoconDataFormat());
l.add(DataFormats.SNBT, k -> new SNBTDataFormat());
l.add(DataFormats.NBT, k -> new NBTDataFormat());
});
}
Expand Down
Loading

0 comments on commit 00cc9f3

Please sign in to comment.