Skip to content

Commit

Permalink
Merge pull request #295 from Matyrobbrt/perf-and-bugs
Browse files Browse the repository at this point in the history
Performance improvements and bugs
  • Loading branch information
Buuz135 authored Jul 19, 2024
2 parents d6274ab + 4d0b70e commit 7cbd075
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 15 deletions.
18 changes: 18 additions & 0 deletions src/main/java/com/buuz135/functionalstorage/FunctionalStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.hrznstudio.titanium.recipe.generator.IJsonFile;
import com.hrznstudio.titanium.recipe.generator.TitaniumSerializableProvider;
import com.hrznstudio.titanium.recipe.serializer.GenericSerializer;
import com.hrznstudio.titanium.util.TileUtil;
import net.minecraft.client.renderer.ItemBlockRenderTypes;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.data.recipes.FinishedRecipe;
Expand Down Expand Up @@ -62,6 +63,7 @@
import net.minecraftforge.common.crafting.CraftingHelper;
import net.minecraftforge.common.util.NonNullLazy;
import net.minecraftforge.data.event.GatherDataEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.event.level.BlockEvent;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.fml.common.Mod;
Expand Down Expand Up @@ -178,6 +180,22 @@ public FunctionalStorage() {
NBTManager.getInstance().scanTileClassForAnnotations(FluidDrawerTile.class);
NBTManager.getInstance().scanTileClassForAnnotations(SimpleCompactingDrawerTile.class);
NBTManager.getInstance().scanTileClassForAnnotations(FramedSimpleCompactingDrawerTile.class);

EventManager.forge(PlayerInteractEvent.LeftClickBlock.class)
.process(event -> {
var state = event.getLevel().getBlockState(event.getPos());
if (event.getLevel().getBlockState(event.getPos()).getBlock() instanceof Drawer drawer) {
final int hit = drawer.getHit(state, event.getLevel(), event.getPos(), event.getEntity());
if (hit != -1) {
TileUtil.getTileEntity(event.getLevel(), event.getPos(), ControllableDrawerTile.class)
.ifPresent(be -> {
be.onClicked(event.getEntity(), hit);
event.setCanceled(true);
});
}
}
})
.subscribe();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
import java.util.Optional;
import java.util.function.Consumer;

public class CompactingDrawerBlock extends RotatableBlock<CompactingDrawerTile> {
public class CompactingDrawerBlock extends RotatableBlock<CompactingDrawerTile> implements Drawer {

public static Multimap<Direction, VoxelShape> CACHED_SHAPES = MultimapBuilder.hashKeys().arrayListValues().build();

Expand Down Expand Up @@ -149,6 +149,7 @@ public void attack(BlockState state, Level worldIn, BlockPos pos, Player player)
TileUtil.getTileEntity(worldIn, pos, CompactingDrawerTile.class).ifPresent(drawerTile -> drawerTile.onClicked(player, getHit(state, worldIn, pos, player)));
}

@Override
public int getHit(BlockState state, Level worldIn, BlockPos pos, Player player) {
HitResult result = RayTraceUtils.rayTraceSimple(worldIn, player, 32, 0);
if (result instanceof BlockHitResult) {
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/buuz135/functionalstorage/block/Drawer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.buuz135.functionalstorage.block;

import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;

public interface Drawer {
int getHit(BlockState state, Level worldIn, BlockPos pos, Player player);
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
import java.util.Optional;
import java.util.function.Consumer;

public class DrawerBlock extends RotatableBlock<DrawerTile> {
public class DrawerBlock extends RotatableBlock<DrawerTile> implements Drawer {

public static HashMap<FunctionalStorage.DrawerType, Multimap<Direction, VoxelShape>> CACHED_SHAPES = new HashMap<>();

Expand Down Expand Up @@ -194,6 +194,7 @@ public void attack(BlockState state, Level worldIn, BlockPos pos, Player player)
TileUtil.getTileEntity(worldIn, pos, DrawerTile.class).ifPresent(drawerTile -> drawerTile.onClicked(player, getHit(state, worldIn, pos, player)));
}

@Override
public int getHit(BlockState state, Level worldIn, BlockPos pos, Player player) {
HitResult result = RayTraceUtils.rayTraceSimple(worldIn, player, 32, 0);
if (result instanceof BlockHitResult) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@

import static com.buuz135.functionalstorage.block.DrawerBlock.LOCKED;

public class EnderDrawerBlock extends RotatableBlock<EnderDrawerTile> {
public class EnderDrawerBlock extends RotatableBlock<EnderDrawerTile> implements Drawer {

public EnderDrawerBlock() {
super("ender_drawer", Properties.copy(Blocks.ENDER_CHEST), EnderDrawerTile.class);
Expand Down Expand Up @@ -125,6 +125,7 @@ public void attack(BlockState state, Level worldIn, BlockPos pos, Player player)
TileUtil.getTileEntity(worldIn, pos, EnderDrawerTile.class).ifPresent(drawerTile -> drawerTile.onClicked(player, getHit(state, worldIn, pos, player)));
}

@Override
public int getHit(BlockState state, Level worldIn, BlockPos pos, Player player) {
HitResult result = RayTraceUtils.rayTraceSimple(worldIn, player, 32, 0);
if (result instanceof BlockHitResult) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
import java.util.Optional;
import java.util.function.Consumer;

public class FluidDrawerBlock extends RotatableBlock<FluidDrawerTile> {
public class FluidDrawerBlock extends RotatableBlock<FluidDrawerTile> implements Drawer {

/**
* Framed version
Expand Down Expand Up @@ -144,6 +144,7 @@ public void attack(BlockState state, Level worldIn, BlockPos pos, Player player)
TileUtil.getTileEntity(worldIn, pos, FluidDrawerTile.class).ifPresent(drawerTile -> drawerTile.onClicked(player, getHit(state, worldIn, pos, player)));
}

@Override
public int getHit(BlockState state, Level worldIn, BlockPos pos, Player player) {
HitResult result = RayTraceUtils.rayTraceSimple(worldIn, player, 32, 0);
if (result instanceof BlockHitResult) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ public ItemStack getStackInSlot(int slot) {
@Override
public ItemStack insertItem(int slot, @NotNull ItemStack stack, boolean simulate) {
if (isValid(slot, stack)) {
if (!simulate){
this.stackList.set(slot, stack);
if (!simulate) {
this.stackList.set(slot, stack.copyWithCount(1));
onChange();
}
return ItemStack.EMPTY;

return stack.getCount() > 1 ? stack.copyWithCount(stack.getCount() - 1) : ItemStack.EMPTY;
}
return stack;
}
Expand All @@ -53,12 +54,13 @@ public ItemStack insertItem(int slot, @NotNull ItemStack stack, boolean simulate
@NotNull
@Override
public ItemStack extractItem(int slot, int amount, boolean simulate) {
if (!simulate){
ItemStack stack = this.stackList.set(slot, ItemStack.EMPTY);
var inSlot = this.stackList.get(slot).copy();
if (amount == 0 || inSlot.isEmpty()) return inSlot;
if (!simulate) {
stackList.set(slot, ItemStack.EMPTY);
onChange();
return stack;
}
return this.stackList.get(slot);
return inSlot;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public abstract class BigInventoryHandler implements IItemHandler, INBTSerializa
public static String AMOUNT = "Amount";

private final FunctionalStorage.DrawerType type;
private List<BigStack> storedStacks;
private final List<BigStack> storedStacks;

public BigInventoryHandler(FunctionalStorage.DrawerType type) {
this.type = type;
Expand All @@ -39,9 +39,10 @@ public int getSlots() {
public ItemStack getStackInSlot(int slot) {
if (type.getSlots() == slot) return ItemStack.EMPTY;
BigStack bigStack = this.storedStacks.get(slot);
ItemStack copied = bigStack.getStack().copy();
copied.setCount(isCreative() ? Integer.MAX_VALUE : bigStack.getAmount());
return copied;
if (isCreative()) {
return bigStack.slotStack.copyWithCount(Integer.MAX_VALUE);
}
return bigStack.slotStack;
}

@Nonnull
Expand Down Expand Up @@ -168,11 +169,13 @@ public List<BigStack> getStoredStacks() {
public static class BigStack {

private ItemStack stack;
private ItemStack slotStack;
private int amount;

public BigStack(ItemStack stack, int amount) {
this.stack = stack.copy();
this.amount = amount;
this.slotStack = stack.copyWithCount(amount);
}

public ItemStack getStack() {
Expand All @@ -181,6 +184,7 @@ public ItemStack getStack() {

public void setStack(ItemStack stack) {
this.stack = stack.copy();
this.slotStack = stack.copyWithCount(amount);
}

public int getAmount() {
Expand All @@ -189,6 +193,7 @@ public int getAmount() {

public void setAmount(int amount) {
this.amount = amount;
this.slotStack.setCount(amount);
}
}
}

0 comments on commit 7cbd075

Please sign in to comment.