-
Notifications
You must be signed in to change notification settings - Fork 1.2k
CC:Tweaked Peripheral for Factory Gauge #9749
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
Open
sansiquay
wants to merge
2
commits into
Creators-of-Create:mc1.20.1/dev
Choose a base branch
from
sansiquay:feature/factory-gauge-peripheral-stable
base: mc1.20.1/dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
278 changes: 278 additions & 0 deletions
278
...mibubi/create/compat/computercraft/implementation/peripherals/FactoryGaugePeripheral.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,278 @@ | ||
| package com.simibubi.create.compat.computercraft.implementation.peripherals; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
|
|
||
| import org.jetbrains.annotations.NotNull; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| import com.simibubi.create.content.logistics.factoryBoard.FactoryPanelBehaviour; | ||
| import com.simibubi.create.content.logistics.factoryBoard.FactoryPanelBlock.PanelSlot; | ||
| import com.simibubi.create.content.logistics.factoryBoard.FactoryPanelBlockEntity; | ||
| import com.simibubi.create.foundation.blockEntity.behaviour.ValueSettingsBehaviour.ValueSettings; | ||
|
|
||
| import dan200.computercraft.api.detail.VanillaDetailRegistries; | ||
| import dan200.computercraft.api.lua.IArguments; | ||
| import dan200.computercraft.api.lua.LuaException; | ||
| import dan200.computercraft.api.lua.LuaFunction; | ||
| import net.minecraft.core.registries.BuiltInRegistries; | ||
| import net.minecraft.resources.ResourceLocation; | ||
| import net.minecraft.world.item.Item; | ||
| import net.minecraft.world.item.Items; | ||
| import net.minecraft.world.item.ItemStack; | ||
|
|
||
| public class FactoryGaugePeripheral extends SyncedPeripheral<FactoryPanelBlockEntity> { | ||
|
|
||
| public FactoryGaugePeripheral(FactoryPanelBlockEntity blockEntity) { | ||
| super(blockEntity); | ||
| } | ||
|
|
||
| @NotNull | ||
| @Override | ||
| public String getType() { | ||
| return "Create_FactoryGauge"; | ||
| } | ||
|
|
||
| // === PANEL SELECTION === | ||
|
|
||
| @Nullable | ||
| private FactoryPanelBehaviour getPanelBehaviour(Optional<String> slotName) throws LuaException { | ||
| PanelSlot slot = PanelSlot.TOP_LEFT; // default | ||
| if (slotName.isPresent()) { | ||
| try { | ||
| slot = PanelSlot.valueOf(slotName.get().toUpperCase()); | ||
| } catch (IllegalArgumentException e) { | ||
| throw new LuaException("Invalid slot name. Use: TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT"); | ||
| } | ||
| } else { | ||
| // Find first active panel | ||
| for (FactoryPanelBehaviour panel : blockEntity.panels.values()) { | ||
| if (panel.isActive()) { | ||
| return panel; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| FactoryPanelBehaviour panel = blockEntity.panels.get(slot); | ||
| if (panel == null || !panel.isActive()) { | ||
| return null; | ||
| } | ||
| return panel; | ||
| } | ||
|
|
||
| // === READ METHODS === | ||
|
|
||
| @LuaFunction(mainThread = true) | ||
| public final List<String> getActivePanels() { | ||
| List<String> result = new ArrayList<>(); | ||
| for (Map.Entry<PanelSlot, FactoryPanelBehaviour> entry : blockEntity.panels.entrySet()) { | ||
| if (entry.getValue().isActive()) { | ||
| result.add(entry.getKey().name()); | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| @LuaFunction(mainThread = true) | ||
| public final int getActivePanelCount() { | ||
| return blockEntity.activePanels(); | ||
| } | ||
|
|
||
| @LuaFunction(mainThread = true) | ||
| public final @Nullable Map<String, Object> getFilter(Optional<String> slot) throws LuaException { | ||
| FactoryPanelBehaviour panel = getPanelBehaviour(slot); | ||
| if (panel == null) return null; | ||
|
|
||
| ItemStack filter = panel.getFilter(); | ||
| if (filter.isEmpty()) return null; | ||
|
|
||
| Map<String, Object> result = new HashMap<>(VanillaDetailRegistries.ITEM_STACK.getBasicDetails(filter)); | ||
| return result; | ||
| } | ||
|
|
||
| @LuaFunction(mainThread = true) | ||
| public final @Nullable Map<String, Object> getThreshold(Optional<String> slot) throws LuaException { | ||
| FactoryPanelBehaviour panel = getPanelBehaviour(slot); | ||
| if (panel == null) return null; | ||
|
|
||
| Map<String, Object> result = new HashMap<>(); | ||
| result.put("count", panel.getAmount()); | ||
| result.put("mode", panel.upTo ? "items" : "stacks"); | ||
| return result; | ||
| } | ||
|
|
||
| @LuaFunction(mainThread = true) | ||
| public final boolean isThresholdMet(Optional<String> slot) throws LuaException { | ||
| FactoryPanelBehaviour panel = getPanelBehaviour(slot); | ||
| if (panel == null) return false; | ||
| return panel.satisfied; | ||
| } | ||
|
|
||
| @LuaFunction(mainThread = true) | ||
| public final boolean isPromisedMet(Optional<String> slot) throws LuaException { | ||
| FactoryPanelBehaviour panel = getPanelBehaviour(slot); | ||
| if (panel == null) return false; | ||
| return panel.promisedSatisfied; | ||
| } | ||
|
|
||
| @LuaFunction(mainThread = true) | ||
| public final @Nullable Map<String, Object> getStatus(Optional<String> slot) throws LuaException { | ||
| FactoryPanelBehaviour panel = getPanelBehaviour(slot); | ||
| if (panel == null) return null; | ||
|
|
||
| Map<String, Object> result = new HashMap<>(); | ||
| result.put("active", panel.isActive()); | ||
| result.put("satisfied", panel.satisfied); | ||
| result.put("promisedSatisfied", panel.promisedSatisfied); | ||
| result.put("waitingForNetwork", panel.waitingForNetwork); | ||
| result.put("redstonePowered", panel.redstonePowered); | ||
| result.put("levelInStorage", panel.getLevelInStorage()); | ||
| result.put("promised", panel.getPromised()); | ||
| result.put("address", panel.recipeAddress); | ||
| result.put("recipeOutput", panel.recipeOutput); | ||
| result.put("isRestocker", blockEntity.restocker); | ||
| return result; | ||
| } | ||
|
|
||
| @LuaFunction(mainThread = true) | ||
| public final int getLevelInStorage(Optional<String> slot) throws LuaException { | ||
| FactoryPanelBehaviour panel = getPanelBehaviour(slot); | ||
| if (panel == null) return 0; | ||
| return panel.getLevelInStorage(); | ||
| } | ||
|
|
||
| @LuaFunction(mainThread = true) | ||
| public final int getPromised(Optional<String> slot) throws LuaException { | ||
| FactoryPanelBehaviour panel = getPanelBehaviour(slot); | ||
| if (panel == null) return 0; | ||
| return panel.getPromised(); | ||
| } | ||
|
|
||
| @LuaFunction(mainThread = true) | ||
| public final @Nullable String getAddress(Optional<String> slot) throws LuaException { | ||
| FactoryPanelBehaviour panel = getPanelBehaviour(slot); | ||
| if (panel == null) return null; | ||
| return panel.recipeAddress; | ||
| } | ||
|
|
||
| @LuaFunction(mainThread = true) | ||
| public final @Nullable String getNetwork(Optional<String> slot) throws LuaException { | ||
| FactoryPanelBehaviour panel = getPanelBehaviour(slot); | ||
| if (panel == null) return null; | ||
| return panel.network != null ? panel.network.toString() : null; | ||
| } | ||
|
|
||
| // === WRITE METHODS === | ||
|
|
||
| @LuaFunction(mainThread = true) | ||
| public final boolean setFilter(IArguments args) throws LuaException { | ||
| String slotName = args.optString(1, null); | ||
| Optional<String> slot = Optional.ofNullable(slotName); | ||
| FactoryPanelBehaviour panel = getPanelBehaviour(slot); | ||
| if (panel == null) { | ||
| throw new LuaException("No active panel found"); | ||
| } | ||
|
|
||
| Object arg = args.get(0); | ||
| ItemStack stack; | ||
|
|
||
| if (arg == null) { | ||
| stack = ItemStack.EMPTY; | ||
| } else if (arg instanceof String itemId) { | ||
| stack = parseItemString(itemId); | ||
| } else if (arg instanceof Map<?, ?> itemTable) { | ||
| stack = parseItemTable(itemTable); | ||
| } else { | ||
| throw new LuaException("Expected item string, table, or nil"); | ||
| } | ||
|
|
||
| return panel.setFilter(stack); | ||
| } | ||
|
|
||
| @LuaFunction(mainThread = true) | ||
| public final void setThreshold(IArguments args) throws LuaException { | ||
| int count = args.getInt(0); | ||
| String mode = args.optString(1, "items"); | ||
| String slotName = args.optString(2, null); | ||
|
|
||
| Optional<String> slot = Optional.ofNullable(slotName); | ||
| FactoryPanelBehaviour panel = getPanelBehaviour(slot); | ||
| if (panel == null) { | ||
| throw new LuaException("No active panel found"); | ||
| } | ||
|
|
||
| if (count < 0 || count > 100) { | ||
| throw new LuaException("Threshold count must be between 0 and 100"); | ||
| } | ||
|
|
||
| if (!mode.equalsIgnoreCase("items") && !mode.equalsIgnoreCase("stacks")) { | ||
| throw new LuaException("Mode must be 'items' or 'stacks'"); | ||
| } | ||
|
|
||
| boolean upTo = mode.equalsIgnoreCase("items"); | ||
| ValueSettings settings = new ValueSettings(upTo ? 0 : 1, count); | ||
| panel.setValueSettings(null, settings, false); | ||
| } | ||
|
|
||
| @LuaFunction(mainThread = true) | ||
| public final void setAddress(IArguments args) throws LuaException { | ||
| String address = args.getString(0); | ||
| String slotName = args.optString(1, null); | ||
|
|
||
| Optional<String> slot = Optional.ofNullable(slotName); | ||
| FactoryPanelBehaviour panel = getPanelBehaviour(slot); | ||
| if (panel == null) { | ||
| throw new LuaException("No active panel found"); | ||
| } | ||
|
|
||
| panel.recipeAddress = address; | ||
| panel.blockEntity.notifyUpdate(); | ||
| } | ||
|
|
||
| @LuaFunction(mainThread = true) | ||
| public final void clearPromises(Optional<String> slot) throws LuaException { | ||
| FactoryPanelBehaviour panel = getPanelBehaviour(slot); | ||
| if (panel == null) { | ||
| throw new LuaException("No active panel found"); | ||
| } | ||
|
|
||
| panel.forceClearPromises = true; | ||
| } | ||
|
|
||
| // === HELPER METHODS === | ||
|
|
||
| private ItemStack parseItemString(String itemId) throws LuaException { | ||
| String fullId = itemId.contains(":") ? itemId : "minecraft:" + itemId; | ||
| ResourceLocation id = ResourceLocation.tryParse(fullId); | ||
| if (id == null) { | ||
| throw new LuaException("Invalid item ID format: " + itemId); | ||
| } | ||
|
|
||
| Item item = BuiltInRegistries.ITEM.get(id); | ||
| if (item == Items.AIR) { | ||
| throw new LuaException("Unknown item: " + itemId); | ||
| } | ||
|
|
||
| return new ItemStack(item); | ||
| } | ||
|
|
||
| private ItemStack parseItemTable(Map<?, ?> table) throws LuaException { | ||
| Object nameObj = table.get("name"); | ||
| if (!(nameObj instanceof String name)) { | ||
| throw new LuaException("Item table must have a 'name' field"); | ||
| } | ||
|
|
||
| ItemStack stack = parseItemString(name); | ||
|
|
||
| Object countObj = table.get("count"); | ||
| if (countObj instanceof Number count) { | ||
| stack.setCount(count.intValue()); | ||
| } | ||
|
|
||
| return stack; | ||
| } | ||
| } | ||
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This pr is awesome and this is a nitpick while nosy-ing around, but since the else statement always return at this point, why not keep this logic in the first if statement and remove the else, just keeps stuff together