Skip to content

Commit

Permalink
Avoid to load client side class
Browse files Browse the repository at this point in the history
Add Error Messages
  • Loading branch information
Karashok-Leo committed Aug 20, 2024
1 parent 5a5d877 commit 7382e4f
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 5 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ yarn_mappings=1.20.1+build.10
loader_version=0.15.7

# Mod Properties
mod_version=1.2.1
mod_version=1.2.3
maven_group=karashokleo.loot_bag
archives_base_name=loot-bag

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public BagEntry(Identifier id, Bag bag)
id ->
{
BagEntry entry = LootBagData.BAGS.get(id);
return entry == null ? DataResult.error(() -> "Cannot find entry with id: " + id) : DataResult.success(entry);
return entry == null ? DataResult.error(() -> LootBagData.unknownBagMessage(id)) : DataResult.success(entry);
},
BagEntry::id
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public ContentEntry(Identifier id, Content content)
id ->
{
ContentEntry entry = LootBagData.CONTENTS.get(id);
return entry == null ? DataResult.error(() -> "Cannot find entry with id: " + id) : DataResult.success(entry);
return entry == null ? DataResult.error(() -> LootBagData.unknownContentMessage(id)) : DataResult.success(entry);
},
ContentEntry::id
);
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/karashokleo/loot_bag/internal/data/LootBagData.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ public class LootBagData
public static final Map<Identifier, ContentEntry> CONTENTS = new HashMap<>();
public static final Map<Identifier, BagEntry> BAGS = new HashMap<>();

public static String unknownContentMessage(Identifier id)
{
return "Unknown loot content: '%s'".formatted(id);
}

public static String unknownBagMessage(Identifier id)
{
return "Unknown loot bag: '%s'".formatted(id);
}

public static void putContent(Identifier id, Content content)
{
CONTENTS.put(id, new ContentEntry(id, content));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSyntaxException;
import karashokleo.loot_bag.api.common.bag.BagEntry;
import karashokleo.loot_bag.internal.data.LootBagData;
import karashokleo.loot_bag.internal.fabric.LootBagMod;
Expand Down Expand Up @@ -69,12 +70,15 @@ public void addEntryFields(JsonObject jsonObject, LootBagEntry leafEntry, JsonSe
@Override
protected LootBagEntry fromJson(JsonObject entryJson, JsonDeserializationContext context, int weight, int quality, LootCondition[] conditions, LootFunction[] functions)
{
Identifier id = new Identifier(JsonHelper.getString(entryJson, KEY));
BagEntry bagEntry = LootBagData.BAGS.get(id);
if (bagEntry == null) throw new JsonSyntaxException(LootBagData.unknownBagMessage(id));
return new LootBagEntry(
weight,
quality,
conditions,
functions,
LootBagData.BAGS.get(new Identifier(JsonHelper.getString(entryJson, KEY)))
bagEntry
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import karashokleo.loot_bag.api.common.bag.BagEntry;
import karashokleo.loot_bag.internal.data.LootBagData;
import karashokleo.loot_bag.internal.fabric.LootBagMod;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.networking.v1.FabricPacket;
import net.fabricmc.fabric.api.networking.v1.PacketSender;
import net.fabricmc.fabric.api.networking.v1.PacketType;
Expand Down Expand Up @@ -36,11 +38,12 @@ public PacketType<?> getType()
return TYPE;
}

@Environment(EnvType.CLIENT)
@SuppressWarnings("unused")
public static void handle(SetScreenPacket packet, ClientPlayerEntity player, PacketSender responseSender)
{
BagEntry entry = LootBagData.BAGS.get(packet.bagId());
if (entry == null) return;
if (entry == null) throw new IllegalStateException(LootBagData.unknownBagMessage(packet.bagId()));
LootBagScreen<?> screen = LootBagScreenRegistry.getFactory(entry.bag().getType()).createScreen(entry.bag(), packet.slot);
MinecraftClient.getInstance().setScreen(screen);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import karashokleo.loot_bag.api.common.content.ContentEntry;
import karashokleo.loot_bag.internal.data.LootBagData;
import karashokleo.loot_bag.internal.fabric.LootBagMod;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.networking.v1.PacketSender;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayNetworkHandler;
Expand All @@ -32,6 +34,7 @@ public static void write(PacketByteBuf buf)
}
}

@Environment(EnvType.CLIENT)
@SuppressWarnings("unused")
public static void handle(MinecraftClient client, ClientPlayNetworkHandler handler, PacketByteBuf buf, PacketSender responseSender)
{
Expand Down

0 comments on commit 7382e4f

Please sign in to comment.