Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -24,24 +24,24 @@
package aztech.modern_industrialization.compat.viewer.impl;

import aztech.modern_industrialization.compat.rei.machines.ReiMachineRecipes;
import aztech.modern_industrialization.machines.gui.GuiComponentClient;
import aztech.modern_industrialization.machines.gui.MachineScreen;
import aztech.modern_industrialization.machines.guicomponents.CraftingMultiblockGuiClient;
import java.util.Optional;

public class MachineScreenPredicateTest {
public static boolean test(ReiMachineRecipes.MachineScreenPredicate predicate, MachineScreen screen) {
return switch (predicate) {
case ANY -> true;
case MULTIBLOCK -> {
for (GuiComponentClient client : screen.getMenu().components) {
if (client instanceof CraftingMultiblockGuiClient cmGui) {
if (cmGui.isShapeValid) {
yield true;
case MULTIBLOCK -> screen.getMenu().components.findOrDefault(
client -> {
if (client instanceof CraftingMultiblockGuiClient cmGui) {
if (cmGui.isShapeValid) {
return Optional.of(true);
}
}
}
}
yield false;
}
return Optional.empty();
},
false);
};
}
}
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).orElse(null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ public class MachineScreen extends MIHandledScreen<MachineMenuClient> implements
public MachineScreen(MachineMenuClient handler, Inventory inventory, Component title) {
super(handler, inventory, title);

for (GuiComponentClient component : handler.components) {
renderers.add(component.createRenderer(this));
}
handler.components.forEach(component -> renderers.add(component.createRenderer(this)));

this.imageHeight = handler.guiParams.backgroundHeight;
this.imageWidth = handler.guiParams.backgroundWidth;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
* 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.List;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import net.minecraft.resources.ResourceLocation;

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

@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 forEach(Consumer<C> action) {
components.forEach(action);
}

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

public final <T> Optional<T> get(Class<T> clazz) {
for (C component : components) {
if (clazz.isInstance(component)) {
return Optional.of((T) component);
}
}
return Optional.empty();
}

public final <T> List<T> tryGet(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 = tryGet(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 = tryGet(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 final <R> R findOrDefault(Function<C, Optional<? extends R>> action, R defaultValue) {
for (C component : components) {
Optional<? extends R> result = action.apply(component);
if (result.isPresent()) {
return result.get();
}
}
return defaultValue;
}

public final <T, R> R findOrDefault(Class<T> clazz, Function<? super T, Optional<? extends R>> action, R defaultValue) {
return findOrDefault(component -> clazz.isInstance(component) ? action.apply((T) component) : Optional.empty(), defaultValue);
}

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<>();
protected final ComponentStorage.GuiServer guiComponents = new ComponentStorage.GuiServer();
protected final ComponentStorage.Server icomponents = new ComponentStorage.Server();
public final MachineGuiParameters guiParams;
/**
* Server-side only: true if the next call to sync() will trigger a remesh.
Expand All @@ -101,57 +97,24 @@ 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);
icomponents.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 ComponentStorage.GuiServer getGuiComponents() {
return guiComponents;
}

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");
}
public final ComponentStorage.Server getComponents() {
return icomponents;
}

@Override
Expand All @@ -176,10 +139,10 @@ public final void writeScreenOpeningData(RegistryFriendlyByteBuf buf) {
inv.fluidPositions.write(buf);
buf.writeInt(guiComponents.size());
// Write components
for (GuiComponent.Server component : guiComponents) {
guiComponents.forEach(component -> {
buf.writeResourceLocation(component.getId());
component.writeInitialData(buf);
}
});
// Write GUI params
guiParams.write(buf);
}
Expand Down Expand Up @@ -238,17 +201,13 @@ public CompoundTag getUpdateTag(HolderLookup.Provider registries) {
CompoundTag tag = new CompoundTag();
tag.putBoolean("remesh", syncCausesRemesh);
syncCausesRemesh = false;
for (IComponent component : icomponents) {
component.writeClientNbt(tag, registries);
}
icomponents.forEach(component -> component.writeClientNbt(tag, registries));
return tag;
}

@Override
public final void saveAdditional(CompoundTag tag, HolderLookup.Provider registries) {
for (IComponent component : icomponents) {
component.writeNbt(tag, registries);
}
icomponents.forEach(component -> component.writeNbt(tag, registries));
}

@Override
Expand All @@ -258,14 +217,10 @@ 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) {
component.readNbt(tag, registries, isUpgradingMachine);
}
icomponents.forEach(component -> component.readNbt(tag, registries, isUpgradingMachine));
} else {
boolean forceChunkRemesh = tag.getBoolean("remesh");
for (IComponent component : icomponents) {
component.readClientNbt(tag, registries);
}
icomponents.forEach(component -> component.readClientNbt(tag, registries));
if (forceChunkRemesh) {
WorldHelper.forceChunkRemesh(level, worldPosition);
requestModelDataUpdate();
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()));
icomponents.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 = icomponents.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 = icomponents.mapOrDefault(OverdriveComponent.class, overdrive -> overdrive.onUse(this, player, hand), result);
}
return result;
}
Expand Down
Loading
Loading