Skip to content

Commit

Permalink
implement lithium api's block entity optimisations
Browse files Browse the repository at this point in the history
  • Loading branch information
ix0rai committed Aug 24, 2022
1 parent ec1862a commit cb14422
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 21 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ dependencies {
modImplementation("dev.emi:emi:${emi_version}") { transitive = false }

// lithium - block pathing compat
shadow(files("libs/lithium-fabric-mc1.19.1-0.9.0-SNAPSHOT-api-dev.jar"))
shadow(files("libs/lithium-fabric-${project.lithium_api_version}-api-dev.jar"))

// dependencies for config
shadow("com.electronwill.night-config:core:${project.night_config_version}")
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mod_menu_version=4.0.5
fabric_version=0.59.0+1.19.2
spruceui_version=4.0.0+1.19
emi_version=0.3.3+1.19
lithium_version=mc1.19.2-0.8.3
lithium_api_version=mc1.19.1-0.9.0-SNAPSHOT

# other dependencies
night_config_version=3.6.5
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.jetbrains.annotations.Nullable;

public class BerryHarvesterBlockEntity extends BlockEntity implements ImplementedInventory, SidedInventory, NamedScreenHandlerFactory {
private final DefaultedList<ItemStack> inventory = DefaultedList.ofSize(9, ItemStack.EMPTY);
private DefaultedList<ItemStack> inventory = DefaultedList.ofSize(9, ItemStack.EMPTY);
private int tickCounter;
private static final int ATTEMPT_HARVEST_ON = 100;

Expand Down Expand Up @@ -120,9 +120,9 @@ public boolean isInventoryFull() {
*/
public void insert(ItemStack stack) {
// find an open slot and insert items
for (int i = 0; i < this.getItems().size(); i++) {
for (int i = 0; i < this.getInventoryLithium().size(); i++) {
// get items currently in slot
ItemStack slot = this.getItems().get(i);
ItemStack slot = this.getInventoryLithium().get(i);
int maxAmount = slot.getMaxCount();

if ((slot.isEmpty() || slot.getItem().equals(stack.getItem())) && slot.getCount() <= maxAmount) {
Expand All @@ -142,13 +142,18 @@ public void insert(ItemStack stack) {
}

@Override
public DefaultedList<ItemStack> getItems() {
public DefaultedList<ItemStack> getInventoryLithium() {
return inventory;
}

@Override
public void setInventoryLithium(DefaultedList<ItemStack> inventory) {
this.inventory = inventory;
}

@Override
public int[] getAvailableSlots(Direction side) {
int[] slots = new int[getItems().size()];
int[] slots = new int[getInventoryLithium().size()];
for (int i = 0; i < slots.length; i++) {
slots[i] = i;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,36 @@
package io.ix0rai.bodacious_berries.block.entity;

import me.jellysquid.mods.lithium.api.inventory.LithiumInventory;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.Inventories;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.util.collection.DefaultedList;

/**
* a simple {@code Inventory} implementation with only default methods + an item list getter.
* @author juuz
* a simple implementation of {@link LithiumInventory} with a few default methods
* uses names like {@link #getInventoryLithium()} for conciseness as they accomplish the same goals as the non-lithium methods
* @author juuz, ix0rai
*/
public interface ImplementedInventory extends Inventory {
public interface ImplementedInventory extends LithiumInventory {

/**
* Retrieves the item list of this inventory.
* Must return the same instance every time it's called.
*/
DefaultedList<ItemStack> getItems();
DefaultedList<ItemStack> getInventoryLithium();

/**
* sets the contents of the inventory to the given list
*/
void setInventoryLithium(DefaultedList<ItemStack> inventory);

/**
* Returns the inventory size.
*/
@Override
default int size() {
return getItems().size();
return getInventoryLithium().size();
}

/**
Expand All @@ -46,18 +53,18 @@ default boolean isEmpty() {
*/
@Override
default ItemStack getStack(int slot) {
return getItems().get(slot);
return getInventoryLithium().get(slot);
}

/**
* Removes items from an inventory slot.
* @param slot The slot to remove from.
* @param count How many items to remove. If there are less items in the slot than what are requested,
* @param count How many items to remove. If there are fewer items in the slot than what are requested,
* takes all items in that slot.
*/
@Override
default ItemStack removeStack(int slot, int count) {
ItemStack result = Inventories.splitStack(getItems(), slot, count);
ItemStack result = Inventories.splitStack(getInventoryLithium(), slot, count);
if (!result.isEmpty()) {
markDirty();
}
Expand All @@ -70,7 +77,7 @@ default ItemStack removeStack(int slot, int count) {
*/
@Override
default ItemStack removeStack(int slot) {
return Inventories.removeStack(getItems(), slot);
return Inventories.removeStack(getInventoryLithium(), slot);
}

/**
Expand All @@ -82,7 +89,7 @@ default ItemStack removeStack(int slot) {
*/
@Override
default void setStack(int slot, ItemStack stack) {
getItems().set(slot, stack);
getInventoryLithium().set(slot, stack);
if (stack.getCount() > getMaxCountPerStack()) {
stack.setCount(getMaxCountPerStack());
}
Expand All @@ -93,7 +100,7 @@ default void setStack(int slot, ItemStack stack) {
*/
@Override
default void clear() {
getItems().clear();
getInventoryLithium().clear();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

public class JuicerBlockEntity extends BlockEntity implements ImplementedInventory, SidedInventory, NamedScreenHandlerFactory {
public static final int TOTAL_BREW_TIME = 300;
private final DefaultedList<ItemStack> inventory = DefaultedList.ofSize(6, ItemStack.EMPTY);
private DefaultedList<ItemStack> inventory = DefaultedList.ofSize(6, ItemStack.EMPTY);
private int brewTime = 0;
private boolean makingDubiousJuice = false;
private boolean makingBerryBlend = false;
Expand Down Expand Up @@ -136,7 +136,7 @@ public static void tick(World world, BlockPos pos, BlockState state, JuicerBlock
stopJuicer(world, pos, state, juicer);
}
} else if (juicer.hasValidReceptacle()) {
DefaultedList<ItemStack> inventory = juicer.getItems();
DefaultedList<ItemStack> inventory = juicer.getInventoryLithium();

if (recipe.isPresent()) {
juicer.makingBerryBlend = !recipe.get().ingredientsMatch(inventory.get(3));
Expand Down Expand Up @@ -176,10 +176,15 @@ public boolean hasValidReceptacle() {
}

@Override
public DefaultedList<ItemStack> getItems() {
public DefaultedList<ItemStack> getInventoryLithium() {
return inventory;
}

@Override
public void setInventoryLithium(DefaultedList<ItemStack> inventory) {
this.inventory = inventory;
}

@Override
public ScreenHandler createMenu(int syncId, PlayerInventory playerInventory, PlayerEntity player) {
return new JuicerScreenHandler(syncId, playerInventory, this, propertyDelegate);
Expand Down

0 comments on commit cb14422

Please sign in to comment.