Skip to content

Commit

Permalink
Scoreboard: Completely revamp the API
Browse files Browse the repository at this point in the history
Adds mutability to Scoreboard.Score among these other changes: (added unless otherwise stated)

Team:
	- getColor
	- setColor
Scoreboard:
	- addLine
	- createTeam
	- removeIndex
	- removeScores
Scoreboard.Score:
	- getNumberFormat
	- getTeam
	- remove
	- setName
	- setNumberFormat
	- setPoints
	- setTeam
  • Loading branch information
camnwalter authored and mattco98 committed Apr 10, 2024
1 parent 1f69d14 commit 5dc54f0
Show file tree
Hide file tree
Showing 11 changed files with 330 additions and 53 deletions.
31 changes: 27 additions & 4 deletions api/ctjs.api
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,7 @@ public final class com/chattriggers/ctjs/api/entity/PlayerMP : com/chattriggers/
public final class com/chattriggers/ctjs/api/entity/Team : com/chattriggers/ctjs/api/CTWrapper {
public fun <init> (Lnet/minecraft/scoreboard/Team;)V
public final fun canSeeInvisibleTeammates ()Z
public final fun getColor ()Ljava/lang/String;
public final fun getDeathMessageVisibility ()Lcom/chattriggers/ctjs/api/entity/Team$Visibility;
public final fun getFriendlyFire ()Z
public synthetic fun getMcValue ()Ljava/lang/Object;
Expand All @@ -1023,6 +1024,7 @@ public final class com/chattriggers/ctjs/api/entity/Team : com/chattriggers/ctjs
public final fun getPrefix ()Ljava/lang/String;
public final fun getRegisteredName ()Ljava/lang/String;
public final fun getSuffix ()Ljava/lang/String;
public final fun setColor (Ljava/lang/Object;)Lcom/chattriggers/ctjs/api/entity/Team;
public final fun setName (Lcom/chattriggers/ctjs/api/message/TextComponent;)Lcom/chattriggers/ctjs/api/entity/Team;
public final fun setName (Ljava/lang/String;)Lcom/chattriggers/ctjs/api/entity/Team;
public final fun setPrefix (Lcom/chattriggers/ctjs/api/message/TextComponent;)Lcom/chattriggers/ctjs/api/entity/Team;
Expand All @@ -1033,6 +1035,10 @@ public final class com/chattriggers/ctjs/api/entity/Team : com/chattriggers/ctjs
public fun toMC ()Lnet/minecraft/scoreboard/Team;
}

public synthetic class com/chattriggers/ctjs/api/entity/Team$EntriesMappings {
public static final synthetic field entries$0 Lkotlin/enums/EnumEntries;
}

