Implement Player Client Options API

== AT ==
public net.minecraft.world.entity.player.Player DATA_PLAYER_MODE_CUSTOMISATION
public net.minecraft.server.level.ServerPlayer particleStatus
This commit is contained in:
MiniDigger | Martin
2020-01-20 21:38:15 +01:00
parent 3d63d68ecb
commit 57802a490d
4 changed files with 169 additions and 27 deletions

View File

@@ -0,0 +1,74 @@
package com.destroystokyo.paper;
import com.google.common.base.Objects;
import java.util.StringJoiner;
public class PaperSkinParts implements SkinParts {
private final int raw;
public PaperSkinParts(int raw) {
this.raw = raw;
}
public boolean hasCapeEnabled() {
return (raw & 1) == 1;
}
public boolean hasJacketEnabled() {
return (raw >> 1 & 1) == 1;
}
public boolean hasLeftSleeveEnabled() {
return (raw >> 2 & 1) == 1;
}
public boolean hasRightSleeveEnabled() {
return (raw >> 3 & 1) == 1;
}
public boolean hasLeftPantsEnabled() {
return (raw >> 4 & 1) == 1;
}
public boolean hasRightPantsEnabled() {
return (raw >> 5 & 1) == 1;
}
public boolean hasHatsEnabled() {
return (raw >> 6 & 1) == 1;
}
@Override
public int getRaw() {
return raw;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PaperSkinParts that = (PaperSkinParts) o;
return raw == that.raw;
}
@Override
public int hashCode() {
return Objects.hashCode(raw);
}
@Override
public String toString() {
return new StringJoiner(", ", PaperSkinParts.class.getSimpleName() + "[", "]")
.add("raw=" + raw)
.add("cape=" + hasCapeEnabled())
.add("jacket=" + hasJacketEnabled())
.add("leftSleeve=" + hasLeftSleeveEnabled())
.add("rightSleeve=" + hasRightSleeveEnabled())
.add("leftPants=" + hasLeftPantsEnabled())
.add("rightPants=" + hasRightPantsEnabled())
.add("hats=" + hasHatsEnabled())
.toString();
}
}

View File

@@ -658,6 +658,30 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
connection.disconnect(message == null ? net.kyori.adventure.text.Component.empty() : message);
}
}
@Override
public <T> T getClientOption(com.destroystokyo.paper.ClientOption<T> type) {
if (com.destroystokyo.paper.ClientOption.SKIN_PARTS == type) {
return type.getType().cast(new com.destroystokyo.paper.PaperSkinParts(this.getHandle().getEntityData().get(net.minecraft.world.entity.player.Player.DATA_PLAYER_MODE_CUSTOMISATION)));
} else if (com.destroystokyo.paper.ClientOption.CHAT_COLORS_ENABLED == type) {
return type.getType().cast(this.getHandle().canChatInColor());
} else if (com.destroystokyo.paper.ClientOption.CHAT_VISIBILITY == type) {
return type.getType().cast(com.destroystokyo.paper.ClientOption.ChatVisibility.valueOf(this.getHandle().getChatVisibility().name()));
} else if (com.destroystokyo.paper.ClientOption.LOCALE == type) {
return type.getType().cast(this.getLocale());
} else if (com.destroystokyo.paper.ClientOption.MAIN_HAND == type) {
return type.getType().cast(this.getMainHand());
} else if (com.destroystokyo.paper.ClientOption.VIEW_DISTANCE == type) {
return type.getType().cast(this.getClientViewDistance());
} else if (com.destroystokyo.paper.ClientOption.TEXT_FILTERING_ENABLED == type) {
return type.getType().cast(this.getHandle().isTextFilteringEnabled());
} else if (com.destroystokyo.paper.ClientOption.ALLOW_SERVER_LISTINGS == type) {
return type.getType().cast(this.getHandle().allowsListing());
} else if (com.destroystokyo.paper.ClientOption.PARTICLE_VISIBILITY == type) {
return type.getType().cast(com.destroystokyo.paper.ClientOption.ParticleVisibility.valueOf(this.getHandle().particleStatus.name()));
}
throw new RuntimeException("Unknown settings type");
}
// Paper end
@Override

View File

@@ -0,0 +1,25 @@
package io.papermc.paper.world;
import com.destroystokyo.paper.ClientOption;
import net.minecraft.server.level.ParticleStatus;
import net.minecraft.world.entity.player.ChatVisiblity;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class TranslationKeyTest {
@Test
public void testChatVisibilityKeys() {
for (ClientOption.ChatVisibility chatVisibility : ClientOption.ChatVisibility.values()) {
if (chatVisibility == ClientOption.ChatVisibility.UNKNOWN) continue;
Assertions.assertEquals(ChatVisiblity.valueOf(chatVisibility.name()).getKey(), chatVisibility.translationKey(), chatVisibility + "'s translation key doesn't match");
}
}
@Test
public void testParticleVisibilityKeys() {
for (ClientOption.ParticleVisibility particleVisibility : ClientOption.ParticleVisibility.values()) {
Assertions.assertEquals(ParticleStatus.valueOf(particleVisibility.name()).getKey(), particleVisibility.translationKey(), particleVisibility + "'s translation key doesn't match");
}
}
}