-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #98 from JustAlittleWolf/1.20
Add Tablist buffering
- Loading branch information
Showing
7 changed files
with
134 additions
and
2 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
Shared/src/main/java/dev/tr7zw/exordium/access/TablistAccess.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package dev.tr7zw.exordium.access; | ||
|
||
import dev.tr7zw.exordium.util.BufferedComponent; | ||
import net.minecraft.world.scores.Objective; | ||
import net.minecraft.world.scores.Scoreboard; | ||
|
||
public interface TablistAccess { | ||
public void updateState(Scoreboard scoreboard, Objective objective); | ||
public BufferedComponent getBufferedComponent(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
Shared/src/main/java/dev/tr7zw/exordium/mixin/PlayerTabOverlayMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package dev.tr7zw.exordium.mixin; | ||
|
||
import dev.tr7zw.exordium.ExordiumModBase; | ||
import dev.tr7zw.exordium.access.TablistAccess; | ||
import dev.tr7zw.exordium.util.BufferedComponent; | ||
import net.minecraft.client.Minecraft; | ||
import net.minecraft.client.multiplayer.PlayerInfo; | ||
import net.minecraft.network.chat.Component; | ||
import net.minecraft.world.scores.Objective; | ||
import net.minecraft.world.scores.Score; | ||
import net.minecraft.world.scores.Scoreboard; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import net.minecraft.client.gui.components.PlayerTabOverlay; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
|
||
import java.util.List; | ||
|
||
@Mixin(PlayerTabOverlay.class) | ||
public abstract class PlayerTabOverlayMixin implements TablistAccess { | ||
@Shadow | ||
private Minecraft minecraft; | ||
private int playerInfoHash = 0; | ||
private int headerHash = 0; | ||
private int footerHash = 0; | ||
private int scoreboardHash = 0; | ||
private int objectiveHash = 0; | ||
@Shadow | ||
private Component header; | ||
@Shadow | ||
private Component footer; | ||
private Objective lastTrackedObjective; | ||
private boolean outdated; | ||
private BufferedComponent bufferedComponent = new BufferedComponent(true, ExordiumModBase.instance.config.tablistSettings) { | ||
|
||
@Override | ||
public boolean needsRender() { | ||
return outdated; | ||
} | ||
|
||
@Override | ||
public void captureState() { | ||
playerInfoHash = fastGetPlayerInfoListHashCode(getPlayerInfos()); | ||
headerHash = header == null ? 0 : header.getString().hashCode(); | ||
footerHash = footer == null ? 0 : footer.getString().hashCode(); | ||
} | ||
}; | ||
|
||
public void updateState(Scoreboard scoreboard, Objective objective) { | ||
boolean scoreboardOrObjectiveChange = scoreboardOrObjectiveChanged(scoreboard, objective); | ||
int newHeaderHash = header == null ? 0 : header.getString().hashCode(); | ||
int newFooterHash = footer == null ? 0 : footer.getString().hashCode(); | ||
outdated = playerInfoHash != fastGetPlayerInfoListHashCode(getPlayerInfos()) || headerHash != newHeaderHash || footerHash != newFooterHash || scoreboardOrObjectiveChange; | ||
} | ||
|
||
public boolean scoreboardOrObjectiveChanged(Scoreboard scoreboard, Objective objective) { | ||
if (objective == null && lastTrackedObjective == null) return false; | ||
|
||
int scoreboardHashCode = 1; | ||
for (Score score : scoreboard.getPlayerScores(objective)) | ||
scoreboardHashCode = 31 * scoreboardHashCode + (score == null ? 0 : score.getScore()); | ||
|
||
int newObjectiveHashCode = objective == null ? 0 : objective.getName().hashCode(); | ||
if (scoreboardHashCode == scoreboardHash && newObjectiveHashCode == objectiveHash) return false; | ||
scoreboardHash = scoreboardHashCode; | ||
objectiveHash = newObjectiveHashCode; | ||
lastTrackedObjective = objective; | ||
return true; | ||
} | ||
|
||
public int fastGetPlayerInfoListHashCode(List<PlayerInfo> playerInfos) { | ||
int hashCode = 1; | ||
for (PlayerInfo playerInfo : playerInfos) { | ||
int combinedHashes = 0; | ||
if (playerInfo == null) { | ||
hashCode *= 31; | ||
continue; | ||
} | ||
combinedHashes += playerInfo.getProfile().getId().hashCode(); | ||
combinedHashes += playerInfo.getTabListDisplayName() == null ? 0 : playerInfo.getTabListDisplayName().getString().hashCode(); | ||
combinedHashes += playerInfo.getSkinLocation().hashCode(); | ||
hashCode = 31 * hashCode + combinedHashes; | ||
} | ||
return hashCode; | ||
} | ||
|
||
public BufferedComponent getBufferedComponent() { | ||
return bufferedComponent; | ||
} | ||
|
||
@Shadow | ||
public abstract List<PlayerInfo> getPlayerInfos(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters