Skip to content

Commit

Permalink
Shared: Fixing issue when there is an invalid recipe
Browse files Browse the repository at this point in the history
Also addressing issues with recipes when swapping between servers and single player worlds where disabled/enabled recipes would get out of sync.
  • Loading branch information
Brian-Wuest committed Dec 1, 2024
1 parent c8b6285 commit 3c4ade9
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 8 deletions.
9 changes: 8 additions & 1 deletion Fabric/src/main/java/com/prefab/fabric/Prefab.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import me.shedaniel.autoconfig.AutoConfig;
import me.shedaniel.autoconfig.serializer.GsonConfigSerializer;
import net.fabricmc.api.ModInitializer;
import net.minecraft.nbt.CompoundTag;

public class Prefab implements ModInitializer {
@Override
Expand All @@ -26,7 +27,13 @@ public void onInitialize() {
AutoConfig.register(ModConfiguration.class, GsonConfigSerializer::new);

PrefabBase.serverConfiguration = new ModConfiguration();
PrefabBase.configuration = AutoConfig.getConfigHolder(ModConfiguration.class).getConfig();
ModConfiguration config = AutoConfig.getConfigHolder(ModConfiguration.class).getConfig();

// Make sure the static mod configuration object is separate from the object loaded from the file system.
// This way we don't have issues when players swap between servers and local worlds.
CompoundTag tag = config.writeCompoundTag();
PrefabBase.configuration = new ModConfiguration();
PrefabBase.configuration.readFromTag(tag);

ServerEvents.registerServerEvents();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerEntityEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ServerPlayer;

import java.util.ArrayList;
Expand Down Expand Up @@ -46,7 +47,13 @@ private static void serverStarted() {
ServerLifecycleEvents.SERVER_STARTED.register((server) -> {
// Get the server configuration.
// This will be pushed to the player when they join the world.
PrefabBase.serverConfiguration = AutoConfig.getConfigHolder(ModConfiguration.class).getConfig();
ModConfiguration config = AutoConfig.getConfigHolder(ModConfiguration.class).getConfig();

// Make sure the static mod configuration object is separate from the object loaded from the file system.
// This way we don't have issues when players swap between servers and local worlds.
CompoundTag tag = config.writeCompoundTag();
PrefabBase.serverConfiguration = new ModConfiguration();
PrefabBase.serverConfiguration.readFromTag(tag);

// Do this when the server starts so that all appropriate tags are used.
ItemSickle.setEffectiveBlocks();
Expand Down
9 changes: 8 additions & 1 deletion NeoForge/src/main/java/com/prefab/neoforge/Prefab.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.prefab.neoforge.network.NetworkWrapper;
import me.shedaniel.autoconfig.AutoConfig;
import me.shedaniel.autoconfig.serializer.GsonConfigSerializer;
import net.minecraft.nbt.CompoundTag;
import net.neoforged.neoforge.registries.*;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.item.CreativeModeTab;
Expand Down Expand Up @@ -57,7 +58,13 @@ private void commonSetup(final FMLCommonSetupEvent event)
AutoConfig.register(ModConfiguration.class, GsonConfigSerializer::new);

PrefabBase.serverConfiguration = new ModConfiguration();
PrefabBase.configuration = AutoConfig.getConfigHolder(ModConfiguration.class).getConfig();
ModConfiguration config = AutoConfig.getConfigHolder(ModConfiguration.class).getConfig();

// Make sure the static mod configuration object is separate from the object loaded from the file system.
// This way we don't have issues when players swap between servers and local worlds.
CompoundTag tag = config.writeCompoundTag();
PrefabBase.configuration = new ModConfiguration();
PrefabBase.configuration.readFromTag(tag);
}

// Add the example block item to the building blocks tab
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.prefab.structures.base.StructureGenerator;
import me.shedaniel.autoconfig.AutoConfig;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.ItemStack;
import net.neoforged.bus.api.SubscribeEvent;
Expand Down Expand Up @@ -40,16 +41,24 @@ public void onServerAboutToStart(ServerAboutToStartEvent event)

@SubscribeEvent
public void onServerStarted(ServerStartedEvent event) {
// Get the server configuration.
// This will be pushed to the player when they join the world.
PrefabBase.serverConfiguration = AutoConfig.getConfigHolder(ModConfiguration.class).getConfig();
ModConfiguration config = AutoConfig.getConfigHolder(ModConfiguration.class).getConfig();

// Make sure the static mod configuration object is separate from the object loaded from the file system.
// This way we don't have issues when players swap between servers and local worlds.
CompoundTag tag = config.writeCompoundTag();
PrefabBase.serverConfiguration = new ModConfiguration();
PrefabBase.serverConfiguration.readFromTag(tag);
}

@SubscribeEvent
public void playerJoinedServer(PlayerEvent.PlayerLoggedInEvent event) {
if (!event.getEntity().level().isClientSide() && event.getEntity() instanceof ServerPlayer player) {
PrefabBase.logger.info("{} logged into server, sending config to client", Objects.requireNonNull(player.getDisplayName()).getString());

// Send the server-side configuration
// This is NOT the "serverConfiguration" field as that can get overwritten when playing on client and then on server and then again on client.
TagMessage message = new TagMessage(PrefabBase.serverConfiguration.writeCompoundTag());
PrefabBase.networkWrapper.sendToClient(ServerToClientTypes.MOD_CONFIG_SYNC, (ServerPlayer) event.getEntity(), message);
PrefabBase.networkWrapper.sendToClient(ServerToClientTypes.MOD_CONFIG_SYNC, player, message);

EntityPlayerConfiguration playerConfig = EntityPlayerConfiguration.loadFromEntity(player);

Expand Down
4 changes: 3 additions & 1 deletion Shared/src/com/prefab/recipe/ConditionedShapedRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ private void validateTagIngredients() {
// Found a tag ingredient, loop through the pattern to determine if any of the keys are missing ingredients.
if (this.pattern.data.isPresent()) {
for(Map.Entry<Character, Ingredient> keyMap : this.pattern.data.get().key().entrySet()) {
if (keyMap.getValue().itemStacks.length == 0) {
if (keyMap.getValue().itemStacks == null
|| keyMap.getValue().itemStacks.length == 0) {
invalidRecipe = true;
break;
}
Expand Down Expand Up @@ -209,6 +210,7 @@ public static ItemStack validateRecipeOutput(ItemStack originalOutput, String co
&& !PrefabBase.serverConfiguration.recipes.get(configName)) {
// The configuration option for this recipe was turned off.
// Specify that the recipe has no output which basically makes it disabled.
PrefabBase.logger.debug("{} recipe is disabled, the item result is still available, but players may be unable to craft the associated item(s)", configName);
return ItemStack.EMPTY;
}

Expand Down

1 comment on commit 3c4ade9

@Brian-Wuest
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes #297

Please sign in to comment.