Skip to content

Commit

Permalink
feat: portable grid activeness and energy
Browse files Browse the repository at this point in the history
Refactors energy info packets and syncing so we can reuse.
  • Loading branch information
raoulvdberge committed Dec 31, 2023
1 parent 552910a commit 5d89d3d
Show file tree
Hide file tree
Showing 26 changed files with 375 additions and 169 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
- Custom disk models. Fluid disks now have a different model.
- Portable Grid

### Changed

- The Portable Grid now shows an energy bar in the UI.

## [2.0.0-milestone.3.2] - 2023-11-03

### Added
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.refinedmods.refinedstorage2.platform.api.configurationcard;

import java.util.Collections;
import java.util.List;

import net.minecraft.nbt.CompoundTag;
Expand All @@ -15,7 +16,11 @@ public interface ConfigurationCardTarget {

void readConfiguration(CompoundTag tag);

List<Item> getUpgradeItems();
default List<Item> getUpgradeItems() {
return Collections.emptyList();
}

boolean addUpgradeItem(Item upgradeItem);
default boolean addUpgradeItem(Item upgradeItem) {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,56 +1,39 @@
package com.refinedmods.refinedstorage2.platform.common.controller;

import com.refinedmods.refinedstorage2.platform.common.Platform;
import com.refinedmods.refinedstorage2.platform.common.content.Menus;
import com.refinedmods.refinedstorage2.platform.common.support.AbstractBaseContainerMenu;
import com.refinedmods.refinedstorage2.platform.common.support.RedstoneMode;
import com.refinedmods.refinedstorage2.platform.common.support.containermenu.ClientProperty;
import com.refinedmods.refinedstorage2.platform.common.support.containermenu.PropertyTypes;
import com.refinedmods.refinedstorage2.platform.common.support.containermenu.ServerProperty;
import com.refinedmods.refinedstorage2.platform.common.support.energy.EnergyContainerMenu;
import com.refinedmods.refinedstorage2.platform.common.support.energy.EnergyInfo;

import javax.annotation.Nullable;

import com.google.common.util.concurrent.RateLimiter;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;

public class ControllerContainerMenu extends AbstractBaseContainerMenu {
private final Player player;
private final RateLimiter energyUpdateRateLimiter = RateLimiter.create(4);

private long stored;
private long capacity;

@Nullable
private ControllerBlockEntity controller;
public class ControllerContainerMenu extends AbstractBaseContainerMenu implements EnergyContainerMenu {
private final EnergyInfo energyInfo;

public ControllerContainerMenu(final int syncId, final Inventory playerInventory, final FriendlyByteBuf buf) {
super(Menus.INSTANCE.getController(), syncId);

addPlayerInventory(playerInventory, 8, 107);

this.stored = buf.readLong();
this.capacity = buf.readLong();
this.player = playerInventory.player;

this.energyInfo = EnergyInfo.forClient(playerInventory.player, buf.readLong(), buf.readLong());
registerProperty(new ClientProperty<>(PropertyTypes.REDSTONE_MODE, RedstoneMode.IGNORE));
}

ControllerContainerMenu(final int syncId,
final Inventory playerInventory,
final ControllerBlockEntity controller,
final Player playerEntity) {
final Player player) {
super(Menus.INSTANCE.getController(), syncId);

this.controller = controller;
this.stored = controller.getActualStored();
this.capacity = controller.getActualCapacity();
this.player = playerEntity;

this.energyInfo = EnergyInfo.forServer(
player,
controller::getActualStored,
controller::getActualCapacity
);
addPlayerInventory(playerInventory, 8, 107);

registerProperty(new ServerProperty<>(
PropertyTypes.REDSTONE_MODE,
controller::getRedstoneMode,
Expand All @@ -61,30 +44,11 @@ public ControllerContainerMenu(final int syncId, final Inventory playerInventory
@Override
public void broadcastChanges() {
super.broadcastChanges();
if (controller == null) {
return;
}
final boolean changed = stored != controller.getActualStored() || capacity != controller.getActualCapacity();
if (changed && energyUpdateRateLimiter.tryAcquire()) {
setEnergyInfo(controller.getActualStored(), controller.getActualCapacity());
Platform.INSTANCE.getServerToClientCommunications().sendControllerEnergyInfo(
(ServerPlayer) player,
stored,
capacity
);
}
}

public void setEnergyInfo(final long newStored, final long newCapacity) {
this.stored = newStored;
this.capacity = newCapacity;
energyInfo.detectChanges();
}

long getStored() {
return stored;
}

long getCapacity() {
return capacity;
@Override
public EnergyInfo getEnergyInfo() {
return energyInfo;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,26 @@
import com.refinedmods.refinedstorage2.platform.common.support.widget.ProgressWidget;
import com.refinedmods.refinedstorage2.platform.common.support.widget.RedstoneModeSideButtonWidget;

import java.util.Collections;
import java.util.List;
import javax.annotation.Nullable;

import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;

import static com.refinedmods.refinedstorage2.platform.common.util.IdentifierUtil.createIdentifier;
import static com.refinedmods.refinedstorage2.platform.common.util.IdentifierUtil.createStoredWithCapacityTranslation;
import static com.refinedmods.refinedstorage2.platform.common.util.IdentifierUtil.createTranslation;

public class ControllerScreen extends AbstractBaseScreen<ControllerContainerMenu> {
private static final ResourceLocation TEXTURE = createIdentifier("textures/gui/controller.png");

private final ProgressWidget progressWidget;
@Nullable
private ProgressWidget progressWidget;

public ControllerScreen(final ControllerContainerMenu menu, final Inventory playerInventory, final Component text) {
super(menu, playerInventory, text);

this.inventoryLabelY = 94;
this.imageWidth = 176;
this.imageHeight = 189;

this.progressWidget = new ProgressWidget(
80,
20,
16,
70,
this::getPercentageFull,
this::createTooltip
);
addRenderableWidget(progressWidget);
}

@Override
Expand All @@ -47,28 +34,24 @@ protected void init() {
getMenu().getProperty(PropertyTypes.REDSTONE_MODE),
createTranslation("gui", "controller.redstone_mode_help")
));
if (progressWidget == null) {
progressWidget = new ProgressWidget(
leftPos + 80,
topPos + 20,
16,
70,
getMenu().getEnergyInfo()::getPercentageFull,
getMenu().getEnergyInfo()::createTooltip
);
} else {
progressWidget.setX(leftPos + 80);
progressWidget.setY(topPos + 20);
}
addRenderableWidget(progressWidget);
}

@Override
protected ResourceLocation getTexture() {
return TEXTURE;
}

private double getPercentageFull() {
return (double) getMenu().getStored() / (double) getMenu().getCapacity();
}

private List<Component> createTooltip() {
return Collections.singletonList(createStoredWithCapacityTranslation(
getMenu().getStored(),
getMenu().getCapacity(),
getPercentageFull()
));
}

@Override
protected void renderLabels(final GuiGraphics graphics, final int mouseX, final int mouseY) {
super.renderLabels(graphics, mouseX, mouseY);
progressWidget.render(graphics, mouseX - leftPos, mouseY - topPos, 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public void writeScreenOpeningData(final ServerPlayer player, final FriendlyByte
final List<PlatformStorageChannelType<?>> types = storageChannelTypeRegistry.getAll();
buf.writeInt(types.size());
types.forEach(type -> writeStorageChannel(type, buf));
if (menuProvider instanceof ExtendedMenuProvider extendedMenuProvider) {
extendedMenuProvider.writeScreenOpeningData(player, buf);
}
}

private <T> void writeStorageChannel(final PlatformStorageChannelType<T> storageChannelType,
Expand Down
Loading

0 comments on commit 5d89d3d

Please sign in to comment.