-
-
Notifications
You must be signed in to change notification settings - Fork 159
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 #643 from booky10/fix/scoreboard-wrappers
Fix scoreboard wrappers for 1.20.3/1.20.4
- Loading branch information
Showing
11 changed files
with
909 additions
and
440 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
api/src/main/java/com/github/retrooper/packetevents/protocol/score/BlankScoreFormat.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,39 @@ | ||
/* | ||
* This file is part of packetevents - https://github.com/retrooper/packetevents | ||
* Copyright (C) 2024 retrooper and contributors | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.github.retrooper.packetevents.protocol.score; | ||
|
||
import net.kyori.adventure.text.Component; | ||
|
||
public final class BlankScoreFormat implements ScoreFormat { | ||
|
||
public static final BlankScoreFormat INSTANCE = new BlankScoreFormat(); | ||
|
||
private BlankScoreFormat() { | ||
} | ||
|
||
@Override | ||
public Component format(int score) { | ||
return Component.empty(); | ||
} | ||
|
||
@Override | ||
public ScoreFormatType getType() { | ||
return ScoreFormatTypes.BLANK; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
api/src/main/java/com/github/retrooper/packetevents/protocol/score/FixedScoreFormat.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,44 @@ | ||
/* | ||
* This file is part of packetevents - https://github.com/retrooper/packetevents | ||
* Copyright (C) 2024 retrooper and contributors | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.github.retrooper.packetevents.protocol.score; | ||
|
||
import net.kyori.adventure.text.Component; | ||
|
||
public final class FixedScoreFormat implements ScoreFormat { | ||
|
||
private final Component value; | ||
|
||
public FixedScoreFormat(Component value) { | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public ScoreFormatType getType() { | ||
return ScoreFormatTypes.FIXED; | ||
} | ||
|
||
@Override | ||
public Component format(int score) { | ||
return this.value; | ||
} | ||
|
||
public Component getValue() { | ||
return this.value; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
api/src/main/java/com/github/retrooper/packetevents/protocol/score/ScoreFormat.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,43 @@ | ||
/* | ||
* This file is part of packetevents - https://github.com/retrooper/packetevents | ||
* Copyright (C) 2024 retrooper and contributors | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.github.retrooper.packetevents.protocol.score; | ||
|
||
import net.kyori.adventure.text.Component; | ||
import net.kyori.adventure.text.format.Style; | ||
import org.jetbrains.annotations.ApiStatus; | ||
|
||
@ApiStatus.NonExtendable | ||
public interface ScoreFormat { | ||
|
||
static BlankScoreFormat blankScore() { | ||
return BlankScoreFormat.INSTANCE; | ||
} | ||
|
||
static StyledScoreFormat styledScore(Style style) { | ||
return new StyledScoreFormat(style); | ||
} | ||
|
||
static FixedScoreFormat fixedScore(Component value) { | ||
return new FixedScoreFormat(value); | ||
} | ||
|
||
Component format(int score); | ||
|
||
ScoreFormatType getType(); | ||
} |
29 changes: 29 additions & 0 deletions
29
api/src/main/java/com/github/retrooper/packetevents/protocol/score/ScoreFormatType.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,29 @@ | ||
/* | ||
* This file is part of packetevents - https://github.com/retrooper/packetevents | ||
* Copyright (C) 2024 retrooper and contributors | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.github.retrooper.packetevents.protocol.score; | ||
|
||
import com.github.retrooper.packetevents.protocol.mapper.StaticMappedEntity; | ||
import com.github.retrooper.packetevents.wrapper.PacketWrapper; | ||
|
||
public interface ScoreFormatType extends StaticMappedEntity { | ||
|
||
ScoreFormat read(PacketWrapper<?> wrapper); | ||
|
||
void write(PacketWrapper<?> wrapper, ScoreFormat format); | ||
} |
106 changes: 106 additions & 0 deletions
106
api/src/main/java/com/github/retrooper/packetevents/protocol/score/ScoreFormatTypes.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,106 @@ | ||
/* | ||
* This file is part of packetevents - https://github.com/retrooper/packetevents | ||
* Copyright (C) 2024 retrooper and contributors | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.github.retrooper.packetevents.protocol.score; | ||
|
||
import com.github.retrooper.packetevents.protocol.player.ClientVersion; | ||
import com.github.retrooper.packetevents.resources.ResourceLocation; | ||
import com.github.retrooper.packetevents.wrapper.PacketWrapper; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.BiConsumer; | ||
import java.util.function.Function; | ||
|
||
public final class ScoreFormatTypes { | ||
|
||
private static final Map<String, ScoreFormatType> SCORE_FORMAT_TYPE_MAP = new HashMap<>(); | ||
private static final Map<Byte, ScoreFormatType> SCORE_FORMAT_TYPE_ID_MAP = new HashMap<>(); | ||
|
||
public static final ScoreFormatType BLANK = define(0, "blank", BlankScoreFormat.class, | ||
wrapper -> ScoreFormat.blankScore(), | ||
(wrapper, format) -> { /**/ }); | ||
public static final ScoreFormatType STYLED = define(1, "styled", StyledScoreFormat.class, | ||
wrapper -> ScoreFormat.styledScore(wrapper.readStyle()), | ||
(wrapper, format) -> wrapper.writeStyle(format.getStyle())); | ||
public static final ScoreFormatType FIXED = define(2, "fixed", FixedScoreFormat.class, | ||
wrapper -> ScoreFormat.fixedScore(wrapper.readComponent()), | ||
(wrapper, format) -> wrapper.writeComponent(format.getValue())); | ||
|
||
private ScoreFormatTypes() { | ||
} | ||
|
||
public static ScoreFormat read(PacketWrapper<?> wrapper) { | ||
int formatTypeId = wrapper.readVarInt(); | ||
ScoreFormatType formatType = getById(wrapper.getServerVersion().toClientVersion(), formatTypeId); | ||
if (formatType == null) { | ||
throw new NullPointerException("Can't resolve format type " + formatTypeId); | ||
} | ||
return formatType.read(wrapper); | ||
} | ||
|
||
public static void write(PacketWrapper<?> wrapper, ScoreFormat format) { | ||
int formatTypeId = format.getType().getId(wrapper.getServerVersion().toClientVersion()); | ||
wrapper.writeVarInt(formatTypeId); | ||
format.getType().write(wrapper, format); | ||
} | ||
|
||
public static <T extends ScoreFormat> ScoreFormatType define(int id, String name, | ||
Class<T> formatClass, | ||
Function<PacketWrapper<?>, T> reader, | ||
BiConsumer<PacketWrapper<?>, T> writer) { | ||
ResourceLocation location = new ResourceLocation(name); | ||
ScoreFormatType type = new ScoreFormatType() { | ||
@Override | ||
public ScoreFormat read(PacketWrapper<?> wrapper) { | ||
return reader.apply(wrapper); | ||
} | ||
|
||
@Override | ||
public void write(PacketWrapper<?> wrapper, ScoreFormat format) { | ||
writer.accept(wrapper, formatClass.cast(format)); | ||
} | ||
|
||
@Override | ||
public ResourceLocation getName() { | ||
return location; | ||
} | ||
|
||
@Override | ||
public int getId() { | ||
return id; | ||
} | ||
}; | ||
SCORE_FORMAT_TYPE_MAP.put(location.toString(), type); | ||
SCORE_FORMAT_TYPE_ID_MAP.put((byte) id, type); | ||
return type; | ||
} | ||
|
||
public static @Nullable ScoreFormatType getById(ClientVersion version, int id) { | ||
return SCORE_FORMAT_TYPE_ID_MAP.get((byte) id); | ||
} | ||
|
||
public static @Nullable ScoreFormatType getByName(String name) { | ||
return getByName(new ResourceLocation(name)); | ||
} | ||
|
||
public static @Nullable ScoreFormatType getByName(ResourceLocation name) { | ||
return SCORE_FORMAT_TYPE_MAP.get(name.toString()); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
api/src/main/java/com/github/retrooper/packetevents/protocol/score/StyledScoreFormat.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,48 @@ | ||
/* | ||
* This file is part of packetevents - https://github.com/retrooper/packetevents | ||
* Copyright (C) 2024 retrooper and contributors | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.github.retrooper.packetevents.protocol.score; | ||
|
||
import net.kyori.adventure.text.Component; | ||
import net.kyori.adventure.text.format.Style; | ||
|
||
public final class StyledScoreFormat implements ScoreFormat { | ||
|
||
private final Style style; | ||
|
||
public StyledScoreFormat(Style style) { | ||
this.style = style; | ||
} | ||
|
||
@Override | ||
public Component format(int score) { | ||
return Component.text() | ||
.content(Integer.toString(score)) | ||
.style(this.style) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public ScoreFormatType getType() { | ||
return ScoreFormatTypes.STYLED; | ||
} | ||
|
||
public Style getStyle() { | ||
return this.style; | ||
} | ||
} |
Oops, something went wrong.