Skip to content

Commit

Permalink
feat: add transfer handler for storage actuator for EMI
Browse files Browse the repository at this point in the history
Closes #1147
  • Loading branch information
klikli-dev committed Aug 18, 2024
1 parent 4c03a6c commit 98e2ec5
Show file tree
Hide file tree
Showing 14 changed files with 950 additions and 23 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ dependencies {

//theurgy
compileOnly ("com.klikli_dev:theurgy-${minecraft_version}-neoforge:${theurgy_version}") {transitive=false}
runtimeOnly ("com.klikli_dev:theurgy-${minecraft_version}-neoforge:${theurgy_version}") {transitive=false}
// runtimeOnly ("com.klikli_dev:theurgy-${minecraft_version}-neoforge:${theurgy_version}") {transitive=false}

// emi
compileOnly ("dev.emi:emi-neoforge:${emi_version}:api") {transitive=false}
Expand Down
3 changes: 3 additions & 0 deletions src/generated/resources/assets/occultism/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -1326,6 +1326,9 @@
"jei.occultism.error.invalid_type": "Invalid recipe type.",
"jei.occultism.error.missing_id": "Cannot identify recipe.",
"jei.occultism.error.pentacle_not_loaded": "The pentacle could not be loaded.",
"jei.occultism.error.recipe_items_missing": "Missing items will be skipped.",
"jei.occultism.error.recipe_move_items": "Move items",
"jei.occultism.error.recipe_no_items": "No compatible items fround for recipe.",

This comment has been minimized.

Copy link
@Heimdallr-1

Heimdallr-1 Aug 19, 2024

fround?

"jei.occultism.error.recipe_too_large": "Recipe larger than 3x3.",
"jei.occultism.ingredient.datura.description": "Can be used to heal all spirits and familiars summoned by Occultism Rituals. Simply right-click the entity to heal it by one heart",
"jei.occultism.ingredient.iesnium_ore.description": "Found in the nether. Only visible while the status §6Third§r §6Eye§r is active. See §6Dictionary§r §6of§r §6Spirits§r for more information.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package com.klikli_dev.occultism.api.client.gui;

import com.klikli_dev.occultism.api.common.data.MachineReference;
import com.klikli_dev.occultism.client.gui.storage.ClientStorageCache;
import net.minecraft.world.item.ItemStack;

import java.util.List;
Expand All @@ -31,6 +32,8 @@ public interface IStorageControllerGui {
//region Getter / Setter
void setStacks(List<ItemStack> stacks);

ClientStorageCache getClientStorageCache();

void setUsedStorageSize(int usedItemTypes, long usedTotalItemCount);

void setMaxStorageSize(int maxItemTypes, long maxTotalItemCount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import com.klikli_dev.occultism.api.common.blockentity.IStorageController;
import com.klikli_dev.occultism.api.common.data.GlobalBlockPos;
import com.klikli_dev.occultism.client.gui.storage.ClientStorageCache;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.inventory.CraftingContainer;

Expand Down Expand Up @@ -57,4 +58,7 @@ public interface IStorageControllerContainer {
*/
void updateOrderSlot(boolean force);

void setClientStorageCache(ClientStorageCache cache);

ClientStorageCache getClientStorageCache();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* MIT License
*
* Copyright 2024 klikli-dev
*
* 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 com.klikli_dev.occultism.client.gui.storage;

import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class ClientStorageCache {
private final Int2ObjectOpenHashMap<List<ItemStack>> entriesByItemId = new Int2ObjectOpenHashMap<>();
private List<ItemStack> stacks = new ArrayList<>();
private boolean entriesByItemIdNeedsUpdate = true;

public List<ItemStack> stacks() {
return this.stacks;
}

public void update(List<ItemStack> stacks) {
this.stacks = stacks;

this.entriesByItemIdNeedsUpdate = true;
}

public List<ItemStack> getByIngredient(Ingredient ingredient) {
var entries = new ArrayList<ItemStack>();
for (int i = 0; i < ingredient.getStackingIds().size(); i++) {
var itemId = ingredient.getStackingIds().getInt(i);
for (var entry : this.getByItemId(itemId)) {
if (ingredient.test(entry)) {
entries.add(entry);
}
}
}
return entries;
}

private Collection<ItemStack> getByItemId(int itemId) {
if (this.entriesByItemIdNeedsUpdate) {
this.rebuildItemIdToEntries();
this.entriesByItemIdNeedsUpdate = false;
}
return this.entriesByItemId.getOrDefault(itemId, List.of());
}

private void rebuildItemIdToEntries() {
this.entriesByItemId.clear();
for (var entry : this.stacks()) {
var itemId = BuiltInRegistries.ITEM.getId(entry.getItem());
var currentList = this.entriesByItemId.get(itemId);
if (currentList == null) {
// For many items without NBT, this list will only ever have one entry
this.entriesByItemId.put(itemId, List.of(entry));
} else if (currentList.size() == 1) {
// Convert the list from an immutable single-entry list to a mutable normal arraylist
var mutableList = new ArrayList<ItemStack>(10);
mutableList.addAll(currentList);
mutableList.add(entry);
this.entriesByItemId.put(itemId, mutableList);
} else {
// If it had more than 1 item, it must have been mutable already
currentList.add(entry);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public abstract class StorageControllerGuiBase<T extends StorageControllerContai
protected static final ResourceLocation BUTTONS = ResourceLocation.fromNamespaceAndPath(Occultism.MODID, "textures/gui/buttons.png");
protected static final String TRANSLATION_KEY_BASE = "gui." + Occultism.MODID + ".storage_controller";
public int lastStacksCount;
public List<ItemStack> stacks;
public ClientStorageCache clientStorageCache;
public List<MachineReference> linkedMachines;
public IStorageControllerContainer storageControllerContainer;
public StorageControllerGuiMode guiMode = StorageControllerGuiMode.INVENTORY;
Expand Down Expand Up @@ -132,7 +132,9 @@ public StorageControllerGuiBase(T container, Inventory playerInventory, Componen
this.currentPage = 1;
this.totalPages = 1;

this.stacks = new ArrayList<>();
this.clientStorageCache = new ClientStorageCache();
this.storageControllerContainer.setClientStorageCache(this.clientStorageCache);

this.linkedMachines = new ArrayList<>();

this.lastClick = System.currentTimeMillis();
Expand Down Expand Up @@ -208,10 +210,15 @@ public void renderToolTip(GuiGraphics guiGraphics, MachineReference machine, int

@Override
public void setStacks(List<ItemStack> stacks) {
this.stacks = stacks;
this.clientStorageCache.update(stacks);
this.resetDisplayCaches();
}

@Override
public ClientStorageCache getClientStorageCache() {
return this.clientStorageCache;
}

@Override
public void setMaxStorageSize(int maxItemTypes, long maxTotalItemCount) {
this.maxItemTypes = maxItemTypes;
Expand Down Expand Up @@ -515,7 +522,7 @@ public void initButtons() {
});
this.addRenderableWidget(this.sortDirectionButton);

if (OccultismEmiIntegration.get().isLoaded() || OccultismJeiIntegration.get().isLoaded()){
if (OccultismEmiIntegration.get().isLoaded() || OccultismJeiIntegration.get().isLoaded()) {
int jeiSyncOffset = 140 + (JeiSettings.isJeiSearchSynced() ? 0 : 1) * 28;
this.jeiSyncButton = new SizedImageButton(
this.leftPos + clearTextButtonLeft + controlButtonSize + 3 + controlButtonSize + 3 + controlButtonSize +
Expand Down Expand Up @@ -582,8 +589,8 @@ protected void drawItems(GuiGraphics guiGraphics, float partialTicks, int mouseX
var changedStacksToDisplay = this.lastCachedStacksToDisplayCount != stacksToDisplay.size();
this.lastCachedStacksToDisplayCount = stacksToDisplay.size();

var changedStacks = this.lastStacksCount != this.stacks.size();
this.lastStacksCount = this.stacks.size();
var changedStacks = this.lastStacksCount != this.getClientStorageCache().stacks().size();
this.lastStacksCount = this.getClientStorageCache().stacks().size();

if (changedPage || changedStacksToDisplay || changedStacks) {
this.sortItemStacks(stacksToDisplay);
Expand Down Expand Up @@ -790,7 +797,7 @@ protected List<ItemStack> applySearchToItems() {
return this.cachedStacksToDisplay;

List<ItemStack> stacksToDisplay = new ArrayList<>();
for (ItemStack stack : this.stacks) {
for (ItemStack stack : this.getClientStorageCache().stacks()) {
if (this.itemMatchesSearch(stack))
stacksToDisplay.add(stack);
}
Expand All @@ -800,7 +807,7 @@ protected List<ItemStack> applySearchToItems() {

return stacksToDisplay;
}
return new ArrayList<>(this.stacks);
return new ArrayList<>(this.getClientStorageCache().stacks());
}

protected List<MachineReference> applySearchToMachines() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ public StorageControllerContainer(int id, Inventory playerInventory,

this.setupCraftingOutput(); //output is slot 0

this.setupCraftingGrid();
this.setupOrderInventorySlot();
this.setupPlayerInventorySlots();
this.setupPlayerHotbar();
this.setupCraftingGrid(); //crafting grid is slots 1-9
this.setupOrderInventorySlot(); //order slot is slot 10
this.setupPlayerInventorySlots(); //player inventory is slots 11-47
this.setupPlayerHotbar(); //player hotbar is slots 48-56

this.slotsChanged(this.matrix);
}
Expand Down
Loading

0 comments on commit 98e2ec5

Please sign in to comment.