Skip to content

Commit

Permalink
Initial port to NeoForge + 1.21
Browse files Browse the repository at this point in the history
  • Loading branch information
3TUSK committed Aug 23, 2024
1 parent 54b7a77 commit e401aa2
Show file tree
Hide file tree
Showing 37 changed files with 678 additions and 662 deletions.
26 changes: 5 additions & 21 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ neoForge {
sourceSet(sourceSets.main)
}
}

unitTest {
enable()
testedMod = mods.voteme
}
}

configurations {
Expand All @@ -117,27 +122,6 @@ shadowJar {
}
tasks.build.dependsOn shadowJar

tasks.withType(ProcessResources).configureEach {
var replaceProperties = [
minecraft_version : minecraft_version,
minecraft_version_range: minecraft_version_range,
neo_version : neo_version,
neo_version_range : neo_version_range,
loader_version_range : loader_version_range,
mod_id : mod_id,
mod_name : mod_name,
mod_license : mod_license,
mod_version : mod_version,
mod_authors : mod_authors,
mod_description : mod_description
]
inputs.properties replaceProperties

filesMatching(['META-INF/neoforge.mods.toml']) {
expand replaceProperties
}
}

publishing {
publications {
release(MavenPublication) {
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/org/teacon/voteme/VoteMe.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.teacon.voteme;

import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.config.ModConfig;
import net.neoforged.fml.ModContainer;
import net.neoforged.fml.common.Mod;
import net.neoforged.fml.config.ModConfig;
import net.neoforged.neoforge.common.ModConfigSpec;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -17,11 +17,11 @@
public final class VoteMe {
public static final Logger LOGGER = LogManager.getLogger(VoteMe.class);

public static final VoteMeConfig CONFIG;
public static VoteMeConfig CONFIG;

static {
Pair<VoteMeConfig, ForgeConfigSpec> pair = new ForgeConfigSpec.Builder().configure(VoteMeConfig::new);
ModLoadingContext.get().registerConfig(ModConfig.Type.SERVER, pair.getRight());
public VoteMe(ModContainer modContainer) {
Pair<VoteMeConfig, ModConfigSpec> pair = new ModConfigSpec.Builder().configure(VoteMeConfig::new);
modContainer.registerConfig(ModConfig.Type.SERVER, pair.getRight());
CONFIG = pair.getLeft();
}
}
6 changes: 3 additions & 3 deletions src/main/java/org/teacon/voteme/VoteMeConfig.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package org.teacon.voteme;

import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraftforge.common.ForgeConfigSpec;
import net.neoforged.neoforge.common.ModConfigSpec;

import javax.annotation.ParametersAreNonnullByDefault;

@MethodsReturnNonnullByDefault
@ParametersAreNonnullByDefault
public final class VoteMeConfig {
public final ForgeConfigSpec.ConfigValue<String> REDIS_ATTACH_URI;
public final ModConfigSpec.ConfigValue<String> REDIS_ATTACH_URI;

public VoteMeConfig(ForgeConfigSpec.Builder builder) {
public VoteMeConfig(ModConfigSpec.Builder builder) {
REDIS_ATTACH_URI = builder
.comment(
"Redis attach uri (example: redis://password@localhost:6379/0), env substitution supported",
Expand Down
21 changes: 18 additions & 3 deletions src/main/java/org/teacon/voteme/category/VoteCategory.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,29 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.core.HolderLookup;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.ComponentSerialization;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.util.GsonHelper;

import javax.annotation.ParametersAreNonnullByDefault;

@MethodsReturnNonnullByDefault
@ParametersAreNonnullByDefault
public final class VoteCategory {

public static final StreamCodec<RegistryFriendlyByteBuf, VoteCategory> STREAM_CODEC = StreamCodec.composite(
ComponentSerialization.STREAM_CODEC, c -> c.name,
ComponentSerialization.STREAM_CODEC, c -> c.description,
ByteBufCodecs.BOOL, c -> c.enabledDefault,
ByteBufCodecs.BOOL, c -> c.enabledModifiable,
VoteCategory::new
);

public final Component name;
public final Component description;
public final boolean enabledDefault, enabledModifiable;
Expand All @@ -23,13 +38,13 @@ public VoteCategory(Component name, Component description, boolean enabledDefaul
this.enabledModifiable = enabledModifiable;
}

public static VoteCategory fromJson(JsonElement json) {
public static VoteCategory fromJson(JsonElement json, HolderLookup.Provider registries) {
JsonObject root = json.getAsJsonObject();
JsonObject enabled = GsonHelper.getAsJsonObject(root, "enabled");
boolean enabledDefault = GsonHelper.getAsBoolean(enabled, "default");
boolean enabledModifiable = GsonHelper.getAsBoolean(enabled, "modifiable");
Component name = Component.Serializer.fromJson(root.get("name"));
Component desc = Component.Serializer.fromJson(root.get("description"));
Component name = Component.Serializer.fromJson(root.get("name"), registries);
Component desc = Component.Serializer.fromJson(root.get("description"), registries);
if (name == null || desc == null) {
throw new JsonSyntaxException("Both name and description are expected");
}
Expand Down
27 changes: 15 additions & 12 deletions src/main/java/org/teacon/voteme/category/VoteCategoryHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.core.HolderLookup;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.HoverEvent;
import net.minecraft.network.chat.MutableComponent;
Expand All @@ -17,12 +18,12 @@
import net.minecraft.server.packs.resources.SimpleJsonResourceReloadListener;
import net.minecraft.util.profiling.ProfilerFiller;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.event.AddReloadListenerEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.network.PacketDistributor;
import net.minecraftforge.server.ServerLifecycleHooks;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.neoforge.event.AddReloadListenerEvent;
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
import net.neoforged.neoforge.network.PacketDistributor;
import net.neoforged.neoforge.server.ServerLifecycleHooks;
import org.teacon.voteme.network.SyncCategoryPacket;
import org.teacon.voteme.network.VoteMePacketManager;

Expand All @@ -33,14 +34,16 @@

@MethodsReturnNonnullByDefault
@ParametersAreNonnullByDefault
@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.FORGE)
@EventBusSubscriber(bus = EventBusSubscriber.Bus.GAME)
public final class VoteCategoryHandler extends SimpleJsonResourceReloadListener {
private static final Gson GSON = new GsonBuilder().create();

private static ImmutableMap<ResourceLocation, VoteCategory> categoryMap = ImmutableMap.of();
private final HolderLookup.Provider registries;

public VoteCategoryHandler() {
public VoteCategoryHandler(HolderLookup.Provider registries) {
super(GSON, "vote_categories");
this.registries = registries;
}

public static Optional<VoteCategory> getCategory(ResourceLocation id) {
Expand All @@ -53,11 +56,11 @@ public static Collection<? extends ResourceLocation> getIds() {

@Override
protected void apply(Map<ResourceLocation, JsonElement> objects, ResourceManager manager, ProfilerFiller profiler) {
categoryMap = ImmutableSortedMap.copyOf(Maps.transformValues(objects, VoteCategory::fromJson));
categoryMap = ImmutableSortedMap.copyOf(Maps.transformValues(objects, v -> VoteCategory.fromJson(v, this.registries)));
MinecraftServer server = ServerLifecycleHooks.getCurrentServer();
if (server != null) {
SyncCategoryPacket packet = SyncCategoryPacket.create(categoryMap);
VoteMePacketManager.CHANNEL.send(PacketDistributor.ALL.noArg(), packet);
PacketDistributor.sendToAllPlayers(packet);
}
}

Expand All @@ -67,15 +70,15 @@ public static void setCategoriesFromServer(ImmutableMap<ResourceLocation, VoteCa

@SubscribeEvent
public static void addReloadListener(AddReloadListenerEvent event) {
event.addListener(new VoteCategoryHandler());
event.addListener(new VoteCategoryHandler(event.getRegistryAccess()));
}

@SubscribeEvent
public static void onLogin(PlayerEvent.PlayerLoggedInEvent event) {
Player player = event.getEntity();
if (player instanceof ServerPlayer serverPlayer) {
SyncCategoryPacket packet = SyncCategoryPacket.create(categoryMap);
VoteMePacketManager.CHANNEL.send(PacketDistributor.PLAYER.with(() -> serverPlayer), packet);
PacketDistributor.sendToPlayer(serverPlayer, packet);
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/main/java/org/teacon/voteme/command/AliasArgumentType.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.synchronization.ArgumentTypeInfos;
import net.minecraft.commands.synchronization.SingletonArgumentInfo;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegisterEvent;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.neoforge.registries.RegisterEvent;
import org.teacon.voteme.vote.VoteArtifactNames;

import javax.annotation.ParametersAreNonnullByDefault;
Expand All @@ -21,7 +21,7 @@

@MethodsReturnNonnullByDefault
@ParametersAreNonnullByDefault
@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD)
@EventBusSubscriber(bus = EventBusSubscriber.Bus.MOD)
public final class AliasArgumentType implements ArgumentType<String> {

public static AliasArgumentType alias() {
Expand Down Expand Up @@ -51,7 +51,7 @@ public Collection<String> getExamples() {

@SubscribeEvent
public static void register(RegisterEvent event) {
event.register(ForgeRegistries.COMMAND_ARGUMENT_TYPES.getRegistryKey(), new ResourceLocation("voteme:alias"), () ->
event.register(Registries.COMMAND_ARGUMENT_TYPE, ResourceLocation.parse("voteme:alias"), () ->
ArgumentTypeInfos.registerByClass(AliasArgumentType.class, SingletonArgumentInfo.contextFree(AliasArgumentType::alias)));
}
}
16 changes: 10 additions & 6 deletions src/main/java/org/teacon/voteme/command/ArtifactArgumentType.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
import net.minecraft.commands.SharedSuggestionProvider;
import net.minecraft.commands.synchronization.ArgumentTypeInfos;
import net.minecraft.commands.synchronization.SingletonArgumentInfo;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegisterEvent;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.neoforge.registries.NeoForgeRegistries;
import net.neoforged.neoforge.registries.RegisterEvent;
import org.teacon.voteme.vote.VoteArtifactNames;

import javax.annotation.ParametersAreNonnullByDefault;
Expand All @@ -30,7 +31,7 @@

@MethodsReturnNonnullByDefault
@ParametersAreNonnullByDefault
@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD)
@EventBusSubscriber(bus = EventBusSubscriber.Bus.MOD)
public final class ArtifactArgumentType implements ArgumentType<UUID> {
private static final Pattern UUID_PATTERN = Pattern.compile("^([-0-9a-fA-F]+)");

Expand Down Expand Up @@ -87,7 +88,10 @@ public Collection<String> getExamples() {

@SubscribeEvent
public static void register(RegisterEvent event) {
event.register(ForgeRegistries.COMMAND_ARGUMENT_TYPES.getRegistryKey(), new ResourceLocation("voteme:artifact"), () ->
if (event.getRegistryKey() != Registries.COMMAND_ARGUMENT_TYPE) {
return;
}
event.register(Registries.COMMAND_ARGUMENT_TYPE, ResourceLocation.parse("voteme:artifact"), () ->
ArgumentTypeInfos.registerByClass(ArtifactArgumentType.class, SingletonArgumentInfo.contextFree(ArtifactArgumentType::artifact)));
}
}
26 changes: 14 additions & 12 deletions src/main/java/org/teacon/voteme/command/VoteMeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.event.RegisterCommandsEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.server.permission.PermissionAPI;
import net.minecraftforge.server.permission.nodes.PermissionNode;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.neoforge.event.RegisterCommandsEvent;
import net.neoforged.neoforge.server.permission.PermissionAPI;
import net.neoforged.neoforge.server.permission.nodes.PermissionNode;
import org.apache.commons.lang3.tuple.Pair;
import org.teacon.voteme.category.VoteCategory;
import org.teacon.voteme.category.VoteCategoryHandler;
import org.teacon.voteme.item.CounterItem;
import org.teacon.voteme.item.VoterItem;
import org.teacon.voteme.item.component.ArtifactID;
import org.teacon.voteme.item.component.CategoryID;
import org.teacon.voteme.roles.VoteRoleHandler;
import org.teacon.voteme.vote.VoteArtifactNames;
import org.teacon.voteme.vote.VoteDataStorage;
Expand Down Expand Up @@ -66,7 +68,7 @@

@MethodsReturnNonnullByDefault
@ParametersAreNonnullByDefault
@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.FORGE)
@EventBusSubscriber(bus = EventBusSubscriber.Bus.GAME)
public final class VoteMeCommand {
public static final SimpleCommandExceptionType ALIAS_INVALID = new SimpleCommandExceptionType(Component.translatable("argument.voteme.alias.invalid"));
public static final SimpleCommandExceptionType ARTIFACT_INVALID = new SimpleCommandExceptionType(Component.translatable("argument.voteme.artifact.invalid"));
Expand Down Expand Up @@ -240,8 +242,8 @@ private static int giveCounter(CommandContext<CommandSourceStack> context) throw
Pair<ResourceLocation, VoteCategory> category = Pair.of(location, result.orElseThrow(() -> CATEGORY_NOT_FOUND.create(location)));
for (ServerPlayer player : targets) {
ItemStack item = CounterItem.INSTANCE.get().getDefaultInstance();
item.getOrCreateTag().putString("CurrentCategory", category.getKey().toString());
item.getOrCreateTag().putUUID("CurrentArtifact", artifactID);
item.set(CategoryID.INSTANCE, category.getKey());
item.set(ArtifactID.INSTANCE, artifactID);
processGiveItemToPlayer(player, item);
}
return targets.size();
Expand All @@ -252,7 +254,7 @@ private static int giveVoter(CommandContext<CommandSourceStack> context) throws
Collection<ServerPlayer> targets = getPlayers(context, "targets");
for (ServerPlayer player : targets) {
ItemStack item = VoterItem.INSTANCE.get().getDefaultInstance();
item.getOrCreateTag().putUUID("CurrentArtifact", artifactID);
item.set(ArtifactID.INSTANCE, artifactID);
processGiveItemToPlayer(player, item);
}
return targets.size();
Expand All @@ -262,9 +264,9 @@ private static int openVoter(CommandContext<CommandSourceStack> context) throws
UUID artifactID = getArtifact(context, "artifact");
Collection<ServerPlayer> targets = getPlayers(context, "targets");
for (ServerPlayer player : targets) {
CompoundTag tag = new CompoundTag();
tag.putUUID("CurrentArtifact", artifactID);
VoterItem.INSTANCE.get().open(player, tag);
ItemStack fakeVoter = new ItemStack(VoterItem.INSTANCE);
fakeVoter.set(ArtifactID.INSTANCE, artifactID);
VoterItem.INSTANCE.get().open(player, fakeVoter);
}
return targets.size();
}
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/org/teacon/voteme/command/VoteMePermissions.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@
import net.minecraft.network.chat.Component;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.server.ServerLifecycleHooks;
import net.minecraftforge.server.permission.events.PermissionGatherEvent;
import net.minecraftforge.server.permission.nodes.PermissionDynamicContext;
import net.minecraftforge.server.permission.nodes.PermissionNode;
import net.minecraftforge.server.permission.nodes.PermissionTypes;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.neoforge.server.ServerLifecycleHooks;
import net.neoforged.neoforge.server.permission.events.PermissionGatherEvent;
import net.neoforged.neoforge.server.permission.nodes.PermissionDynamicContext;
import net.neoforged.neoforge.server.permission.nodes.PermissionNode;
import net.neoforged.neoforge.server.permission.nodes.PermissionTypes;

import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.UUID;

@MethodsReturnNonnullByDefault
@ParametersAreNonnullByDefault
@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.FORGE)
@EventBusSubscriber(bus = EventBusSubscriber.Bus.GAME)
public final class VoteMePermissions {
public static final PermissionNode<Boolean> ADMIN = Util.make(new PermissionNode<>("voteme", "admin", PermissionTypes.BOOLEAN, VoteMePermissions::moderator),
node -> node.setInformation(Component.translatable("permission.voteme.admin.name"), Component.translatable("permission.voteme.admin.description")));
Expand Down
Loading

0 comments on commit e401aa2

Please sign in to comment.