Skip to content

Commit

Permalink
ref: better code performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Uhutown committed Oct 18, 2023
1 parent 4767759 commit e43d4e8
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/troblecodings/signals/blocks/Signal.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public AxisAlignedBB getBoundingBox(final IBlockState state, final IBlockAccess
final SignalStateInfo info = new SignalStateInfo(world, pos, this);
final Map<SEProperty, String> properties = world.isRemote
? ClientSignalStateHandler.getClientStates(new StateInfo(info.world, info.pos))
: SignalStateHandler.getStates(info);
: ((SignalTileEntity) te).getProperties();
return FULL_BLOCK_AABB.expand(0, getHeight(properties), 0);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public void deserializeClient(final ByteBuffer buf) {
CLIENT_NAMES.put(new StateInfo(mc.world, pos), name);
}
final WorldClient level = mc.world;
if (level == null)
return;
mc.addScheduledTask(() -> {
level.getChunkFromBlockCoords(pos).markDirty();
final IBlockState state = level.getBlockState(pos);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ private static void updateListeners(final SignalStateInfo info,
}
if (listeners == null)
return;
listeners.forEach(listener -> listener.update(info, changedProperties, changedState));
info.world.getMinecraftServer().addScheduledTask(() -> listeners
.forEach(listener -> listener.update(info, changedProperties, changedState)));
}

private static void statesToBuffer(final Signal signal, final Map<SEProperty, String> states,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,47 @@
package com.troblecodings.signals.tileentitys;

import java.util.HashMap;
import java.util.Map;

import com.google.common.collect.ImmutableMap;
import com.troblecodings.core.interfaces.NamableWrapper;
import com.troblecodings.guilib.ecs.interfaces.ISyncable;
import com.troblecodings.signals.SEProperty;
import com.troblecodings.signals.blocks.Signal;
import com.troblecodings.signals.core.RenderOverlayInfo;
import com.troblecodings.signals.core.SignalStateListener;
import com.troblecodings.signals.handler.SignalStateHandler;
import com.troblecodings.signals.handler.SignalStateInfo;

import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;

public class SignalTileEntity extends SyncableTileEntity implements NamableWrapper, ISyncable {

private final Map<SEProperty, String> properties = new HashMap<>();

private final SignalStateListener listener = (info, states, changed) -> {
switch (changed) {
case ADDED_TO_FILE:
case ADDED_TO_CACHE: {
properties.clear();
properties.putAll(SignalStateHandler.getStates(info));
break;
}
case REMOVED_FROM_FILE:
case REMOVED_FROM_CACHE: {
properties.clear();
break;
}
case UPDATED: {
properties.putAll(states);
break;
}
default:
break;
}
};

@Override
public boolean isValid(final EntityPlayer player) {
return true;
Expand All @@ -29,13 +61,26 @@ public Signal getSignal() {
return (Signal) getBlockType();
}

public Map<SEProperty, String> getProperties() {
return ImmutableMap.copyOf(properties);
}

@Override
public void onLoad() {
if (!world.isRemote) {
final IBlockState state = world.getBlockState(pos);
world.notifyBlockUpdate(pos, state, state, 3);
world.markBlockRangeForRenderUpdate(pos, pos);
markDirty();
SignalStateHandler.addListener(new SignalStateInfo(world, pos, getSignal()), listener);
}
}

@Override
public void onChunkUnload() {
if (!world.isRemote) {
SignalStateHandler.removeListener(new SignalStateInfo(world, pos, getSignal()),
listener);
}
}
}

0 comments on commit e43d4e8

Please sign in to comment.