Skip to content

Commit

Permalink
Merge pull request #643 from booky10/fix/scoreboard-wrappers
Browse files Browse the repository at this point in the history
Fix scoreboard wrappers for 1.20.3/1.20.4
  • Loading branch information
retrooper authored Jan 4, 2024
2 parents f9b3786 + 6049e37 commit e7dcb4c
Show file tree
Hide file tree
Showing 11 changed files with 909 additions and 440 deletions.
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;
}
}
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;
}
}
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();
}
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);
}
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());
}
}
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;
}
}
Loading

0 comments on commit e7dcb4c

Please sign in to comment.