Skip to content

Commit

Permalink
chore: A handful of performance improvements ported from 1.20.1
Browse files Browse the repository at this point in the history
  • Loading branch information
ferriarnus authored Sep 26, 2024
1 parent bfd0a0c commit cb66072
Show file tree
Hide file tree
Showing 27 changed files with 150 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.ChunkPos;
Expand Down Expand Up @@ -42,6 +41,7 @@ public class EnderBlockEntity extends BlockEntity {
public static final String INDEX = "Index";
private final List<NetworkDataSlot<?>> dataSlots = new ArrayList<>();
private final List<Runnable> afterDataSync = new ArrayList<>();
private boolean isChangedDeferred = true;

private final Map<BlockCapability<?, ?>, EnumMap<Direction, BlockCapabilityCache<?, ?>>> selfCapabilities = new HashMap<>();
private final Map<BlockCapability<?, ?>, EnumMap<Direction, BlockCapabilityCache<?, ?>>> neighbourCapabilities = new HashMap<>();
Expand All @@ -59,26 +59,46 @@ public static void tick(Level level, BlockPos pos, BlockState state, EnderBlockE
} else {
blockEntity.serverTick();
}
blockEntity.endTick();
}

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

/**
* Perform client side ticking.
*/
@EnsureSide(EnsureSide.Side.CLIENT)
public void clientTick() {

}

/**
* Perform client and server side ticking.
*/
public void endTick() {
if (this.level == null) {
return;
}
if (isChangedDeferred) {
isChangedDeferred = false;
setChanged(level, getBlockPos(), getBlockState());
}
}

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

// endregion

// region Sync
Expand Down Expand Up @@ -128,26 +148,25 @@ public void handleUpdateTag(CompoundTag syncData, HolderLookup.Provider lookupPr
}

private byte @Nullable [] createBufferSlotUpdate() {
RegistryFriendlyByteBuf buf = new RegistryFriendlyByteBuf(Unpooled.buffer(), level.registryAccess());
int amount = 0;
List<Integer> needsUpdate = new ArrayList<>();
for (int i = 0; i < dataSlots.size(); i++) {
var networkDataSlot = dataSlots.get(i);
if (networkDataSlot.doesNeedUpdate()) {
amount ++;
buf.writeInt(i);
networkDataSlot.write(buf);
if (dataSlots.get(i).doesNeedUpdate()) {
needsUpdate.add(i);
}
}

if (amount == 0) {
if (needsUpdate.isEmpty()) {
return null;
}

// Fine to use a normal byte buf here, we're not using codecs in here.
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.array();
RegistryFriendlyByteBuf buf = new RegistryFriendlyByteBuf(Unpooled.buffer(), level.registryAccess());
buf.writeInt(needsUpdate.size());
needsUpdate.forEach(i -> {
buf.writeInt(i);
dataSlots.get(i).write(buf);
});
return buf.array();
}

public <T extends NetworkDataSlot<?>> T addDataSlot(T slot) {
Expand Down Expand Up @@ -184,6 +203,7 @@ public <T> void clientUpdateSlot(@Nullable NetworkDataSlot<T> slot, T value) {
public void sync() {
var syncData = createBufferSlotUpdate();
if (syncData != null && level instanceof ServerLevel serverLevel) {
setChanged();
PacketDistributor.sendToPlayersTrackingChunk(serverLevel, new ChunkPos(getBlockPos()),
new ServerboundCDataSlotUpdate(getBlockPos(), syncData));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.BaseEntityBlock;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.RenderShape;
Expand Down Expand Up @@ -161,4 +162,20 @@ public int getLightEmission(BlockState state, BlockGetter level, BlockPos pos) {
}
return super.getLightEmission(state, level, pos);
}

@Override
protected void neighborChanged(BlockState state, Level level, BlockPos pos, Block neighborBlock, BlockPos neighborPos, boolean movedByPiston) {
super.neighborChanged(state, level, pos, neighborBlock, neighborPos, movedByPiston);
if (level.getBlockEntity(pos) instanceof MachineBlockEntity machineBlock) {
machineBlock.neighborChanged(state, level, pos, neighborPos);
}
}

@Override
public void onNeighborChange(BlockState state, LevelReader level, BlockPos pos, BlockPos neighbor) {
super.onNeighborChange(state, level, pos, neighbor);
if (level.getBlockEntity(pos) instanceof MachineBlockEntity machineBlock) {
machineBlock.neighborChanged(state, level, pos, neighbor);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,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 @@ -60,7 +60,7 @@ protected void updateLocations() {
}

@Override
public @Nullable MachineInventoryLayout getInventoryLayout() {
public @Nullable MachineInventoryLayout createInventoryLayout() {
return MachineInventoryLayout.builder()
.inputSlot((integer, itemStack) -> itemStack.getCapability(EIOCapabilities.Filter.ITEM) instanceof EntityFilter)
.slotAccess(FILTER)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,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 @@ -100,7 +100,7 @@ private void internalSetActionRange(ActionRange actionRange) {
}

@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 @@ -165,7 +165,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.isSameItemSameComponents(itemStack, GHOST.get(integer).getItemStack(this)))
.slotAccess(INPUT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected void updateLocations() {
}

@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 @@ -45,4 +45,21 @@ public record MachineState(MachineStateType type, MutableComponent component) {

public static final NetworkDataSlot.CodecType<Set<MachineState>> DATA_SLOT_TYPE
= NetworkDataSlot.CodecType.createSet(CODEC, STREAM_CODEC.cast());

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
MachineState that = (MachineState) obj;
return type == that.type && component == that.component; //Use identity
}

@Override
public int hashCode() {
return 31 * type.ordinal() + System.identityHashCode(component); //Only hash instance
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,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 @@ -141,7 +141,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 @@ -63,7 +63,7 @@ protected SingleSlotAccess getOutputSlotAccess() {
}

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

@Override
public @Nullable MachineInventoryLayout getInventoryLayout() {
public @Nullable MachineInventoryLayout createInventoryLayout() {
return MachineInventoryLayout.builder()
.inputSlot((integer, itemStack) -> itemStack.getCapability(EIOCapabilities.Filter.ITEM) instanceof EntityFilter)
.slotAccess(FILTER)
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 @@ -85,7 +85,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 @@ -104,7 +104,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 @@ -83,7 +83,7 @@ public SoulEngineBlockEntity(BlockPos worldPosition, BlockState blockState) {
}

@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 @@ -62,7 +62,7 @@ public int getGenerationRate() {
}

@Override
public MachineInventoryLayout getInventoryLayout() {
public MachineInventoryLayout createInventoryLayout() {
return MachineInventoryLayout.builder()
.inputSlot((slot, stack) -> stack.getBurnTime(RecipeType.SMELTING) > 0 && stack.getCraftingRemainingItem().isEmpty())
.slotAccess(FUEL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,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 @@ -31,7 +31,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((i, s) -> s.getCapability(EIOCapabilities.Filter.ITEM) instanceof ItemFilterCapability))
.slotAccess(FILTER)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ protected VatCraftingMachineTask createTask(Level level, FermentingRecipe.Input
}

@Override
public @Nullable MachineInventoryLayout getInventoryLayout() {
public @Nullable MachineInventoryLayout createInventoryLayout() {
return MachineInventoryLayout.builder().inputSlot(2).slotAccess(REAGENTS).build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public WiredChargerBlockEntity(BlockPos worldPosition, BlockState blockState) {
}

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

0 comments on commit cb66072

Please sign in to comment.