Skip to content

Commit

Permalink
Implement RegisterTagEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
aromaa committed Feb 8, 2025
1 parent 7b0547d commit ae1336a
Show file tree
Hide file tree
Showing 24 changed files with 1,071 additions and 1 deletion.
2 changes: 1 addition & 1 deletion SpongeAPI
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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.tags;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.llamalad7.mixinextras.sugar.Local;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagEntry;
import net.minecraft.tags.TagLoader;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Coerce;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.common.bridge.tags.TagLoaderBridge;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

@Mixin(TagLoader.class)
public abstract class TagLoaderMixin_Forge<T> implements TagLoaderBridge<T> {

@WrapOperation(method = "build(Lnet/minecraft/tags/TagEntry$Lookup;Ljava/util/List;)Lcom/mojang/datafixers/util/Either;",
at = @At(value = "INVOKE", target = "Lnet/minecraft/tags/TagEntry;build(Lnet/minecraft/tags/TagEntry$Lookup;Ljava/util/function/Consumer;)Z"))
protected boolean forge$onBuildTag(final TagEntry instance, final TagEntry.Lookup<T> lookup, final Consumer<T> consumer, final Operation<Boolean> original,
final TagEntry.Lookup<T> $$0, final List<TagLoader.EntryWithSource> $$1, final @Local TagLoader.EntryWithSource entry) {
if (entry.remove()) {
return original.call(instance, lookup, consumer);
}
return this.bridge$acceptTag(instance, lookup, consumer, original::call, $$1, entry);
}

@Inject(method = "lambda$build$6", at = @At("HEAD"))
private void forge$onStartBuildingTag(final TagEntry.Lookup<T> $$0x, final Map<ResourceLocation, Collection<T>> $$1x, final ResourceLocation $$2x,
final @Coerce Object $$3x, final CallbackInfo ci) {
this.bridge$buildingTagKey($$2x);
}

@Inject(method = "lambda$build$6", at = @At("RETURN"))
private void forge$onDoneBuildingTag(final CallbackInfo ci) {
this.bridge$buildingTagKey(null);
}

@Override
public boolean bridge$isAdd(final TagLoader.EntryWithSource entry) {
return !entry.remove();
}
}
1 change: 1 addition & 0 deletions forge/src/mixins/resources/mixins.spongeforge.core.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"server.commands.SpreadPlayersCommandMixin_Forge",
"server.level.ServerPlayerMixin_Forge",
"server.network.ServerGamePacketListenerImplMixin_Forge",
"tags.TagLoaderMixin_Forge",
"world.entity.LivingEntityMixin_Forge",
"world.entity.LivingEntityMixin_Forge_Attack_Impl",
"world.entity.item.ItemEntityMixin_Forge",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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.neoforge.mixin.core.tags;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.llamalad7.mixinextras.sugar.Local;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagEntry;
import net.minecraft.tags.TagLoader;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Coerce;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.common.bridge.tags.TagLoaderBridge;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

@Mixin(TagLoader.class)
public abstract class TagLoaderMixin_NeoForge<T> implements TagLoaderBridge<T> {

@WrapOperation(method = "build(Lnet/minecraft/tags/TagEntry$Lookup;Ljava/util/List;)Lcom/mojang/datafixers/util/Either;",
at = @At(value = "INVOKE", target = "Lnet/minecraft/tags/TagEntry;build(Lnet/minecraft/tags/TagEntry$Lookup;Ljava/util/function/Consumer;)Z"))
protected boolean neo$onBuildTag(final TagEntry instance, final TagEntry.Lookup<T> lookup, final Consumer<T> consumer, final Operation<Boolean> original,
final TagEntry.Lookup<T> $$0, final List<TagLoader.EntryWithSource> $$1, final @Local TagLoader.EntryWithSource entry) {
if (entry.remove()) {
return original.call(instance, lookup, consumer);
}
return this.bridge$acceptTag(instance, lookup, consumer, original::call, $$1, entry);
}

@Inject(method = "lambda$build$6", at = @At("HEAD"))
private void neo$onStartBuildingTag(final TagEntry.Lookup<T> $$0x, final Map<ResourceLocation, Collection<T>> $$1x, final ResourceLocation $$2x,
final @Coerce Object $$3x, final CallbackInfo ci) {
this.bridge$buildingTagKey($$2x);
}

@Inject(method = "lambda$build$6", at = @At("RETURN"))
private void neo$onDoneBuildingTag(final CallbackInfo ci) {
this.bridge$buildingTagKey(null);
}

@Override
public boolean bridge$isAdd(final TagLoader.EntryWithSource entry) {
return !entry.remove();
}
}
1 change: 1 addition & 0 deletions neoforge/src/mixins/resources/mixins.spongeneo.core.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"server.level.ServerEntityMixin_Neo",
"server.level.ServerPlayerMixin_Neo",
"server.network.ServerGamePacketListenerImplMixin_Neo",
"tags.TagLoaderMixin_NeoForge",
"world.entity.LivingEntityMixin_Neo",
"world.entity.LivingEntityMixin_Neo_Attack_Impl",
"world.entity.item.ItemEntityMixin_Neo",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.tags;

