-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
817e610
commit 2990773
Showing
19 changed files
with
346 additions
and
84 deletions.
There are no files selected for viewing
This file contains 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
4 changes: 2 additions & 2 deletions
4
src/generated/resources/.cache/560c5d1147eb8b61b5d489f37c738096153f5af2
This file contains 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
// 1.20.6 2024-05-11T13:25:30.0411425 Languages: en_us for mod: mininggadgets | ||
e818e2aea1f0fc0b2317b97b5679d8eebc239abb assets/mininggadgets/lang/en_us.json | ||
// 1.20.6 2024-05-11T13:50:02.2019011 Languages: en_us for mod: mininggadgets | ||
de5afda06eece1f64a4df1a2f3fd029bcc802b1b assets/mininggadgets/lang/en_us.json |
This file contains 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 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 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
10 changes: 5 additions & 5 deletions
10
src/main/java/com/direwolf20/mininggadgets/common/containers/FilterContainer.java
This file contains 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 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 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
138 changes: 138 additions & 0 deletions
138
...in/java/com/direwolf20/mininggadgets/common/containers/handlers/DataComponentHandler.java
This file contains 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,138 @@ | ||
package com.direwolf20.mininggadgets.common.containers.handlers; | ||
|
||
import com.direwolf20.mininggadgets.setup.MGDataComponents; | ||
import net.minecraft.core.NonNullList; | ||
import net.minecraft.world.item.ItemStack; | ||
import net.neoforged.neoforge.items.IItemHandlerModifiable; | ||
|
||
public class DataComponentHandler implements IItemHandlerModifiable { | ||
private final ItemStack stack; | ||
private final int size; | ||
|
||
public DataComponentHandler(ItemStack stack, int size) { | ||
this.stack = stack; | ||
this.size = size; | ||
} | ||
|
||
@Override | ||
public int getSlots() { | ||
return size; | ||
} | ||
|
||
@Override | ||
public ItemStack getStackInSlot(int slot) { | ||
validateSlotIndex(slot); | ||
if (getItemList().size() < slot + 1) | ||
return ItemStack.EMPTY; | ||
return getItemList().get(slot); | ||
} | ||
|
||
@Override | ||
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) { | ||
if (stack.isEmpty()) | ||
return ItemStack.EMPTY; | ||
|
||
if (!isItemValid(slot, stack)) | ||
return stack; | ||
|
||
validateSlotIndex(slot); | ||
|
||
NonNullList<ItemStack> itemStacks = getItemList(); | ||
|
||
ItemStack existing = itemStacks.get(slot); | ||
|
||
int limit = Math.min(getSlotLimit(slot), stack.getMaxStackSize()); | ||
|
||
if (!existing.isEmpty()) { | ||
if (!ItemStack.isSameItemSameComponents(stack, existing)) | ||
return stack; | ||
|
||
limit -= existing.getCount(); | ||
} | ||
|
||
if (limit <= 0) | ||
return stack; | ||
|
||
boolean reachedLimit = stack.getCount() > limit; | ||
|
||
if (!simulate) { | ||
if (existing.isEmpty()) { | ||
itemStacks.set(slot, reachedLimit ? stack.copyWithCount(limit) : stack); | ||
} else { | ||
existing.grow(reachedLimit ? limit : stack.getCount()); | ||
} | ||
setItemList(itemStacks); | ||
} | ||
|
||
return reachedLimit ? stack.copyWithCount(stack.getCount() - limit) : ItemStack.EMPTY; | ||
} | ||
|
||
@Override | ||
public ItemStack extractItem(int slot, int amount, boolean simulate) { | ||
NonNullList<ItemStack> itemStacks = getItemList(); | ||
if (amount == 0) | ||
return ItemStack.EMPTY; | ||
|
||
validateSlotIndex(slot); | ||
|
||
ItemStack existing = itemStacks.get(slot); | ||
|
||
if (existing.isEmpty()) | ||
return ItemStack.EMPTY; | ||
|
||
int toExtract = Math.min(amount, existing.getMaxStackSize()); | ||
|
||
if (existing.getCount() <= toExtract) { | ||
if (!simulate) { | ||
itemStacks.set(slot, ItemStack.EMPTY); | ||
setItemList(itemStacks); | ||
return existing; | ||
} else { | ||
return existing.copy(); | ||
} | ||
} else { | ||
if (!simulate) { | ||
itemStacks.set(slot, existing.copyWithCount(existing.getCount() - toExtract)); | ||
setItemList(itemStacks); | ||
} | ||
|
||
return existing.copyWithCount(toExtract); | ||
} | ||
} | ||
|
||
private void validateSlotIndex(int slot) { | ||
if (slot < 0 || slot >= getSlots()) | ||
throw new RuntimeException("Slot " + slot + " not in valid range - [0," + getSlots() + ")"); | ||
} | ||
|
||
@Override | ||
public int getSlotLimit(int slot) { | ||
return 64; | ||
} | ||
|
||
@Override | ||
public boolean isItemValid(int slot, ItemStack stack) { | ||
return stack.getItem().canFitInsideContainerItems(); | ||
} | ||
|
||
@Override | ||
public void setStackInSlot(int slot, ItemStack stack) { | ||
validateSlotIndex(slot); | ||
if (!stack.isEmpty() && !isItemValid(slot, stack)) | ||
throw new RuntimeException("Invalid stack " + stack + " for slot " + slot + ")"); | ||
NonNullList<ItemStack> itemStacks = getItemList(); | ||
itemStacks.set(slot, stack); | ||
setItemList(itemStacks); | ||
} | ||
|
||
private NonNullList<ItemStack> getItemList() { | ||
DireItemContainerContents contents = this.stack.getOrDefault(MGDataComponents.ITEMSTACK_HANDLER, DireItemContainerContents.fromItems(NonNullList.withSize(size, ItemStack.EMPTY))); | ||
NonNullList<ItemStack> list = NonNullList.withSize(size, ItemStack.EMPTY); | ||
contents.copyInto(list); | ||
return list; | ||
} | ||
|
||
private void setItemList(NonNullList<ItemStack> itemStacks) { | ||
this.stack.set(MGDataComponents.ITEMSTACK_HANDLER, DireItemContainerContents.fromItems(itemStacks)); | ||
} | ||
} |
Oops, something went wrong.