Skip to content

Commit

Permalink
refactor: simplify initial grid packet
Browse files Browse the repository at this point in the history
Don't force people to use our own extended menu provider.
  • Loading branch information
raoulvdberge committed Dec 31, 2023
1 parent cec6e71 commit e1f11ca
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.Item;
Expand Down Expand Up @@ -98,7 +97,7 @@ public interface PlatformApi {

PlatformRegistry<GridSynchronizer> getGridSynchronizerRegistry();

MenuProvider getGridMenuProvider(Grid grid, MenuProvider menuProvider);
void writeGridScreenOpeningData(Grid grid, FriendlyByteBuf buf);

UpgradeRegistry getUpgradeRegistry();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.Item;
Expand Down Expand Up @@ -163,8 +162,8 @@ public PlatformRegistry<GridSynchronizer> getGridSynchronizerRegistry() {
}

@Override
public MenuProvider getGridMenuProvider(final Grid grid, final MenuProvider menuProvider) {
return ensureLoaded().getGridMenuProvider(grid, menuProvider);
public void writeGridScreenOpeningData(final Grid grid, final FriendlyByteBuf buf) {
ensureLoaded().writeGridScreenOpeningData(grid, buf);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
import com.refinedmods.refinedstorage2.platform.api.upgrade.BuiltinUpgradeDestinations;
import com.refinedmods.refinedstorage2.platform.api.upgrade.UpgradeRegistry;
import com.refinedmods.refinedstorage2.platform.api.wirelesstransmitter.WirelessTransmitterRangeModifier;
import com.refinedmods.refinedstorage2.platform.common.grid.GridExtendedMenuProvider;
import com.refinedmods.refinedstorage2.platform.common.grid.NoOpGridSynchronizer;
import com.refinedmods.refinedstorage2.platform.common.grid.screen.AbstractGridScreen;
import com.refinedmods.refinedstorage2.platform.common.grid.screen.hint.GridInsertionHintsImpl;
import com.refinedmods.refinedstorage2.platform.common.grid.screen.hint.ItemGridInsertionHint;
import com.refinedmods.refinedstorage2.platform.common.grid.screen.hint.SingleItemGridInsertionHint;
Expand Down Expand Up @@ -96,7 +96,6 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.Item;
Expand Down Expand Up @@ -280,12 +279,8 @@ public PlatformRegistry<GridSynchronizer> getGridSynchronizerRegistry() {
}

@Override
public MenuProvider getGridMenuProvider(final Grid grid, final MenuProvider menuProvider) {
return new GridExtendedMenuProvider(
grid,
storageChannelTypeRegistry,
menuProvider
);
public void writeGridScreenOpeningData(final Grid grid, final FriendlyByteBuf buf) {
AbstractGridScreen.writeScreenOpeningData(storageChannelTypeRegistry, grid, buf);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.refinedmods.refinedstorage2.platform.common.grid;

import com.refinedmods.refinedstorage2.platform.api.PlatformApi;
import com.refinedmods.refinedstorage2.platform.api.grid.Grid;
import com.refinedmods.refinedstorage2.platform.common.content.BlockConstants;
import com.refinedmods.refinedstorage2.platform.common.support.AbstractDirectionalBlock;
import com.refinedmods.refinedstorage2.platform.common.support.BlockItemProvider;
Expand All @@ -10,14 +8,10 @@
import com.refinedmods.refinedstorage2.platform.common.support.direction.BiDirectionType;
import com.refinedmods.refinedstorage2.platform.common.support.direction.DirectionType;

import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.item.DyeColor;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.EntityBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
Expand Down Expand Up @@ -66,14 +60,4 @@ public DyeColor getColor() {
public boolean canAlwaysConnect() {
return true;
}

@Override
@SuppressWarnings("deprecation")
public MenuProvider getMenuProvider(final BlockState state, final Level level, final BlockPos pos) {
final BlockEntity blockEntity = level.getBlockEntity(pos);
if (blockEntity instanceof Grid grid && blockEntity instanceof MenuProvider menuProvider) {
return PlatformApi.INSTANCE.getGridMenuProvider(grid, menuProvider);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@
import com.refinedmods.refinedstorage2.platform.api.support.resource.ItemResource;
import com.refinedmods.refinedstorage2.platform.common.storage.channel.StorageChannelTypes;
import com.refinedmods.refinedstorage2.platform.common.support.AbstractDirectionalBlock;
import com.refinedmods.refinedstorage2.platform.common.support.containermenu.ExtendedMenuProvider;
import com.refinedmods.refinedstorage2.platform.common.support.network.AbstractRedstoneModeNetworkNodeContainerBlockEntity;

import java.util.List;
import java.util.Objects;

import net.minecraft.core.BlockPos;
import net.minecraft.world.MenuProvider;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;

public abstract class AbstractGridBlockEntity
extends AbstractRedstoneModeNetworkNodeContainerBlockEntity<GridNetworkNode>
implements Grid, MenuProvider {
implements Grid, ExtendedMenuProvider {
protected AbstractGridBlockEntity(final BlockEntityType<? extends AbstractGridBlockEntity> type,
final BlockPos pos,
final BlockState state,
Expand All @@ -39,6 +41,11 @@ protected AbstractGridBlockEntity(final BlockEntityType<? extends AbstractGridBl
));
}

@Override
public void writeScreenOpeningData(final ServerPlayer player, final FriendlyByteBuf buf) {
PlatformApi.INSTANCE.writeGridScreenOpeningData(this, buf);
}

@Override
public <T> List<TrackedResourceAmount<T>> getResources(final StorageChannelType<T> type,
final Class<? extends Actor> actorType) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,42 @@

import com.refinedmods.refinedstorage2.platform.api.PlatformApi;
import com.refinedmods.refinedstorage2.platform.api.grid.Grid;
import com.refinedmods.refinedstorage2.platform.api.storage.channel.PlatformStorageChannelType;
import com.refinedmods.refinedstorage2.platform.api.support.network.bounditem.SlotReference;
import com.refinedmods.refinedstorage2.platform.api.support.registry.PlatformRegistry;
import com.refinedmods.refinedstorage2.platform.common.content.ContentNames;
import com.refinedmods.refinedstorage2.platform.common.support.containermenu.ExtendedMenuProvider;

import javax.annotation.Nullable;

import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;

class WirelessGridExtendedMenuProvider extends GridExtendedMenuProvider {
class WirelessGridExtendedMenuProvider implements ExtendedMenuProvider {
private final Grid grid;
private final SlotReference slotReference;

WirelessGridExtendedMenuProvider(final Grid grid,
final PlatformRegistry<PlatformStorageChannelType<?>>
storageChannelTypeRegistry,
final SlotReference slotReference) {
super(grid, storageChannelTypeRegistry, new MenuProvider() {
@Override
public Component getDisplayName() {
return ContentNames.WIRELESS_GRID;
}

@Override
public AbstractContainerMenu createMenu(final int syncId, final Inventory inventory, final Player player) {
return new WirelessGridContainerMenu(syncId, inventory, grid, slotReference);
}
});
WirelessGridExtendedMenuProvider(final Grid grid, final SlotReference slotReference) {
this.grid = grid;
this.slotReference = slotReference;
}

@Override
public void writeScreenOpeningData(final ServerPlayer player, final FriendlyByteBuf buf) {
super.writeScreenOpeningData(player, buf);
PlatformApi.INSTANCE.writeGridScreenOpeningData(grid, buf);
PlatformApi.INSTANCE.writeSlotReference(slotReference, buf);
}

@Override
public Component getDisplayName() {
return ContentNames.WIRELESS_GRID;
}

@Nullable
@Override
public AbstractContainerMenu createMenu(final int syncId, final Inventory inventory, final Player player) {
return new WirelessGridContainerMenu(syncId, inventory, grid, slotReference);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ public void use(final ServerPlayer player,
final NetworkBoundItemSession session) {
session.drainEnergy(Platform.INSTANCE.getConfig().getWirelessGrid().getOpenEnergyUsage());
final Grid grid = new WirelessGrid(session);
Platform.INSTANCE.getMenuOpener().openMenu(player, new WirelessGridExtendedMenuProvider(
grid,
PlatformApi.INSTANCE.getStorageChannelTypeRegistry(),
slotReference
));
Platform.INSTANCE.getMenuOpener().openMenu(player, new WirelessGridExtendedMenuProvider(grid, slotReference));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
import com.refinedmods.refinedstorage2.api.grid.operations.GridInsertMode;
import com.refinedmods.refinedstorage2.api.grid.view.GridResource;
import com.refinedmods.refinedstorage2.api.grid.view.GridView;
import com.refinedmods.refinedstorage2.api.storage.TrackedResourceAmount;
import com.refinedmods.refinedstorage2.api.storage.tracked.TrackedResource;
import com.refinedmods.refinedstorage2.platform.api.PlatformApi;
import com.refinedmods.refinedstorage2.platform.api.grid.Grid;
import com.refinedmods.refinedstorage2.platform.api.grid.GridScrollMode;
import com.refinedmods.refinedstorage2.platform.api.grid.GridSynchronizer;
import com.refinedmods.refinedstorage2.platform.api.grid.view.PlatformGridResource;
import com.refinedmods.refinedstorage2.platform.api.storage.PlayerActor;
import com.refinedmods.refinedstorage2.platform.api.storage.channel.PlatformStorageChannelType;
import com.refinedmods.refinedstorage2.platform.api.support.registry.PlatformRegistry;
import com.refinedmods.refinedstorage2.platform.api.support.resource.ItemResource;
import com.refinedmods.refinedstorage2.platform.common.Platform;
Expand All @@ -22,6 +26,7 @@
import com.refinedmods.refinedstorage2.platform.common.support.widget.History;
import com.refinedmods.refinedstorage2.platform.common.support.widget.RedstoneModeSideButtonWidget;
import com.refinedmods.refinedstorage2.platform.common.support.widget.ScrollbarWidget;
import com.refinedmods.refinedstorage2.platform.common.util.PacketUtil;
import com.refinedmods.refinedstorage2.query.lexer.SyntaxHighlighter;
import com.refinedmods.refinedstorage2.query.lexer.SyntaxHighlighterColors;

Expand All @@ -34,8 +39,10 @@
import net.minecraft.ChatFormatting;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
Expand Down Expand Up @@ -632,4 +639,35 @@ public boolean keyReleased(final int key, final int scanCode, final int modifier

return super.keyReleased(key, scanCode, modifiers);
}

public static void writeScreenOpeningData(final PlatformRegistry<PlatformStorageChannelType<?>>
storageChannelTypeRegistry,
final Grid grid,
final FriendlyByteBuf buf) {
buf.writeBoolean(grid.isGridActive());
final List<PlatformStorageChannelType<?>> types = storageChannelTypeRegistry.getAll();
buf.writeInt(types.size());
types.forEach(type -> writeStorageChannel(storageChannelTypeRegistry, type, grid, buf));
}

private static <T> void writeStorageChannel(
final PlatformRegistry<PlatformStorageChannelType<?>> storageChannelTypeRegistry,
final PlatformStorageChannelType<T> storageChannelType,
final Grid grid,
final FriendlyByteBuf buf
) {
final ResourceLocation id = storageChannelTypeRegistry.getId(storageChannelType).orElseThrow();
buf.writeResourceLocation(id);
final List<TrackedResourceAmount<T>> resources = grid.getResources(storageChannelType, PlayerActor.class);
buf.writeInt(resources.size());
resources.forEach(resource -> writeGridResource(storageChannelType, resource, buf));
}

private static <T> void writeGridResource(final PlatformStorageChannelType<T> storageChannelType,
final TrackedResourceAmount<T> resource,
final FriendlyByteBuf buf) {
storageChannelType.toBuffer(resource.resourceAmount().getResource(), buf);
buf.writeLong(resource.resourceAmount().getAmount());
PacketUtil.writeTrackedResource(buf, resource.trackedResource());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ public AbstractGridContainerMenu createMenu(final int syncId, final Inventory in

@Override
public void writeScreenOpeningData(final ServerPlayer player, final FriendlyByteBuf buf) {
PlatformApi.INSTANCE.writeGridScreenOpeningData(this, buf);
buf.writeLong(energyStorage.getStored());
buf.writeLong(energyStorage.getCapacity());
}
Expand Down
Loading

0 comments on commit e1f11ca

Please sign in to comment.