Skip to content

Commit

Permalink
feat: unified creative tab
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelHillcox committed Jul 27, 2024
1 parent 6c0d32c commit a7b71ca
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 6 deletions.
15 changes: 12 additions & 3 deletions common/src/main/java/dev/ftb/mods/ftblibrary/FTBLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
import dev.architectury.event.events.common.CommandRegistrationEvent;
import dev.architectury.event.events.common.LifecycleEvent;
import dev.architectury.event.events.common.PlayerEvent;
import dev.architectury.networking.NetworkManager;
import dev.architectury.registry.registries.DeferredSupplier;
import dev.architectury.utils.Env;
import dev.architectury.utils.EnvExecutor;
import dev.ftb.mods.ftblibrary.items.ModItems;
import dev.ftb.mods.ftblibrary.net.FTBLibraryNet;
import dev.ftb.mods.ftblibrary.net.SyncKnownServerRegistriesPacket;
import dev.ftb.mods.ftblibrary.util.KnownServerRegistries;
import dev.ftb.mods.ftblibrary.util.NetworkHelper;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.CreativeModeTab;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand All @@ -27,6 +30,8 @@ public FTBLibrary() {
LifecycleEvent.SERVER_STOPPED.register(this::serverStopped);
PlayerEvent.PLAYER_JOIN.register(this::playerJoined);

ModItems.init();

EnvExecutor.runInEnv(Env.CLIENT, () -> FTBLibraryClient::init);
}

Expand All @@ -43,6 +48,10 @@ private void serverStopped(MinecraftServer server) {
}

private void playerJoined(ServerPlayer player) {
NetworkManager.sendToPlayer(player, new SyncKnownServerRegistriesPacket(KnownServerRegistries.server));
NetworkHelper.sendTo(player, new SyncKnownServerRegistriesPacket(KnownServerRegistries.server));
}

public static DeferredSupplier<CreativeModeTab> getCreativeModeTab() {
return ModItems.FTB_LIBRARY_TAB;
}
}
}
26 changes: 26 additions & 0 deletions common/src/main/java/dev/ftb/mods/ftblibrary/items/ModItems.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package dev.ftb.mods.ftblibrary.items;

import dev.architectury.registry.registries.DeferredRegister;
import dev.architectury.registry.registries.DeferredSupplier;
import dev.ftb.mods.ftblibrary.FTBLibrary;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;