import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagEntry;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(TagEntry.class)
public interface TagEntryAccessor {

@Accessor("id") ResourceLocation accessor$id();

@Accessor("tag") boolean accessor$tag();
}
1 change: 1 addition & 0 deletions src/accessors/resources/mixins.sponge.accessors.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"server.rcon.thread.RconClientAccessor",
"sounds.SoundEventAccessor",
"stats.StatsCounterAccessor",
"tags.TagEntryAccessor",
"util.datafix.schemas.V100Accessor",
"util.thread.ProcessorMailboxAccessor",
"world.CompoundContainerAccessor",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.inject.Injector;
import net.minecraft.core.RegistryAccess;
import net.minecraft.server.RegistryLayer;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.world.level.WorldDataConfiguration;
import org.spongepowered.api.Client;
import org.spongepowered.api.Engine;
Expand Down Expand Up @@ -84,4 +85,6 @@ public interface Lifecycle {
void callStoppedGameEvent();

void setWorldDataConfiguration(WorldDataConfiguration first);

void establishTags(ResourceManager resourceManager);
}
9 changes: 9 additions & 0 deletions src/main/java/org/spongepowered/common/SpongeLifecycle.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.server.RegistryLayer;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.world.flag.FeatureFlagSet;
import net.minecraft.world.level.WorldDataConfiguration;
import org.checkerframework.checker.nullness.qual.Nullable;
Expand All @@ -46,13 +47,15 @@
import org.spongepowered.api.event.SpongeEventFactory;
import org.spongepowered.common.applaunch.plugin.DummyPluginContainer;
import org.spongepowered.common.bridge.server.MinecraftServerBridge;
import org.spongepowered.common.bridge.server.packs.resources.ResourceManagerBridge;
import org.spongepowered.common.data.SpongeDataManager;
import org.spongepowered.common.event.lifecycle.AbstractRegisterRegistryEvent;
import org.spongepowered.common.event.lifecycle.AbstractRegisterRegistryValueEvent;
import org.spongepowered.common.event.lifecycle.RegisterBuilderEventImpl;
import org.spongepowered.common.event.lifecycle.RegisterChannelEventImpl;
import org.spongepowered.common.event.lifecycle.RegisterDataEventImpl;
import org.spongepowered.common.event.lifecycle.RegisterFactoryEventImpl;
import org.spongepowered.common.event.lifecycle.RegisterTagEventImpl;
import org.spongepowered.common.event.manager.SpongeEventManager;
import org.spongepowered.common.event.tracking.PhaseTracker;
import org.spongepowered.common.launch.Lifecycle;
Expand Down Expand Up @@ -283,4 +286,10 @@ private Collection<PluginContainer> filterInternalPlugins(final Collection<Plugi
.collect(Collectors.toList());
}

@Override
public void establishTags(final ResourceManager resourceManager) {
final RegisterTagEventImpl event = new RegisterTagEventImpl(Cause.of(EventContext.empty(), this.game), this.game);
this.game.eventManager().post(event);
((ResourceManagerBridge) resourceManager).bridge$pluginProvidedTags(event.tags());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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.server.packs.resources;

import org.spongepowered.common.tag.SpongePluginTags;

public interface ResourceManagerBridge {

void bridge$pluginProvidedTags(SpongePluginTags pluginTags);

SpongePluginTags bridge$pluginProvidedTags();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.tags;

import net.minecraft.core.RegistryAccess;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagEntry;
import net.minecraft.tags.TagLoader;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.List;
import java.util.function.Consumer;

public interface TagLoaderBridge<T> {

void bridge$registryEntry(RegistryAccess.RegistryEntry<T> registryEntry);

void bridge$buildingTagKey(@Nullable ResourceLocation key);

default boolean bridge$isAdd(final TagLoader.EntryWithSource entry) {
return true;
}

boolean bridge$acceptTag(TagEntry instance, TagEntry.Lookup<T> lookup, Consumer<T> consumer,
final AcceptTag<T> original, List<TagLoader.EntryWithSource> tags, TagLoader.EntryWithSource entry);

interface AcceptTag<T> {
boolean accept(TagEntry instance, TagEntry.Lookup<T> lookup, Consumer<T> consumer);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.tags;

import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.common.tag.SpongePluginTagPredicate;

import java.util.Set;

public interface TagLoader_EntryWithSourceBridge {

void bridge$predicates(Set<SpongePluginTagPredicate<?>> predicates);

@Nullable Set<SpongePluginTagPredicate<?>> bridge$predicates();
}
Loading

0 comments on commit ae1336a

Please sign in to comment.