Skip to content

Commit

Permalink
chore: A handful of performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ferriarnus authored Sep 28, 2024
1 parent febf741 commit 7b2df7b
Show file tree
Hide file tree
Showing 25 changed files with 129 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,13 @@ public interface IGrindingBallData {
* Grinding ball identity value. Used when no bonus grinding ball is installed.
*/
IGrindingBallData IDENTITY = new IGrindingBallData() {

public static final ResourceLocation ENDERIO = new ResourceLocation("enderio", "grindingball/identity");

@Override
public ResourceLocation getGrindingBallId() {
// ID isn't actually mapped anywhere.
return new ResourceLocation("enderio", "grindingball/identity");
return ENDERIO;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class EnderBlockEntity extends BlockEntity {

public static final String DATA = "Data";
public static final String INDEX = "Index";
private boolean isChangedDeferred = true;
private final List<NetworkDataSlot<?>> dataSlots = new ArrayList<>();

private final List<Runnable> afterDataSync = new ArrayList<>();
Expand All @@ -56,24 +57,44 @@ public static void tick(Level level, BlockPos pos, BlockState state, EnderBlockE
} else {
blockEntity.serverTick();
}
blockEntity.endTick();
}

/**
* Perform server-side ticking
*/
public void serverTick() {
@UseOnly(LogicalSide.SERVER)
protected void serverTick() {
// Perform syncing.
if (level != null && !level.isClientSide) {
if (level != null) {
sync();
level.blockEntityChanged(worldPosition);
}
}

/**
* Perform client side ticking.
*/
public void clientTick() {
@UseOnly(LogicalSide.CLIENT)
protected void clientTick() {

}

/**
* Perform tick on both client and server, on the end.
*/
protected void endTick() {
if (this.level == null) {
return;
}
if (isChangedDeferred) {
isChangedDeferred = false;
setChanged(level, getBlockPos(), getBlockState());
}
}

@Override
public void setChanged() {
this.isChangedDeferred = true;
}

// endregion
Expand Down Expand Up @@ -129,23 +150,24 @@ public void handleUpdateTag(CompoundTag syncData) {

@Nullable
private FriendlyByteBuf createBufferSlotUpdate() {
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer());
int amount = 0;
List<Integer> needsUpdate = new ArrayList<>();
for (int i = 0; i < dataSlots.size(); i++) {
NetworkDataSlot<?> networkDataSlot = dataSlots.get(i);
if (networkDataSlot.needsUpdate()) {
amount ++;
buf.writeInt(i);
networkDataSlot.writeBuffer(buf);
if (dataSlots.get(i).needsUpdate()) {
needsUpdate.add(i);
}
}
if (amount == 0) {

if (needsUpdate.isEmpty()) {
return null;
}
FriendlyByteBuf result = new FriendlyByteBuf(Unpooled.buffer()); //Use 2 buffers to be able to write the amount of data
result.writeInt(amount);
result.writeBytes(buf.copy());
return result;

FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer());
buf.writeInt(needsUpdate.size());
needsUpdate.forEach(i -> {
buf.writeInt(i);
dataSlots.get(i).writeBuffer(buf);
});
return buf;
}

public void addDataSlot(NetworkDataSlot<?> slot) {
Expand Down Expand Up @@ -181,6 +203,7 @@ public <T> void clientUpdateSlot(@Nullable NetworkDataSlot<T> slot, T value) {
public void sync() {
var syncData = createBufferSlotUpdate();
if (syncData != null) {
setChanged();
CoreNetwork.sendToTracking(level.getChunkAt(getBlockPos()), new S2CDataSlotUpdate(getBlockPos(), syncData));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,18 @@ public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level pLevel, Bloc
public void neighborChanged(BlockState pState, Level pLevel, BlockPos pPos, Block pBlock, BlockPos pFromPos, boolean pIsMoving) {
super.neighborChanged(pState, pLevel, pPos, pBlock, pFromPos, pIsMoving);
updateBlockEntityCache(pLevel, pPos);
if (pLevel.getBlockEntity(pPos) instanceof MachineBlockEntity machineBlock) {
machineBlock.neighborChanged(pState, pLevel, pPos, pFromPos);
}
}

@Override
public void onNeighborChange(BlockState state, LevelReader level, BlockPos pos, BlockPos neighbor) {
super.onNeighborChange(state, level, pos, neighbor);
updateBlockEntityCache(level, pos);
if (level.getBlockEntity(pos) instanceof MachineBlockEntity machineBlock) {
machineBlock.neighborChanged(state, level, pos, neighbor);
}
}

private void updateBlockEntityCache(LevelReader level, BlockPos pos) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ protected SingleSlotAccess getOutputSlotAccess() {
// region Inventory

@Override
public MachineInventoryLayout getInventoryLayout() {
public MachineInventoryLayout createInventoryLayout() {
return MachineInventoryLayout.builder()
.inputSlot(3, this::acceptSlotInput)
.slotAccess(INPUTS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public AbstractContainerMenu createMenu(int containerId, Inventory inventory, Pl
}

@Override
public MachineInventoryLayout getInventoryLayout() {
public MachineInventoryLayout createInventoryLayout() {
return MachineInventoryLayout
.builder()
.capacitor()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void updateServerCallback() {
}

@Override
public @Nullable MachineInventoryLayout getInventoryLayout() {
public @Nullable MachineInventoryLayout createInventoryLayout() {
return MachineInventoryLayout.builder()
.capacitor()
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public RecipeWrapper getContainer() {
}

@Override
public MachineInventoryLayout getInventoryLayout() {
public MachineInventoryLayout createInventoryLayout() {
return MachineInventoryLayout.builder()
.inputSlot((slot, stack) -> stack.getItem() == Items.WRITABLE_BOOK)
.slotAccess(BOOK)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public boolean acceptItemDrain(ItemStack item) {
}

@Override
public MachineInventoryLayout getInventoryLayout() {
public MachineInventoryLayout createInventoryLayout() {
return MachineInventoryLayout
.builder()
.inputSlot((slot, stack) -> acceptItemFill(stack))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public AbstractContainerMenu createMenu(int containerId, Inventory inventory, Pl
}

@Override
public MachineInventoryLayout getInventoryLayout() {
public MachineInventoryLayout createInventoryLayout() {
return MachineInventoryLayout.builder()
.inputSlot(6, (integer, itemStack) -> ItemStack.isSameItemSameTags(itemStack, GHOST.get(integer).getItemStack(this)))
.slotAccess(INPUT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public record MachineState(MachineStateType type, MutableComponent component) {
public static final MachineState FULL_OUTPUT = new MachineState(MachineStateType.ERROR, MachineLang.TOOLTIP_OUTPUT_FULL);
public static final MachineState REDSTONE = new MachineState(MachineStateType.DISABLED, MachineLang.TOOLTIP_BLOCKED_RESTONE);


@Override
public boolean equals(Object o) {
if (this == o) {
Expand All @@ -31,17 +30,13 @@ public boolean equals(Object o) {
return false;
}
MachineState that = (MachineState) o;

if (type != that.type) {
return false;
}
return component.equals(that.component);
return type == that.type && component == that.component; //Use identity
}

@Override
public int hashCode() {
int result = type.hashCode();
result = 31 * result + component.hashCode();
int result = type.ordinal();
result = 31 * result + System.identityHashCode(component); //Only hash instance
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void onLoad() {
// region Inventory

@Override
public MachineInventoryLayout getInventoryLayout() {
public MachineInventoryLayout createInventoryLayout() {
return MachineInventoryLayout.builder()
.capacitor()
.inputSlot(this::isValidInput)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void onLoad() {
// region Inventory

@Override
public MachineInventoryLayout getInventoryLayout() {
public MachineInventoryLayout createInventoryLayout() {
return MachineInventoryLayout.builder().capacitor().build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected SingleSlotAccess getOutputSlotAccess() {
}

@Override
public MachineInventoryLayout getInventoryLayout() {
public MachineInventoryLayout createInventoryLayout() {
return MachineInventoryLayout.builder()
.inputSlot((s, i) -> ForgeHooks.getBurnTime(i, RecipeType.SMELTING) > 0)
.slotAccess(FUEL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void onLoad() {
}

@Override
public MachineInventoryLayout getInventoryLayout() {
public MachineInventoryLayout createInventoryLayout() {
return MachineInventoryLayout.builder()
.inputSlot(this::isValidInput)
.slotAccess(INPUT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void onLoad() {
// region Inventory

@Override
public MachineInventoryLayout getInventoryLayout() {
public MachineInventoryLayout createInventoryLayout() {
return MachineInventoryLayout.builder()
.setStackLimit(1) // Force all input slots to have 1 output
.inputSlot(6, this::isValidInput)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void onLoad() {
// region Inventory

@Override
public MachineInventoryLayout getInventoryLayout() {
public MachineInventoryLayout createInventoryLayout() {
return MachineInventoryLayout.builder()
.setStackLimit(1)
.inputSlot((slot, stack) -> stack.is(EIOItems.FILLED_SOUL_VIAL.get()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public SoulEngineBlockEntity(BlockEntityType<?> type,
}

@Override
public MachineInventoryLayout getInventoryLayout() {
public MachineInventoryLayout createInventoryLayout() {
return MachineInventoryLayout.builder()
.capacitor()
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public int getGenerationRate() {
}

@Override
public MachineInventoryLayout getInventoryLayout() {
public MachineInventoryLayout createInventoryLayout() {
return MachineInventoryLayout.builder()
.inputSlot((slot, stack) -> ForgeHooks.getBurnTime(stack, RecipeType.SMELTING) > 0 && stack.getCraftingRemainingItem().isEmpty())
.slotAccess(FUEL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public AbstractContainerMenu createMenu(int containerId, Inventory inventory, Pl
}

@Override
public @Nullable MachineInventoryLayout getInventoryLayout() {
public @Nullable MachineInventoryLayout createInventoryLayout() {
return MachineInventoryLayout.builder().setStackLimit(1).ghostSlot().slotAccess(GHOST).build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public AbstractContainerMenu createMenu(int containerId, Inventory inventory, Pl
}

@Override
public MachineInventoryLayout getInventoryLayout() {
public MachineInventoryLayout createInventoryLayout() {
return extractableGUISlot(MachineInventoryLayout.builder(), 27)
.slot(slot -> slot.guiInsert().guiExtract().filter(this::acceptFilter))
.slotAccess(FILTER_SLOT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public WiredChargerBlockEntity(BlockEntityType<?> type, BlockPos worldPosition,
}

@Override
public MachineInventoryLayout getInventoryLayout() {
public MachineInventoryLayout createInventoryLayout() {
return MachineInventoryLayout
.builder()
.capacitor()
Expand Down
Loading

0 comments on commit 7b2df7b

Please sign in to comment.