public class ModItems {
private static final DeferredRegister<Item> ITEMS = DeferredRegister.create(FTBLibrary.MOD_ID, Registries.ITEM);
private static final DeferredSupplier<Item> ICON_ITEM = ITEMS.register("icon_item", () -> new Item(new Item.Properties()));

private static final DeferredRegister<CreativeModeTab> CREATIVE_MODE_TABS = DeferredRegister.create(FTBLibrary.MOD_ID, Registries.CREATIVE_MODE_TAB);
public static final DeferredSupplier<CreativeModeTab> FTB_LIBRARY_TAB = CREATIVE_MODE_TABS.register("ftb_library", () -> CreativeModeTab.builder(CreativeModeTab.Row.TOP, 0)
.title(Component.translatable("itemGroup.ftbsuite.creative_tab"))
.icon(() -> new ItemStack(ICON_ITEM.get()))
.build());

public static void init() {
CREATIVE_MODE_TABS.register();
ITEMS.register();
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package dev.ftb.mods.ftblibrary.util;

import dev.architectury.injectables.annotations.ExpectPlatform;
import dev.architectury.networking.NetworkManager;
import dev.architectury.platform.Platform;
import dev.architectury.utils.Env;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;

public class NetworkHelper {
public static <T extends CustomPacketPayload> void registerC2S(CustomPacketPayload.Type<T> type, StreamCodec<? super RegistryFriendlyByteBuf, T> codec, NetworkManager.NetworkReceiver<T> handler) {
Expand All @@ -22,8 +25,24 @@ public static <T extends CustomPacketPayload> void registerS2C(CustomPacketPaylo
}
}

@ExpectPlatform
public static void sendToAll(CustomPacketPayload.Type<?> type, MinecraftServer server, Packet<?> packet) {
throw new AssertionError();
}

@ExpectPlatform
public static void sendTo(CustomPacketPayload.Type<?> type, ServerPlayer player, Packet<?> packet) {
throw new AssertionError();
}

@ExpectPlatform
public static <T extends CustomPacketPayload> void sendToAll(MinecraftServer server, T packet) {
NetworkManager.sendToPlayers(server.getPlayerList().getPlayers(), packet);
throw new AssertionError();
}

@ExpectPlatform
public static <T extends CustomPacketPayload> void sendTo(ServerPlayer player, T packet) {
throw new AssertionError();
}

public static <B extends FriendlyByteBuf, V extends Enum<V>> StreamCodec<B, V> enumStreamCodec(Class<V> enumClass) {
Expand Down
4 changes: 3 additions & 1 deletion common/src/main/resources/assets/ftblibrary/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,7 @@
"dimension.minecraft.the_nether": "The Nether",
"dimension.minecraft.the_end": "The End",
"dimension.hyperbox.hyperbox": "Hyperbox",
"dimension.ae2.spatial_storage": "AE2 Spatial Storage"
"dimension.ae2.spatial_storage": "AE2 Spatial Storage",
"itemGroup.ftbsuite.creative_tab": "FTB Suite",
"item.ftblibrary.icon_item": "FTB Icon"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "minecraft:item/generated",
"textures": {
"layer0": "ftblibrary:item/icon_item"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package dev.ftb.mods.ftblibrary.util.fabric;

import dev.architectury.networking.NetworkManager;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;

import java.util.List;

public class NetworkHelperImpl {
public static void sendToAll(CustomPacketPayload.Type<?> type, MinecraftServer server, Packet<?> packet) {
List<ServerPlayer> players = server.getPlayerList().getPlayers();
for (ServerPlayer player : players) {
sendTo(type, player, packet);
}
}

public static void sendTo(CustomPacketPayload.Type<?> type, ServerPlayer player, Packet<?> packet) {
if (ServerPlayNetworking.canSend(player, type)) {
player.connection.send(packet);
}
}

public static <T extends CustomPacketPayload> void sendToAll(MinecraftServer server, T packet) {
List<ServerPlayer> players = server.getPlayerList().getPlayers();
for (ServerPlayer player : players) {
sendTo(player, packet);
}
}

public static <T extends CustomPacketPayload> void sendTo(ServerPlayer player, T packet) {
if (ServerPlayNetworking.canSend(player, packet.type())) {
NetworkManager.sendToPlayer(player, packet);
}
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ org.gradle.daemon=false
# Mod
mod_id=ftblibrary
readable_name=FTB Library
mod_version=2100.1.3
mod_version=2100.1.4
mod_author=FTB Team

# Maven
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package dev.ftb.mods.ftblibrary.util.neoforge;

import dev.architectury.networking.NetworkManager;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;

import java.util.List;

public class NetworkHelperImpl {
public static void sendToAll(CustomPacketPayload.Type<?> type, MinecraftServer server, Packet<?> packet) {
List<ServerPlayer> players = server.getPlayerList().getPlayers();
for (ServerPlayer player : players) {
sendTo(type, player, packet);
}
}

public static void sendTo(CustomPacketPayload.Type<?> type, ServerPlayer player, Packet<?> packet) {
if (player.connection.hasChannel(type)) {
player.connection.send(packet);
}
}

public static <T extends CustomPacketPayload> void sendToAll(MinecraftServer server, T packet) {
List<ServerPlayer> players = server.getPlayerList().getPlayers();
for (ServerPlayer player : players) {
sendTo(player, packet);
}
}

public static <T extends CustomPacketPayload> void sendTo(ServerPlayer player, T packet) {
if (player.connection.hasChannel(packet.type())) {
NetworkManager.sendToPlayer(player, packet);
}
}
}

0 comments on commit a7b71ca

Please sign in to comment.