-
Notifications
You must be signed in to change notification settings - Fork 95
Make machine component api more consistent and accessible #938
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c7fe6ae
Make machine component api more consistent and accessible across both…
Swedz 62755b4
Make ComponentStorage iterable
Swedz 8a8b358
Rename components variable and make them public instead of using getters
Swedz 99c0cbd
Revert changes to MachineScreenPredicateTest
Swedz d28b26c
Dont use NotNull in ComponentStorage iterator method
Swedz bce134c
Unncessary var use removed (accident, whoops)
Swedz 8fb1f80
Use null instead of optional
Swedz 5eab0bb
Rename tryGet to getAll
Swedz 40e056d
Remove findOrDefault
Swedz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
src/main/java/aztech/modern_industrialization/machines/ComponentStorage.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| /* | ||
| * 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.Optional; | ||
| import java.util.function.BiConsumer; | ||
| import java.util.function.Consumer; | ||
| import java.util.function.Function; | ||
| import net.minecraft.resources.ResourceLocation; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| public sealed class ComponentStorage<C> implements Iterable<C> permits ComponentStorage.GuiServer, ComponentStorage.Server { | ||
| protected final List<C> components = new ArrayList<>(); | ||
|
|
||
| @NotNull | ||
Swedz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| @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)); | ||
| } | ||
| } | ||
|
|
||
| public final <T> Optional<T> get(Class<T> clazz) { | ||
Swedz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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) { | ||
Swedz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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> { | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.