Skip to content

Commit

Permalink
- Improve the mechanism for selective TileEntity updates.
Browse files Browse the repository at this point in the history
- Fix a strong dependency issue with Mekanism.
  • Loading branch information
KasumiNova committed Sep 21, 2023
1 parent 0260e43 commit 07667f2
Show file tree
Hide file tree
Showing 19 changed files with 86 additions and 116 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
package github.kasuminova.mmce.common.block.appeng;

import net.minecraft.block.state.IBlockState;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

import javax.annotation.Nonnull;

public abstract class BlockMEItemBus extends BlockMEMachineComponent {

@Override
public void dropBlockAsItemWithChance(@Nonnull final World worldIn, @Nonnull final BlockPos pos, @Nonnull final IBlockState state, final float chance, final int fortune) {
}

// @Override
// public boolean hasComparatorInputOverride(@Nonnull IBlockState state) {
// return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public TileEntity createTileEntity(final World world, final IBlockState state) {
return new MEItemInputBus();
}

@Override
public void dropBlockAsItemWithChance(@Nonnull final World worldIn, @Nonnull final BlockPos pos, @Nonnull final IBlockState state, final float chance, final int fortune) {
}

@Override
public void breakBlock(final World worldIn,
@Nonnull final BlockPos pos,
Expand All @@ -50,16 +54,17 @@ public void breakBlock(final World worldIn,
TileEntity te = worldIn.getTileEntity(pos);

if (te == null) {
super.breakBlock(worldIn, pos, state);
super.dropBlockAsItemWithChance(worldIn, pos, state, 1.0F, 0);
worldIn.removeTileEntity(pos);
return;
}
if (!(te instanceof final MEItemInputBus bus)) {
super.breakBlock(worldIn, pos, state);
worldIn.removeTileEntity(pos);
return;
}

if (!bus.hasItem() && !bus.configInvHasItem()) {
super.breakBlock(worldIn, pos, state);
worldIn.removeTileEntity(pos);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public TileEntity createTileEntity(final World world, final IBlockState state) {
return new MEItemOutputBus();
}

@Override
public void dropBlockAsItemWithChance(@Nonnull final World worldIn, @Nonnull final BlockPos pos, @Nonnull final IBlockState state, final float chance, final int fortune) {
}

@Override
public void breakBlock(final World worldIn,
@Nonnull final BlockPos pos,
Expand All @@ -52,16 +56,19 @@ public void breakBlock(final World worldIn,
TileEntity te = worldIn.getTileEntity(pos);

if (te == null) {
super.breakBlock(worldIn, pos, state);
super.dropBlockAsItemWithChance(worldIn, pos, state, 1.0F, 0);
worldIn.removeTileEntity(pos);
return;
}
if (!(te instanceof final MEItemOutputBus bus)) {
super.breakBlock(worldIn, pos, state);
super.dropBlockAsItemWithChance(worldIn, pos, state, 1.0F, 0);
worldIn.removeTileEntity(pos);
return;
}

if (!bus.hasItem()) {
super.breakBlock(worldIn, pos, state);
super.dropBlockAsItemWithChance(worldIn, pos, state, 1.0F, 0);
worldIn.removeTileEntity(pos);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public class TaskExecutor {

private final MpscLinkedAtomicQueue<Action> mainThreadActions = new MpscLinkedAtomicQueue<>();
private final MpscLinkedAtomicQueue<TileEntitySynchronized> requireUpdateTEQueue = new MpscLinkedAtomicQueue<>();
private final MpscLinkedAtomicQueue<TileEntitySynchronized> requireMarkNoUpdateTEQueue = new MpscLinkedAtomicQueue<>();

private final TaskSubmitter submitter = new TaskSubmitter();

private volatile boolean inTick = false;
Expand Down Expand Up @@ -141,14 +143,18 @@ private int spinAwaitActionExecutor() {
}

private void updateTileEntity() {
if (requireUpdateTEQueue.isEmpty()) {
if (requireUpdateTEQueue.isEmpty() && requireMarkNoUpdateTEQueue.isEmpty()) {
return;
}

TileEntitySynchronized te;
while ((te = requireUpdateTEQueue.poll()) != null) {
te.markForUpdate();
}

while ((te = requireMarkNoUpdateTEQueue.poll()) != null) {
te.markNoUpdate();
}
}

/**
Expand Down Expand Up @@ -193,6 +199,10 @@ public void addTEUpdateTask(final TileEntitySynchronized te) {
requireUpdateTEQueue.offer(te);
}

public void addTEMarkNoUpdateTask(final TileEntitySynchronized te) {
requireMarkNoUpdateTEQueue.offer(te);
}

private synchronized void submitTask() {
ActionExecutor executor;
while ((executor = executors.poll()) != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import net.minecraft.inventory.Container;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
Expand All @@ -29,7 +28,7 @@
@SuppressWarnings("MethodMayBeStatic")
public class EventHandler {
/**
* <p>当玩家对打开控制器界面时更新控制器的信息,以避免某些消息不同步的问题。</p>
* <p>当玩家对某些方块实体右击时更新方块实体,以避免某些消息不同步的问题。</p>
* <p>Update the controller's information when the player interacts with the controller,
* Avoid the problem of some messages being out of sync.</p>
*/
Expand All @@ -41,13 +40,12 @@ public void onPlayerRightClickBlock(PlayerInteractEvent.RightClickBlock event) {
}

TileEntity te = world.getTileEntity(event.getPos());
if (!(te instanceof SelectiveUpdateTileEntity)) {
if (!(te instanceof SelectiveUpdateTileEntity) || !(te instanceof final TileEntitySynchronized teSync)) {
return;
}
SPacketUpdateTileEntity packet = ((SelectiveUpdateTileEntity) te).getTrueUpdatePacket();
if (event.getEntityPlayer() instanceof EntityPlayerMP) {
((EntityPlayerMP) event.getEntityPlayer()).connection.sendPacket(packet);
}

// 触发更新,并使其同步至客户端。
teSync.notifyUpdate();
}

/**
Expand All @@ -74,7 +72,7 @@ public void onPlayerTick(TickEvent.PlayerTickEvent event) {
}
}

if (!(te instanceof SelectiveUpdateTileEntity) || !(te instanceof TileEntitySynchronized)) {
if (!(te instanceof SelectiveUpdateTileEntity) || !(te instanceof final TileEntitySynchronized teSync)) {
return;
}

Expand All @@ -83,12 +81,13 @@ public void onPlayerTick(TickEvent.PlayerTickEvent event) {
return;
}

teSync.markForUpdateSync();

ModularMachinery.EXECUTE_MANAGER.addSyncTask(() -> {
if (event.player instanceof EntityPlayerMP) {
EntityPlayerMP playerMP = (EntityPlayerMP) player;
TileEntitySynchronized teSynchronized = (TileEntitySynchronized) te;
if (teSynchronized.getLastUpdateTick() + 1 >= playerMP.world.getTotalWorldTime()) {
playerMP.connection.sendPacket(((SelectiveUpdateTileEntity) te).getTrueUpdatePacket());
if (teSync.getLastUpdateTick() + 1 >= playerMP.world.getTotalWorldTime()) {
teSync.notifyUpdate();
}

World world = event.player.getEntityWorld();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import hellfirepvp.modularmachinery.common.tiles.base.TileColorableMachineComponent;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraftforge.fml.common.FMLCommonHandler;

import javax.annotation.Nonnull;
Expand Down Expand Up @@ -55,16 +54,6 @@ public void writeCustomNBT(final NBTTagCompound compound) {
proxy.writeToNBT(compound);
}

@Override
public SPacketUpdateTileEntity getUpdatePacket() {
return null;
}

@Override
public SPacketUpdateTileEntity getTrueUpdatePacket() {
return super.getUpdatePacket();
}

// AppEng Compat

@MENetworkEventSubscribe
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,9 @@ private CraftCheck doEnergyIO(final List<ProcessingComponent<?>> components,
final RecipeCraftingContext context,
final float durationMultiplier)
{
float mul = doEnergyIOInternal(components, context, parallelism * durationMultiplier);
if (mul < parallelism) {
float maxMultiplier = parallelism * durationMultiplier;
float mul = doEnergyIOInternal(components, context, maxMultiplier);
if (mul < maxMultiplier) {
return switch (actionType) {
case INPUT -> CraftCheck.failure("craftcheck.failure.energy.input");
case OUTPUT -> CraftCheck.failure("craftcheck.failure.energy.output.space");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private void inputFromExternal(IItemHandler external) {

if (successAtLeastOnce) {
incrementSuccessCounter(maxWorkDelay, minWorkDelay);
markForUpdate();
markNoUpdate();
} else {
decrementSuccessCounter();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,15 @@ private void outputToExternal(IItemHandler external) {

if (successAtLeastOnce) {
incrementSuccessCounter(maxWorkDelay, minWorkDelay);
super.markForUpdate();
super.markNoUpdate();
} else {
decrementSuccessCounter();
}
}

@Override
public void markForUpdate() {
super.markForUpdate();
public void markNoUpdate() {
super.markNoUpdate();
inventoryChanged = true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ResourceLocation;
Expand Down Expand Up @@ -150,16 +149,6 @@ public void writeCustomNBT(final NBTTagCompound compound) {
}
}

@Override
public SPacketUpdateTileEntity getUpdatePacket() {
return null;
}

@Override
public SPacketUpdateTileEntity getTrueUpdatePacket() {
return super.getUpdatePacket();
}

public class UpgradeBusProvider extends MachineComponent<UpgradeBusProvider> {

public UpgradeBusProvider() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public long acceptEnergyFromNetwork(EnumFacing side, long voltage, long amperage
long acceptingAmperage = Math.min(availableSpace / voltage, maxAmperage);
if (acceptingAmperage > 0) {
hatch.setCurrentEnergy(hatch.getCurrentEnergy() + ((acceptingAmperage * voltage) * 4L));
hatch.markForUpdate();
hatch.markNoUpdate();
return acceptingAmperage;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package hellfirepvp.modularmachinery.common.tiles.base;

import net.minecraft.network.play.server.SPacketUpdateTileEntity;

/**
* <p>仅作为识别用的接口实现,一定程度上缓解过大的带宽占用问题。</p>
* <p>如果你想知道更多原因,请查看<a href="https://github.com/HellFirePvP/ModularMachinery/issues/228">此链接</a>。</p>
* <p>Alleviate excessive bandwidth usage to a certain extent</p>
* <p>If you want to know more about why, please <a href="https://github.com/HellFirePvP/ModularMachinery/issues/228">See This Link</a>.</p>
*/
public interface SelectiveUpdateTileEntity {
SPacketUpdateTileEntity getTrueUpdatePacket();

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void setMachineColor(int newColor) {
}

this.definedColor = newColor;
this.markForUpdate();
this.markForUpdateSync();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTPrimitive;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ITickable;
Expand Down Expand Up @@ -136,7 +135,7 @@ public int receiveEnergy(int maxReceive, boolean simulate) {
insertable = Math.min(insertable, convertDownEnergy(size.transferLimit));
if (!simulate) {
this.energy.set(MiscUtils.clamp(this.energy.get() + insertable, 0, this.size.maxEnergy));
markForUpdate();
markNoUpdate();
}
return insertable;
}
Expand All @@ -150,7 +149,7 @@ public int extractEnergy(int maxExtract, boolean simulate) {
extractable = Math.min(extractable, convertDownEnergy(size.transferLimit));
if (!simulate) {
this.energy.set(MiscUtils.clamp(this.energy.get() - extractable, 0, this.size.maxEnergy));
markForUpdate();
markNoUpdate();
}
return extractable;
}
Expand Down Expand Up @@ -194,16 +193,6 @@ public <T> T getCapability(@Nonnull Capability<T> capability, @Nullable EnumFaci
return super.getCapability(capability, facing);
}

@Override
public SPacketUpdateTileEntity getUpdatePacket() {
return null;
}

@Override
public SPacketUpdateTileEntity getTrueUpdatePacket() {
return super.getUpdatePacket();
}

@Override
public void readCustomNBT(NBTTagCompound compound) {
super.readCustomNBT(compound);
Expand Down
Loading

0 comments on commit 07667f2

Please sign in to comment.