Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added better datasyncing #93

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher;
import net.minecraft.item.BlockItem;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.state.IntegerProperty;
import net.minecraft.state.Property;
import net.minecraft.state.properties.BlockStateProperties;
Expand All @@ -22,7 +21,6 @@
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.vector.Quaternion;
import net.minecraftforge.client.model.data.EmptyModelData;
import net.minecraftforge.common.Tags;
import software.bernie.geckolib3.renderers.geo.GeoBlockRenderer;
import software.bernie.techarium.client.RenderUtils;
import software.bernie.techarium.client.tile.model.BotariumModel;
Expand Down Expand Up @@ -216,12 +214,7 @@ private static float getMachineProgress(BotariumTile tile) {
}

private static ProgressBarAddon getMachineProgressBarAddon(BotariumTile tile) {
for (ProgressBarAddon progressBarAddon : tile.getController().getMultiProgressBar().getProgressBarAddons()) {
if (progressBarAddon.getName().equals("techarium.gui.mainprogress")) {
return progressBarAddon;
}
}
throw new NullPointerException("No progressbar found");
return tile.getController().getMultiProgressBar().getBarByName("techarium.gui.mainprogress").orElseThrow(() -> new NullPointerException("No progressbar found"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@
import net.minecraft.block.Block;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.ContainerType;
import net.minecraft.inventory.container.IContainerListener;
import net.minecraft.inventory.container.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.IWorldPosCallable;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fml.network.NetworkDirection;
import software.bernie.techarium.machine.controller.MachineController;
import software.bernie.techarium.machine.interfaces.recipe.IMachineRecipe;
import software.bernie.techarium.network.NetworkConnection;
import software.bernie.techarium.network.container.SyncContainerPacket;
import software.bernie.techarium.tile.base.MachineMasterTile;
import software.bernie.techarium.util.Vector2i;
import software.bernie.techarium.util.inventory.ContainerUtil;
Expand Down Expand Up @@ -76,6 +80,17 @@ public MachineController<? extends IMachineRecipe> getMachineController() {
return tile.getController();
}

@Override
public void broadcastChanges() {
super.broadcastChanges();
for (IContainerListener containerListener : containerListeners) {
if (containerListener instanceof ServerPlayerEntity) {
NetworkConnection.INSTANCE.sendTo(new SyncContainerPacket(this), ((ServerPlayerEntity) containerListener).connection.connection, NetworkDirection.PLAY_TO_CLIENT);

}
}
}

@Override
public ItemStack quickMoveStack(PlayerEntity player, int index) {
return ContainerUtil.handleShiftClick(this, player, index);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package software.bernie.techarium.display.screen;

import com.mojang.blaze3d.matrix.MatrixStack;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.ITextComponent;
import software.bernie.techarium.display.container.AutomaticContainer;
import software.bernie.techarium.display.container.ExchangeStationContainer;
import software.bernie.techarium.display.screen.widget.SelectableWidget;
import software.bernie.techarium.display.screen.widget.ListWidget;
import software.bernie.techarium.display.screen.widget.exchange.RecipeWidget;
import software.bernie.techarium.machine.interfaces.recipe.IMachineRecipe;
import software.bernie.techarium.network.NetworkConnection;
import software.bernie.techarium.network.container.RecipeWidgetClickContainerPacket;
import software.bernie.techarium.recipe.recipe.ExchangeStationRecipe;
Expand All @@ -26,6 +27,7 @@ public class ExchangeStationScreen extends AutomaticContainerScreen {
private List<ExchangeStationRecipe> recipes;

private final ExchangeStationContainer exchangeStationContainer;
ListWidget<RecipeWidget> listWidget;

//weird generic stuff, pls put only ExchangeStationStuff here
public ExchangeStationScreen(AutomaticContainer container, PlayerInventory inv, ITextComponent containerName) {
Expand All @@ -36,21 +38,29 @@ public ExchangeStationScreen(AutomaticContainer container, PlayerInventory inv,
recipes = exchangeStationContainer.getMachineController().getRecipes().collect(Collectors.toList());
}

@Override
public void render(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
super.render(matrixStack, mouseX, mouseY, partialTicks);
IMachineRecipe currentRecipe = exchangeStationContainer.getTile().getController().getCurrentRecipe();
if (recipes.contains(currentRecipe)) {
listWidget.select(recipes.indexOf(currentRecipe));
}
}

@Override
protected void init() {
super.init();
List<SelectableWidget> widgets = new ArrayList<>();
List<RecipeWidget> widgets = new ArrayList<>();
for (int i = 0; i < recipes.size(); i++) {
ExchangeStationRecipe recipe = recipes.get(i);
RecipeWidget recipeWidget = new RecipeWidget(new Vector2i(16 + leftPos, 24 + topPos + 18*i), recipe.getInput(), recipe.getOutput());
recipeWidget.onClick = Optional.of((widget, screen) -> NetworkConnection.INSTANCE.sendToServer(new RecipeWidgetClickContainerPacket(getMenu(), widget)));
widgets.add(recipeWidget);
}
ListWidget widget = new ListWidget(new Vector2i(15 + leftPos,23 + topPos), new Vector2i(62,110),new Vector2i(54,0), 110, widgets, 6, 1, 18, this);
addButton(widget);
widget.getRekursiveSubWidgets().forEach(this::addButton);
listWidget = new ListWidget(new Vector2i(15 + leftPos,23 + topPos), new Vector2i(62,110),new Vector2i(54,0), 110, widgets, 6, 1, 18, this);
addButton(listWidget);
listWidget.getRekursiveSubWidgets().forEach(this::addButton);
}

@Override
public ExchangeStationContainer getMenu() {
return (ExchangeStationContainer) super.getMenu();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
import java.util.ArrayList;
import java.util.List;

public class ListWidget extends Widget {
final List<SelectableWidget> widgets;
public class ListWidget<T extends SelectableWidget> extends Widget {
final List<T> widgets;
final int numWidgets;
final ScrollBarWidget scrollBar;
final int firstY;
final int yOff;

private final Screen on;

public ListWidget(Vector2i pos, Vector2i size, Vector2i scrollBarPos, int scrollBarLength, List<SelectableWidget> widgets, int numWidgets, int firstY, int yOff, Screen on) {
public ListWidget(Vector2i pos, Vector2i size, Vector2i scrollBarPos, int scrollBarLength, List<T> widgets, int numWidgets, int firstY, int yOff, Screen on) {
super(pos.getX(), pos.getY(), size.getX(), size.getY(), StringTextComponent.EMPTY);
scrollBar = new ScrollBarWidget(scrollBarPos.add(pos), scrollBarLength, true, false);
this.widgets = widgets;
Expand Down Expand Up @@ -54,11 +54,11 @@ private int topVisibleWidget() {
return Math.min((int)(scrollBar.getScrollPosition()/(1f/(widgets.size()-numWidgets+1))),widgets.size() - numWidgets);
}

public void elementClicked(SelectableWidget widget) {
public void elementClicked(T widget) {
if (!widgets.contains(widget))
return;
for (int i = 0; i < widgets.size(); i++) {
SelectableWidget localWidget = widgets.get(i);
T localWidget = widgets.get(i);
if (localWidget == widget) {
localWidget.setSelected(true);
int finalI = i;
Expand All @@ -69,8 +69,21 @@ public void elementClicked(SelectableWidget widget) {
}
}

public SelectableWidget getSelected() {
for (SelectableWidget widget : widgets) {
public void select(int index) {
for (int i = 0; i < widgets.size(); i++) {
T localWidget = widgets.get(i);
if (i == index) {
localWidget.setSelected(true);
int finalI = i;
localWidget.onClick.ifPresent(onClick -> onClick.accept(finalI, on));
} else {
localWidget.setSelected(false);
}
}
}

public T getSelected() {
for (T widget : widgets) {
if (widget.isSelected())
return widget;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public void forceSetEnergy(int energy) {
this.energy = energy;
}

public void setMaxEnergyStored(int maxEnergyStored) {
capacity = maxEnergyStored;
}

public EnergyStorageAddon(int totalEnergy, int maxIO, int posX, int posY) {
this(totalEnergy, maxIO, maxIO, posX, posY);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,5 +195,4 @@ public List<ITextComponent> createToolTipMessage() {
public static String getFluidName(FluidStack stack) {
return new TranslationTextComponent(stack.getFluid().getAttributes().getTranslationKey(stack)).getString();
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package software.bernie.techarium.machine.addon.progressbar;

import net.minecraft.inventory.container.Slot;
import software.bernie.techarium.machine.addon.fluid.FluidTankAddon;
import software.bernie.techarium.machine.interfaces.IContainerComponentProvider;
import software.bernie.techarium.machine.interfaces.IFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class MultiProgressBarAddon implements IContainerComponentProvider {

Expand Down Expand Up @@ -38,6 +40,9 @@ public void attemptTickAllBars() {
}
}

public Optional<ProgressBarAddon> getBarByName(String name) {
return progressBarAddons.stream().filter(addon -> addon.getName().equals(name)).findFirst();
}
public List<ProgressBarAddon> getProgressBarAddons() {
return progressBarAddons;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public List<IFactory<? extends Slot>> getContainerComponents() {
@Override
public List<ITextComponent> createToolTipMessage() {
DecimalFormat decimalFormat = new DecimalFormat();
int progress = (this.getMaxProgress() - this.getProgress()) / this.getProgressToAdd();
int progress = (this.getMaxProgress() - this.getProgress()) / getProgressToAdd();
if (!this.canProgressUp()) {
progress = this.getMaxProgress() - progress;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import software.bernie.techarium.machine.interfaces.IFactory;
import software.bernie.techarium.machine.interfaces.recipe.IMachineRecipe;
import software.bernie.techarium.tile.base.MachineMasterTile;
import software.bernie.techarium.tile.sync.*;
import software.bernie.techarium.util.Vector2i;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -50,7 +51,7 @@ public class MachineController<T extends IMachineRecipe> implements IContainerCo
private Map<Integer, Vector2i> playerHotbarSlotsXY = new HashMap<>();
private boolean isPowered;
private EnergyStorageAddon energyStorage;
private final LazyOptional<IEnergyStorage> lazyEnergyStorage = LazyOptional.of(this::getEnergyStorage);
private final LazyOptional<EnergyStorageAddon> lazyEnergyStorage = LazyOptional.of(this::getEnergyStorage);

private MultiInventoryAddon multiInventory = new MultiInventoryAddon();
private MultiFluidTankAddon multiTank = new MultiFluidTankAddon();
Expand Down Expand Up @@ -160,7 +161,7 @@ public MultiFluidTankAddon getMultiTank() {
return multiTank;
}

public LazyOptional<IEnergyStorage> getLazyEnergyStorage() {
public LazyOptional<EnergyStorageAddon> getLazyEnergyStorage() {
return lazyEnergyStorage;
}

Expand Down Expand Up @@ -191,18 +192,18 @@ public List<IFactory<? extends Slot>> getContainerComponents() {
return components;
}

public void tick() {
public void tick(boolean isServer) {
if (currentRecipeLocation != null) {
this.currentRecipe = (T) this.tile.getLevel().getRecipeManager().byKey(currentRecipeLocation).orElse(null);
currentRecipeLocation = null;
}
if (!isServer)
return;
int lastEnergy = getEnergyStorage() != null ? getEnergyStorage().getEnergyStored() : 0;
if (multiProgressBar != null) {
this.multiProgressBar.attemptTickAllBars();
}

if(currentRecipeLocation != null)
{
this.currentRecipe = (T) this.tile.getLevel().getRecipeManager().byKey(currentRecipeLocation).orElse(null);
currentRecipeLocation = null;
}

if (currentRecipe == null) {
handleRecipeNull(shouldCheckRecipe);
}
Expand Down Expand Up @@ -240,6 +241,25 @@ public Stream<T> getRecipes() {
.map(tile::castRecipe);
}

public List<TechariumDataSlot<?>> createDataSlots() {
List<TechariumDataSlot<?>> dataSlots = new ArrayList<>();
dataSlots.add(new ResourceLocationDataSlot(
() -> currentRecipe != null ? currentRecipe.getId() : currentRecipeLocation,
recipe -> currentRecipeLocation = recipe));
getLazyEnergyStorage().ifPresent(energyStorage -> {
dataSlots.add(new IntDataSlot(energyStorage::getEnergyStored, energyStorage::forceSetEnergy));
dataSlots.add(new IntDataSlot(energyStorage::getLastDrained, energyStorage::setLastDrained));
});
for (FluidTankAddon fluidTank : getMultiTank().getFluidTanks()) {
dataSlots.add(new FluidStackDataSlot(fluidTank::getFluid, fluidTank::setFluid));
}
for (ProgressBarAddon progressBar : getMultiProgressBar().getProgressBarAddons()) {
dataSlots.add(new IntDataSlot(progressBar::getProgress, progressBar::setProgress));
dataSlots.add(new IntDataSlot(progressBar::getProgressToAdd, progressBar::setProgressToAdd));
}
return dataSlots;
}

@Override
public CompoundNBT serializeNBT() {
CompoundNBT nbt = new CompoundNBT();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public static void registerMessages() {
registerMessage(new ChangedMainConfigContainerPacket());
registerMessage(new RecipeWidgetClickContainerPacket());
registerMessage(new UpdateCoilTypePacket());
registerMessage(new SyncContainerPacket());
}
public static <MSG extends Packet<MSG>> void registerMessage(MSG dummyPacket) {
INSTANCE.registerMessage(getAndUpdateIndex(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package software.bernie.techarium.network.container;

import net.minecraft.client.Minecraft;
import net.minecraft.inventory.container.Container;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.fml.network.NetworkDirection;
import net.minecraftforge.fml.network.NetworkEvent;
import software.bernie.techarium.network.Packet;

import java.util.Optional;
import java.util.function.Function;

public abstract class ServerToClientContainerPacket<MSG extends ServerToClientContainerPacket<MSG>> extends Packet<MSG> {

private int containerID = 0;
private Function<PacketBuffer,MSG> packetCreator;

protected ServerToClientContainerPacket(Function<PacketBuffer,MSG> packetCreator) {
this.packetCreator = packetCreator;
}

protected ServerToClientContainerPacket(Container container) {
this.containerID = container.containerId;
}

protected ServerToClientContainerPacket(PacketBuffer buffer) {
containerID = buffer.readInt();
}
@Override
public boolean isValid(NetworkEvent.Context context) {
return true;
}

@Override
public Optional<NetworkDirection> getDirection() {
return Optional.of(NetworkDirection.PLAY_TO_CLIENT);
}

@Override
public void write(PacketBuffer writeInto) {
writeInto.writeInt(containerID);
}

@Override
public MSG create(PacketBuffer readFrom) {
return packetCreator.apply(readFrom);
}

public Optional<Container> getContainer(NetworkEvent.Context context) {
Container container = Minecraft.getInstance().player.containerMenu;
if (container.containerId == containerID) {
return Optional.of(container);
}
return Optional.empty();
}
}
Loading