Skip to content
Merged
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 @@ -34,10 +34,8 @@ public static boolean test(ReiMachineRecipes.MachineScreenPredicate predicate, M
case ANY -> true;
case MULTIBLOCK -> {
for (GuiComponentClient client : screen.getMenu().components) {
if (client instanceof CraftingMultiblockGuiClient cmGui) {
if (cmGui.isShapeValid) {
yield true;
}
if (client instanceof CraftingMultiblockGuiClient cmGui && cmGui.isShapeValid) {
yield true;
}
}
yield false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import aztech.modern_industrialization.inventory.ConfigurableItemStack;
import aztech.modern_industrialization.inventory.MIInventory;
import aztech.modern_industrialization.inventory.SlotPositions;
import aztech.modern_industrialization.machines.ComponentStorage;
import aztech.modern_industrialization.machines.GuiComponentsClient;
import aztech.modern_industrialization.util.NbtHelper;
import java.util.ArrayList;
Expand All @@ -52,34 +53,29 @@ public static MachineMenuClient create(int syncId, Inventory playerInventory, Re
SlotPositions fluidPositions = SlotPositions.read(buf);
MIInventory inventory = new MIInventory(itemStacks, fluidStacks, itemPositions, fluidPositions);
// Components
List<GuiComponentClient> components = new ArrayList<>();
ComponentStorage<GuiComponentClient> components = new ComponentStorage<>();
int componentCount = buf.readInt();
for (int i = 0; i < componentCount; ++i) {
ResourceLocation id = buf.readResourceLocation();
components.add(GuiComponentsClient.get(id).createFromInitialData(buf));
components.register(GuiComponentsClient.get(id).createFromInitialData(buf));
}
// GUI params
MachineGuiParameters guiParams = MachineGuiParameters.read(buf);

return new MachineMenuClient(syncId, playerInventory, inventory, components, guiParams);
}

public final List<GuiComponentClient> components;
public final ComponentStorage<GuiComponentClient> components;

private MachineMenuClient(int syncId, Inventory playerInventory, MIInventory inventory, List<GuiComponentClient> components,
private MachineMenuClient(int syncId, Inventory playerInventory, MIInventory inventory, ComponentStorage<GuiComponentClient> components,
MachineGuiParameters guiParams) {
super(syncId, playerInventory, inventory, guiParams, components);
this.components = components;
}

@Nullable
public <T extends GuiComponentClient> T getComponent(Class<T> klass) {
for (GuiComponentClient component : components) {
if (klass.isInstance(component)) {
return (T) component;
}
}
return null;
return components.get(klass);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* MIT License
*
* Copyright (c) 2020 Azercoco & Technici4n
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package aztech.modern_industrialization.machines;

import aztech.modern_industrialization.machines.gui.GuiComponent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;

public sealed class ComponentStorage<C> implements Iterable<C> permits ComponentStorage.GuiServer, ComponentStorage.Server {
protected final List<C> components = new ArrayList<>();

@Override
public Iterator<C> iterator() {
return components.iterator();
}

@SafeVarargs
public final void register(C... components) {
Collections.addAll(this.components, components);
}

@SafeVarargs
public final void unregister(C... components) {
for (C component : components) {
this.components.remove(component);
}
}

public final int size() {
return components.size();
}

public final C get(int index) {
return components.get(index);
}

public final void forEachIndexed(BiConsumer<Integer, C> action) {
for (int i = 0; i < components.size(); i++) {
action.accept(i, components.get(i));
}
}

@Nullable
public final <T> T get(Class<T> clazz) {
for (C component : components) {
if (clazz.isInstance(component)) {
return (T) component;
}
}
return null;
}

public final <T> List<T> getAll(Class<T> clazz) {
List<T> components = new ArrayList<>();
for (C component : this.components) {
if (clazz.isInstance(component)) {
components.add((T) component);
}
}
return components;
}

public final <T> void forType(Class<T> clazz, Consumer<? super T> action) {
List<T> component = getAll(clazz);
for (T c : component) {
action.accept(c);
}
}

public final <T, R> R mapOrDefault(Class<T> clazz, Function<? super T, ? extends R> action, R defaultValue) {
List<T> components = getAll(clazz);
if (components.isEmpty()) {
return defaultValue;
} else if (components.size() == 1) {
return action.apply(components.get(0));
} else {
throw new RuntimeException("Multiple components of type " + clazz.getName() + " found");
}
}

public static final class GuiServer extends ComponentStorage<GuiComponent.Server> {
/**
* @throws RuntimeException if the component doesn't exist.
*/
public <S extends GuiComponent.Server> S get(ResourceLocation componentId) {
for (GuiComponent.Server component : components) {
if (component.getId().equals(componentId)) {
return (S) component;
}
}
throw new RuntimeException("Couldn't find component " + componentId);
}
}

public static final class Server extends ComponentStorage<IComponent> {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@
import aztech.modern_industrialization.util.NbtHelper;
import aztech.modern_industrialization.util.WorldHelper;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import net.minecraft.Util;
import net.minecraft.core.Direction;
import net.minecraft.core.HolderLookup;
Expand All @@ -50,7 +47,6 @@
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.ItemInteractionResult;
Expand All @@ -75,8 +71,8 @@
@SuppressWarnings("rawtypes")
public abstract class MachineBlockEntity extends FastBlockEntity
implements MenuProvider, WrenchableBlockEntity {
public final List<GuiComponent.Server> guiComponents = new ArrayList<>();
private final List<IComponent> icomponents = new ArrayList<>();
public final ComponentStorage.GuiServer guiComponents = new ComponentStorage.GuiServer();
public final ComponentStorage.Server components = new ComponentStorage.Server();
public final MachineGuiParameters guiParams;
/**
* Server-side only: true if the next call to sync() will trigger a remesh.
Expand All @@ -101,59 +97,18 @@ public MachineBlockEntity(BEP bep, MachineGuiParameters guiParams, OrientationCo
}

protected final void registerGuiComponent(GuiComponent.Server... components) {
Collections.addAll(guiComponents, components);
guiComponents.register(components);
}

protected final void registerComponents(IComponent... components) {
Collections.addAll(icomponents, components);
this.components.register(components);
}

/**
* @return The inventory that will be synced with the client.
*/
public abstract MIInventory getInventory();

/**
* @throws RuntimeException if the component doesn't exist.
*/
@SuppressWarnings("unchecked")
public <S extends GuiComponent.Server> S getComponent(ResourceLocation componentId) {
for (GuiComponent.Server component : guiComponents) {
if (component.getId().equals(componentId)) {
return (S) component;
}
}
throw new RuntimeException("Couldn't find component " + componentId);
}

private <T> List<T> tryGetComponent(Class<T> clazz) {
List<T> components = new ArrayList<>();
for (var component : icomponents) {
if (clazz.isInstance(component)) {
components.add((T) component);
}
}
return components;
}

public final <T> void forComponentType(Class<T> clazz, Consumer<? super T> action) {
List<T> component = tryGetComponent(clazz);
for (T c : component) {
action.accept(c);
}
}

public <T, R> R mapComponentOrDefault(Class<T> clazz, Function<? super T, ? extends R> action, R defaultValue) {
List<T> components = tryGetComponent(clazz);
if (components.isEmpty()) {
return defaultValue;
} else if (components.size() == 1) {
return action.apply(components.get(0));
} else {
throw new RuntimeException("Multiple components of type " + clazz.getName() + " found");
}
}

@Override
public final Component getDisplayName() {
return Component.translatable(Util.makeDescriptionId("block", guiParams.blockId));
Expand Down Expand Up @@ -238,15 +193,15 @@ public CompoundTag getUpdateTag(HolderLookup.Provider registries) {
CompoundTag tag = new CompoundTag();
tag.putBoolean("remesh", syncCausesRemesh);
syncCausesRemesh = false;
for (IComponent component : icomponents) {
for (IComponent component : components) {
component.writeClientNbt(tag, registries);
}
return tag;
}

@Override
public final void saveAdditional(CompoundTag tag, HolderLookup.Provider registries) {
for (IComponent component : icomponents) {
for (IComponent component : components) {
component.writeNbt(tag, registries);
}
}
Expand All @@ -258,12 +213,12 @@ public final void loadAdditional(CompoundTag tag, HolderLookup.Provider registri

public final void load(CompoundTag tag, HolderLookup.Provider registries, boolean isUpgradingMachine) {
if (!tag.contains("remesh")) {
for (IComponent component : icomponents) {
for (IComponent component : components) {
component.readNbt(tag, registries, isUpgradingMachine);
}
} else {
boolean forceChunkRemesh = tag.getBoolean("remesh");
for (IComponent component : icomponents) {
for (IComponent component : components) {
component.readClientNbt(tag, registries);
}
if (forceChunkRemesh) {
Expand Down Expand Up @@ -300,7 +255,7 @@ public static void registerFluidApi(BlockEntityType<?> bet) {

public List<ItemStack> dropExtra() {
List<ItemStack> drops = new ArrayList<>();
forComponentType(DropableComponent.class, u -> drops.add(u.getDrop()));
components.forType(DropableComponent.class, u -> drops.add(u.getDrop()));
return drops;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ protected ItemInteractionResult useItemOn(Player player, InteractionHand hand, D
result = LubricantHelper.onUse(this.crafter, player, hand);
}
if (!result.consumesAction()) {
result = mapComponentOrDefault(UpgradeComponent.class, upgrade -> upgrade.onUse(this, player, hand), result);
result = components.mapOrDefault(UpgradeComponent.class, upgrade -> upgrade.onUse(this, player, hand), result);
}
if (!result.consumesAction()) {
result = redstoneControl.onUse(this, player, hand);
}
if (!result.consumesAction()) {
result = mapComponentOrDefault(OverdriveComponent.class, overdrive -> overdrive.onUse(this, player, hand), result);
result = components.mapOrDefault(OverdriveComponent.class, overdrive -> overdrive.onUse(this, player, hand), result);
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import aztech.modern_industrialization.inventory.ConfigurableScreenHandler;
import aztech.modern_industrialization.inventory.MIInventory;
import aztech.modern_industrialization.inventory.SlotGroup;
import java.util.List;
import aztech.modern_industrialization.machines.ComponentStorage;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.inventory.Slot;
Expand All @@ -38,7 +38,7 @@ public abstract class MachineMenuCommon extends ConfigurableScreenHandler implem
public final MachineGuiParameters guiParams;

MachineMenuCommon(int syncId, Inventory playerInventory, MIInventory inventory, MachineGuiParameters guiParams,
List<? extends GuiComponent.Common> guiComponents) {
ComponentStorage<? extends GuiComponent.Common> guiComponents) {
super(MIRegistries.MACHINE_MENU.get(), syncId, playerInventory, inventory);
this.guiParams = guiParams;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ public MachineMenuServer(int syncId, Inventory playerInventory, MachineBlockEnti
@Override
public void broadcastChanges() {
super.broadcastChanges();
for (int i = 0; i < blockEntity.guiComponents.size(); ++i) {
GuiComponent.Server component = blockEntity.guiComponents.get(i);
blockEntity.guiComponents.forEachIndexed((i, component) -> {
if (component.needsSync(trackedData.get(i))) {
var buf = new RegistryFriendlyByteBuf(Unpooled.buffer(), blockEntity.getLevel().registryAccess());
component.writeCurrentData(buf);
Expand All @@ -61,7 +60,7 @@ public void broadcastChanges() {
trackedData.set(i, component.copyData());
buf.release();
}
}
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void handle(Context ctx) {

AbstractContainerMenu menu = ctx.getPlayer().containerMenu;
if (menu.containerId == syncId && menu instanceof MachineMenuServer machineMenu) {
ShapeSelection.Server shapeSelection = machineMenu.blockEntity.getComponent(GuiComponents.SHAPE_SELECTION);
ShapeSelection.Server shapeSelection = machineMenu.blockEntity.guiComponents.get(GuiComponents.SHAPE_SELECTION);
shapeSelection.behavior.handleClick(shapeLine, clickedLeftButton ? -1 : +1);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void handle(Context ctx) {
AbstractContainerMenu sh = ctx.getPlayer().containerMenu;
if (sh.containerId == containedId && sh instanceof MachineMenuServer screenHandler) {
// Check that locking the slots is allowed in the first place
ReiSlotLocking.Server slotLocking = screenHandler.blockEntity.getComponent(GuiComponents.REI_SLOT_LOCKING);
ReiSlotLocking.Server slotLocking = screenHandler.blockEntity.guiComponents.get(GuiComponents.REI_SLOT_LOCKING);
if (!slotLocking.allowLocking.get())
return;

Expand Down
Loading
Loading