public final class com/chattriggers/ctjs/api/entity/Team$Visibility : java/lang/Enum, com/chattriggers/ctjs/api/CTWrapper {
public static final field ALWAYS Lcom/chattriggers/ctjs/api/entity/Team$Visibility;
public static final field Companion Lcom/chattriggers/ctjs/api/entity/Team$Visibility$Companion;
Expand Down Expand Up @@ -2314,6 +2320,9 @@ public final class com/chattriggers/ctjs/api/world/PotionEffectType {

public final class com/chattriggers/ctjs/api/world/Scoreboard {
public static final field INSTANCE Lcom/chattriggers/ctjs/api/world/Scoreboard;
public static final fun addLine (ILcom/chattriggers/ctjs/api/message/TextComponent;)V
public static final fun addLine (ILjava/lang/String;)V
public static final fun createTeam (Ljava/lang/String;)Lcom/chattriggers/ctjs/api/entity/Team;
public static final fun getLineByIndex (I)Lcom/chattriggers/ctjs/api/world/Scoreboard$Score;
public static final fun getLines ()Ljava/util/List;
public static final fun getLines (Z)Ljava/util/List;
Expand All @@ -2323,6 +2332,10 @@ public final class com/chattriggers/ctjs/api/world/Scoreboard {
public static final fun getShouldRender ()Z
public static final fun getSidebar ()Lnet/minecraft/scoreboard/ScoreboardObjective;
public static final fun getTitle ()Lcom/chattriggers/ctjs/api/message/TextComponent;
public static final fun removeIndex (I)V
public static final fun removeIndex (IZ)V
public static synthetic fun removeIndex$default (IZILjava/lang/Object;)V
public static final fun removeScores (I)V
public static final fun setLine (ILcom/chattriggers/ctjs/api/message/TextComponent;)V
public static final fun setLine (ILcom/chattriggers/ctjs/api/message/TextComponent;Z)V
public static final fun setLine (ILjava/lang/String;)V
Expand All @@ -2335,11 +2348,21 @@ public final class com/chattriggers/ctjs/api/world/Scoreboard {
public static final fun toMC ()Lnet/minecraft/scoreboard/Scoreboard;
}

public final class com/chattriggers/ctjs/api/world/Scoreboard$Score {
public fun <init> (Lnet/minecraft/scoreboard/ScoreboardEntry;)V
public final class com/chattriggers/ctjs/api/world/Scoreboard$Score : com/chattriggers/ctjs/api/CTWrapper {
public fun <init> (Lnet/minecraft/scoreboard/ScoreAccess;)V
public synthetic fun getMcValue ()Ljava/lang/Object;
public fun getMcValue ()Lnet/minecraft/scoreboard/ScoreAccess;
public final fun getName ()Lcom/chattriggers/ctjs/api/message/TextComponent;
public final fun getPoints ()I
public final fun toMC ()Lnet/minecraft/scoreboard/ScoreboardEntry;
public final fun getNumberFormat ()Lnet/minecraft/scoreboard/number/NumberFormat;
public final fun getScore ()I
public final fun getTeam ()Lcom/chattriggers/ctjs/api/entity/Team;
public final fun remove ()V
public final fun setName (Lcom/chattriggers/ctjs/api/message/TextComponent;)Lcom/chattriggers/ctjs/api/world/Scoreboard$Score;
public final fun setNumberFormat (Ljava/lang/Object;)Lcom/chattriggers/ctjs/api/world/Scoreboard$Score;
public final fun setScore (I)Lcom/chattriggers/ctjs/api/world/Scoreboard$Score;
public final fun setTeam (Lcom/chattriggers/ctjs/api/entity/Team;)Lcom/chattriggers/ctjs/api/world/Scoreboard$Score;
public synthetic fun toMC ()Ljava/lang/Object;
public fun toMC ()Lnet/minecraft/scoreboard/ScoreAccess;
public fun toString ()Ljava/lang/String;
}

Expand Down
10 changes: 8 additions & 2 deletions docs/MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,11 @@ Here is a list of targeted changes for various different APIs:
- `Scoreboard`
- Remove `Scoreboard.getScoreboardTitle()` in favor of the less verbose `Scoreboard.getTitle()`
- `Scoreboard.getTitle()` now returns `TextComponent` instead of `String`
- `Score.getName()` now returns `TextComponent` instead of `String`
- `Score`
- is now mutable. You can now edit the score, name, number format, and team
- `getPoints`/`setPoints` are renamed to `getScore`/`setScore`
- Added `addLine()`, `createTeam()`, `removeIndex()`, `removeScores()` methods
- `getLines` now actually sorts by descending instead of ascending
- `Book` now uses `TextComponent` instead of `Message`
- `Settings`
- Renamed all methods in the `skin` object to indicate they return whether the part is enabled, not the actual part themselves (i.e. `getCape()` -> `isCapeEnabled()`)
Expand Down Expand Up @@ -195,7 +199,9 @@ Here is a list of targeted changes for various different APIs:
- `TabList`
- Renamed `getHeaderMessage()` to `getHeaderComponent()`, and it now returns a `TextComponent` instead of a `Message`
- Renamed `getFooterMessage()` to `getFooterComponent()`, and it now returns a `TextComponent` instead of a `Message`
- `Team.getNameTagVisibility()` and `Team.getDeathMessageVisibility()` now return a `Team.Visibility` instead of a string
- `Team`
- `Team.getNameTagVisibility()` and `Team.getDeathMessageVisibility()` now return a `Team.Visibility` instead of a string
- Added `setColor()`
- `Client`
- `getChatGUI` was renamed to `getChatGui` to match the naming of `getTabGui`
- Removed `Config.modulesFolder`. Use `ChatTriggers.MODULES_FOLDER` or the string `"./config/ChatTriggers/modules"`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.chattriggers.ctjs.internal.mixins;

import com.chattriggers.ctjs.api.world.Scoreboard;
import com.chattriggers.ctjs.internal.engine.CTEvents;
import com.chattriggers.ctjs.api.triggers.TriggerType;
import com.chattriggers.ctjs.internal.engine.module.ModuleManager;
Expand Down Expand Up @@ -29,8 +30,10 @@ private void injectWorldUnload(ClientWorld world, CallbackInfo ci) {
TriggerType.SERVER_DISCONNECT.triggerAll();
}

if (this.world != null)
if (this.world != null) {
TriggerType.WORLD_UNLOAD.triggerAll();
Scoreboard.INSTANCE.clearCustom$ctjs();
}
}

@Inject(method = "joinWorld", at = @At("TAIL"))
Expand All @@ -46,6 +49,7 @@ private void injectDisconnect(Screen screen, CallbackInfo ci) {
if (this.isIntegratedServerRunning() || this.getCurrentServerEntry() != null) {
TriggerType.WORLD_UNLOAD.triggerAll();
TriggerType.SERVER_DISCONNECT.triggerAll();
Scoreboard.INSTANCE.clearCustom$ctjs();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.chattriggers.ctjs.internal.mixins;

import net.minecraft.scoreboard.ScoreHolder;
import net.minecraft.scoreboard.ScoreboardScore;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(targets = "net.minecraft.scoreboard.Scoreboard$1")
public interface Scoreboard$1Accessor {
@Accessor("field_47543")
ScoreboardScore getScore();

@Accessor("field_47547")
ScoreHolder getHolder();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.chattriggers.ctjs.internal.mixins;

import com.chattriggers.ctjs.api.world.Scoreboard;
import net.minecraft.scoreboard.ScoreboardObjective;
import net.minecraft.text.Text;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(ScoreboardObjective.class)
public class ScoreboardObjectiveMixin {
@Inject(method = "setDisplayName", at = @At("HEAD"), cancellable = true)
private void chattriggers$keepCustomName(Text name, CallbackInfo ci) {
if (Scoreboard.INSTANCE.getCustomTitle$ctjs()) {
ci.cancel();
}
}
}
2 changes: 2 additions & 0 deletions src/main/kotlin/com/chattriggers/ctjs/CTJS.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.chattriggers.ctjs.api.commands.DynamicCommands
import com.chattriggers.ctjs.api.message.ChatLib
import com.chattriggers.ctjs.api.render.Image
import com.chattriggers.ctjs.api.triggers.TriggerType
import com.chattriggers.ctjs.api.world.Scoreboard
import com.chattriggers.ctjs.api.world.World
import com.chattriggers.ctjs.engine.Console
import com.chattriggers.ctjs.engine.Register
Expand Down Expand Up @@ -85,6 +86,7 @@ class CTJS : ClientModInitializer {
fun unload(asCommand: Boolean = true) {
TriggerType.WORLD_UNLOAD.triggerAll()
TriggerType.GAME_UNLOAD.triggerAll()
Scoreboard.clearCustom()

isLoaded = false

Expand Down
21 changes: 21 additions & 0 deletions src/main/kotlin/com/chattriggers/ctjs/api/entity/Team.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package com.chattriggers.ctjs.api.entity
import com.chattriggers.ctjs.api.CTWrapper
import com.chattriggers.ctjs.api.message.TextComponent
import com.chattriggers.ctjs.MCTeam
import com.chattriggers.ctjs.api.message.ChatLib
import net.minecraft.scoreboard.AbstractTeam
import net.minecraft.text.TextColor
import net.minecraft.util.Formatting

class Team(override val mcValue: MCTeam) : CTWrapper<MCTeam> {
/**
Expand Down Expand Up @@ -79,6 +82,24 @@ class Team(override val mcValue: MCTeam) : CTWrapper<MCTeam> {
*/
fun setSuffix(suffix: String) = setSuffix(TextComponent(suffix))

fun getColor() = mcValue.color.toString()

/**
* Sets the team color
* @param color a string format of a [Formatting], or a hex value
* @return the team for method chaining
*/
fun setColor(color: Any?) = apply {
mcValue.color = when (color) {
is Number -> Formatting.byColorIndex(color.toInt())
is CharSequence -> Formatting.entries.find {
it.toString() == ChatLib.addColor(color.toString())
} ?: Formatting.RESET
null -> Formatting.RESET
else -> throw IllegalArgumentException("Could not convert type ${color::class.simpleName} to a Formatting")
}
}

/**
* Gets the team's friendly fire setting
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,12 +399,12 @@ class TextComponent private constructor(
}
}

private companion object {
internal companion object {
private val colorToFormatChar = Formatting.entries.mapNotNull { format ->
TextColor.fromFormatting(format)?.let { it to format }
}.toMap()

private fun jsObjectToStyle(obj: NativeObject): Style {
internal fun jsObjectToStyle(obj: NativeObject): Style {
return Style.EMPTY
.withColor(obj["color"]?.let { color ->
when (color) {
Expand Down
Loading

0 comments on commit 5dc54f0

Please sign in to comment.