-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make the compact mob farm store xp nuggets from create (if it's prese…
…nt), make it apply mending to the sword item, if it has mending and rewrite the whole inventory as a `SlottedStorage`. I didn't want to merge these two things together in one commit, but I forgot to commit the xp stuff when I was done with it, and I'm too lazy to go through all the changes :D No one's gonna see this anyway
- Loading branch information
1 parent
28d3df7
commit b01ef59
Showing
8 changed files
with
200 additions
and
77 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
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
8 changes: 8 additions & 0 deletions
8
src/main/java/top/offsetmonkey538/compactmobfarms/accessor/LivingEntityAccessor.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,8 @@ | ||
package top.offsetmonkey538.compactmobfarms.accessor; | ||
|
||
import java.util.function.Consumer; | ||
|
||
public interface LivingEntityAccessor { | ||
|
||
void compact_mob_farms$setXpDropMethod(Consumer<Integer> dropMethod); | ||
} |
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
166 changes: 104 additions & 62 deletions
166
src/main/java/top/offsetmonkey538/compactmobfarms/inventory/CompactMobFarmInventory.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 |
---|---|---|
@@ -1,100 +1,142 @@ | ||
package top.offsetmonkey538.compactmobfarms.inventory; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import net.minecraft.entity.player.PlayerEntity; | ||
import net.minecraft.inventory.Inventories; | ||
import net.minecraft.inventory.Inventory; | ||
import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant; | ||
import net.fabricmc.fabric.api.transfer.v1.item.base.SingleStackStorage; | ||
import net.fabricmc.fabric.api.transfer.v1.storage.SlottedStorage; | ||
import net.fabricmc.fabric.api.transfer.v1.storage.StorageView; | ||
import net.fabricmc.fabric.api.transfer.v1.storage.base.SingleSlotStorage; | ||
import net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.nbt.NbtCompound; | ||
import net.minecraft.nbt.NbtList; | ||
|
||
public interface CompactMobFarmInventory extends Inventory { | ||
|
||
List<ItemStack> getItems(); | ||
@SuppressWarnings("UnstableApiUsage") | ||
public class CompactMobFarmInventory implements SlottedStorage<ItemVariant> { | ||
private final List<Slot> slots = new ArrayList<>(); | ||
|
||
@Override | ||
default int size() { | ||
return getItems().size(); | ||
public int getSlotCount() { | ||
return slots.size(); | ||
} | ||
|
||
@Override | ||
default boolean isEmpty() { | ||
return getItems().isEmpty(); | ||
public SingleSlotStorage<ItemVariant> getSlot(int slot) { | ||
return slots.get(slot); | ||
} | ||
|
||
@Override | ||
default ItemStack getStack(int slot) { | ||
if (slot >= size()) return ItemStack.EMPTY; | ||
return getItems().get(slot); | ||
public boolean supportsInsertion() { | ||
return false; | ||
} | ||
|
||
@Override | ||
default ItemStack removeStack(int slot, int amount) { | ||
if (slot >= size()) return ItemStack.EMPTY; | ||
ItemStack removedStack; | ||
|
||
if (getStack(slot).getCount() - amount <= 0) { | ||
removedStack = getItems().remove(slot); | ||
} else { | ||
removedStack = Inventories.splitStack(getItems(), slot, amount); | ||
} | ||
public long insert(ItemVariant resource, long maxAmount, TransactionContext transaction) { | ||
if (resource.isBlank()) return 0; | ||
|
||
for (Slot slot : slots) { | ||
final ItemStack storedStack = slot.getStack(); | ||
if (!storedStack.isOf(resource.getItem()) || (resource.getNbt() != null && resource.getNbt().equals(storedStack.getNbt()))) continue; | ||
|
||
int oldCount = storedStack.getCount(); | ||
int newCount = (int) Math.min(64, oldCount + maxAmount); | ||
|
||
if (!removedStack.isEmpty()) { | ||
this.markDirty(); | ||
storedStack.setCount(newCount); | ||
slot.setStack(storedStack); | ||
|
||
return newCount - oldCount; | ||
} | ||
return removedStack; | ||
} | ||
|
||
@Override | ||
default ItemStack removeStack(int slot) { | ||
this.markDirty(); | ||
return getItems().remove(slot); | ||
} | ||
|
||
default void addStack(ItemStack stack) { | ||
if (stack.isEmpty()) return; | ||
// None of the already stored stacks match the new stack. | ||
|
||
int count = (int) Math.min(64, maxAmount); | ||
final ItemStack addedStack = resource.toStack(count); | ||
|
||
slots.add(new Slot(addedStack)); | ||
|
||
return count; | ||
} | ||
|
||
for (int i = 0; i < size(); i++) { | ||
ItemStack storedStack = getStack(i); | ||
@Override | ||
public long extract(ItemVariant resource, long maxAmount, TransactionContext transaction) { | ||
if (resource.isBlank()) return 0; | ||
|
||
if (!storedStack.isOf(stack.getItem()) || (stack.getNbt() != null && !stack.getNbt().equals(storedStack.getNbt()))) continue; | ||
for (Slot slot : slots) { | ||
final ItemStack storedStack = slot.getStack(); | ||
if (!storedStack.isOf(resource.getItem()) || (resource.getNbt() != null && resource.getNbt().equals(storedStack.getNbt()))) continue; | ||
|
||
int newCount = stack.getCount() + storedStack.getCount(); | ||
if (newCount > getMaxCountPerStack()) newCount = getMaxCountPerStack(); | ||
int oldCount = storedStack.getCount(); | ||
int newCount = (int) Math.min(0, oldCount - maxAmount); | ||
|
||
storedStack.setCount(newCount); | ||
getItems().set(i, storedStack); | ||
markDirty(); | ||
return; | ||
slot.setStack(storedStack); | ||
if (newCount == 0) slots.remove(slot); | ||
|
||
return oldCount - newCount; | ||
} | ||
|
||
getItems().add(stack); | ||
markDirty(); | ||
return 0; | ||
} | ||
|
||
@Override | ||
default void setStack(int slot, ItemStack stack) { | ||
if (slot >= size()) return; | ||
if (stack.isEmpty()) { | ||
getItems().remove(slot); | ||
return; | ||
} | ||
getItems().set(slot, stack); | ||
this.markDirty(); | ||
public Iterator<StorageView<ItemVariant>> iterator() { | ||
return new Iterator<>() { | ||
int i = 0; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return CompactMobFarmInventory.this.slots.size() > i; | ||
} | ||
|
||
@Override | ||
public StorageView<ItemVariant> next() { | ||
return CompactMobFarmInventory.this.slots.get(i++); | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
default boolean isValid(int slot, ItemStack stack) { | ||
// We don't want hoppers or anything to put items in here | ||
return false; | ||
public NbtList toNbtList() { | ||
final NbtList nbt = new NbtList(); | ||
|
||
slots.forEach((Slot slot) -> nbt.add(slot.toNbt())); | ||
|
||
return nbt; | ||
} | ||
|
||
@Override | ||
default boolean canPlayerUse(PlayerEntity player) { | ||
return true; | ||
public void fromNbt(NbtList nbtList) { | ||
nbtList.forEach(nbt -> slots.add(new Slot((NbtCompound) nbt))); | ||
} | ||
|
||
@Override | ||
default void clear() { | ||
getItems().clear(); | ||
markDirty(); | ||
private class Slot extends SingleStackStorage { | ||
private ItemStack stack; | ||
|
||
public Slot(ItemStack stack) { | ||
this.stack = stack; | ||
} | ||
public Slot(NbtCompound nbt) { | ||
this(ItemStack.fromNbt(nbt)); | ||
} | ||
|
||
@Override | ||
protected ItemStack getStack() { | ||
return stack; | ||
} | ||
|
||
@Override | ||
protected void setStack(ItemStack stack) { | ||
this.stack = stack; | ||
} | ||
|
||
@Override | ||
protected void onFinalCommit() { | ||
if (stack.isEmpty()) CompactMobFarmInventory.this.slots.remove(this); | ||
} | ||
|
||
public NbtCompound toNbt() { | ||
return stack.writeNbt(new NbtCompound()); | ||
} | ||
} | ||
} |
Oops, something went wrong.