Skip to content

Commit

Permalink
refactor: close #190 & code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
IWareQ committed May 9, 2024
1 parent e9d6569 commit 1a081cb
Show file tree
Hide file tree
Showing 84 changed files with 947 additions and 1,100 deletions.
5 changes: 3 additions & 2 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public interface BlockState {

ItemStack toItemStack();

//TODO: 确认是否只需要实现BlockDefinition::getRuntimeId(), 现有实现较为复杂且低效
// TODO: Confirm if only need to implement BlockDefinition::getRuntimeId(), the existing implementation is quite complex and inefficient
default SimpleBlockDefinition toNetworkBlockDefinition() {
var statesBuilder = NbtMap.builder();
for (var propertyValue : getPropertyValues().values()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import org.allaymc.api.block.component.common.CustomBlockComponent;
import org.allaymc.api.block.property.type.BlockPropertyType;
import org.allaymc.api.blockentity.type.BlockEntityType;
import org.allaymc.api.utils.Identifier;
import org.allaymc.api.data.VanillaBlockId;
import org.allaymc.api.utils.Identifier;

import java.util.HashMap;
import java.util.List;
Expand Down
83 changes: 44 additions & 39 deletions Allay-API/src/main/java/org/allaymc/api/client/data/LoginData.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.google.gson.JsonObject;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import org.allaymc.api.client.skin.Image;
Expand All @@ -28,12 +29,15 @@
*
* @author LucGamesYT | daoge_cmd
*/
@ToString

@Getter
@Builder
@ToString
@EqualsAndHashCode
@AllArgsConstructor
public class LoginData {
private static final Gson GSON = new Gson();

private boolean xboxAuthenticated;
private String displayName;
private String xuid;
Expand Down Expand Up @@ -61,30 +65,33 @@ private void decodeChainData(List<String> chainData) {
}

for (String chain : chainData) {
JsonObject chainMap = decodeToken(chain);
if (chainMap == null) {
continue;
}
if (chainMap.has("extraData")) {
JsonObject extraData = (JsonObject) chainMap.get("extraData");
this.displayName = extraData.get("displayName").getAsString();
this.uuid = UUID.fromString(extraData.get("identity").getAsString());
this.xuid = extraData.get("XUID").getAsString();
var chainMap = decodeToken(chain);
if (chainMap != null) {
if (chainMap.has("extraData")) {
JsonObject extraData = (JsonObject) chainMap.get("extraData");
this.displayName = extraData.get("displayName").getAsString();
this.uuid = UUID.fromString(extraData.get("identity").getAsString());
this.xuid = extraData.get("XUID").getAsString();
}
this.identityPublicKey = chainMap.get("identityPublicKey").getAsString();
}
this.identityPublicKey = chainMap.get("identityPublicKey").getAsString();
}
}

private void decodeSkinData(String skinData) {

JsonObject skinMap = decodeToken(skinData);
if (skinMap.has("DeviceModel") && skinMap.has("DeviceId") &&
skinMap.has("ClientRandomId") && skinMap.has("DeviceOS") && skinMap.has("GuiScale")) {
String deviceModel = skinMap.get("DeviceModel").getAsString();
String deviceId = skinMap.get("DeviceId").getAsString();
long clientId = skinMap.get("ClientRandomId").getAsLong();
int deviceOS = skinMap.get("DeviceOS").getAsInt();
int uiProfile = skinMap.get("UIProfile").getAsInt();
var skinMap = decodeToken(skinData);
if (skinMap.has("DeviceModel") &&
skinMap.has("DeviceId") &&
skinMap.has("ClientRandomId") &&
skinMap.has("DeviceOS") &&
skinMap.has("GuiScale")
) {
var deviceModel = skinMap.get("DeviceModel").getAsString();
var deviceId = skinMap.get("DeviceId").getAsString();
var clientId = skinMap.get("ClientRandomId").getAsLong();
var deviceOS = skinMap.get("DeviceOS").getAsInt();
var uiProfile = skinMap.get("UIProfile").getAsInt();
this.deviceInfo = new DeviceInfo(deviceModel, deviceId, clientId, Device.getDevice(deviceOS), UIProfile.getById(uiProfile));
}

Expand Down Expand Up @@ -165,19 +172,17 @@ private void decodeSkinData(String skinData) {
}

private JsonObject decodeToken(String token) {
String[] tokenSplit = token.split("\\.");
if (tokenSplit.length < 2) {
throw new IllegalArgumentException("Invalid token length");
}
var tokenSplit = token.split("\\.");
if (tokenSplit.length < 2) throw new IllegalArgumentException("Invalid token length");
return GSON.fromJson(new String(Base64.getDecoder().decode(tokenSplit[1]), StandardCharsets.UTF_8), JsonObject.class);
}

private Image getImage(JsonObject skinMap, String name) {
if (skinMap.has(name + "Data")) {
byte[] skinImage = Base64.getDecoder().decode(skinMap.get(name + "Data").getAsString());
var skinImage = Base64.getDecoder().decode(skinMap.get(name + "Data").getAsString());
if (skinMap.has(name + "ImageHeight") && skinMap.has(name + "ImageWidth")) {
int width = skinMap.get(name + "ImageWidth").getAsInt();
int height = skinMap.get(name + "ImageHeight").getAsInt();
var width = skinMap.get(name + "ImageWidth").getAsInt();
var height = skinMap.get(name + "ImageHeight").getAsInt();
return new Image(width, height, skinImage);
} else {
return Image.getImage(skinImage);
Expand All @@ -187,28 +192,28 @@ private Image getImage(JsonObject skinMap, String name) {
}

private SkinAnimation getSkinAnimationData(JsonObject animationData) {
byte[] data = Base64.getDecoder().decode(animationData.get("Image").getAsString());
int width = animationData.get("ImageWidth").getAsInt();
int height = animationData.get("ImageHeight").getAsInt();
float frames = animationData.get("Frames").getAsFloat();
int type = animationData.get("Type").getAsInt();
int expression = animationData.get("AnimationExpression").getAsInt();
var data = Base64.getDecoder().decode(animationData.get("Image").getAsString());
var width = animationData.get("ImageWidth").getAsInt();
var height = animationData.get("ImageHeight").getAsInt();
var frames = animationData.get("Frames").getAsFloat();
var type = animationData.get("Type").getAsInt();
var expression = animationData.get("AnimationExpression").getAsInt();
return new SkinAnimation(new Image(width, height, data), type, frames, expression);
}

private PersonaPiece getPersonaPiece(JsonObject personaPiece) {
String pieceId = personaPiece.get("PieceId").getAsString();
String pieceType = personaPiece.get("PieceType").getAsString();
String packId = personaPiece.get("PackId").getAsString();
String productId = personaPiece.get("ProductId").getAsString();
boolean isDefault = personaPiece.get("IsDefault").getAsBoolean();
var pieceId = personaPiece.get("PieceId").getAsString();
var pieceType = personaPiece.get("PieceType").getAsString();
var packId = personaPiece.get("PackId").getAsString();
var productId = personaPiece.get("ProductId").getAsString();
var isDefault = personaPiece.get("IsDefault").getAsBoolean();
return new PersonaPiece(pieceId, pieceType, packId, productId, isDefault);
}

private PersonaPieceTint getPersonaPieceTint(JsonObject personaPiceTint) {
String pieceType = personaPiceTint.get("PieceType").getAsString();
var pieceType = personaPiceTint.get("PieceType").getAsString();
List<String> colors = new ArrayList<>();
for (JsonElement element : personaPiceTint.getAsJsonArray("Colors")) {
for (var element : personaPiceTint.getAsJsonArray("Colors")) {
colors.add(element.getAsString());
}
return new PersonaPieceTint(pieceType, colors);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package org.allaymc.api.component.interfaces;

import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.SneakyThrows;
import org.allaymc.api.utils.Identifier;
import lombok.ToString;
import org.allaymc.api.component.annotation.ComponentIdentifier;
import org.allaymc.api.utils.Identifier;

import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -43,12 +45,6 @@ static <P extends Component> Map<Identifier, ComponentProvider<? extends P>> toM
return map;
}

T provide(ComponentInitInfo info);

Class<?> getComponentClass();

@SneakyThrows

static Identifier findComponentIdentifier(Class<?> clazz) {
Identifier identifier = null;
while (identifier == null) {
Expand All @@ -60,7 +56,6 @@ static Identifier findComponentIdentifier(Class<?> clazz) {
}

@SneakyThrows

static Identifier findComponentIdentifierInCertainClass(Class<?> clazz) {
for (var field : clazz.getDeclaredFields()) {
if (field.isAnnotationPresent(ComponentIdentifier.class) && Identifier.class == field.getType() && isStatic(field.getModifiers())) {
Expand All @@ -71,12 +66,16 @@ static Identifier findComponentIdentifierInCertainClass(Class<?> clazz) {
return null;
}

@SneakyThrows
T provide(ComponentInitInfo info);

Class<?> getComponentClass();

default Identifier findComponentIdentifier() {
return findComponentIdentifier(getComponentClass());
}

@ToString
@EqualsAndHashCode
@AllArgsConstructor
class SimpleComponentProvider<T extends Component, I extends ComponentInitInfo> implements ComponentProvider<T> {
private Function<I, T> provider;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.allaymc.api.container;

import lombok.NoArgsConstructor;
import org.allaymc.api.utils.exception.ContainerException;
import org.cloudburstmc.protocol.bedrock.data.inventory.ContainerSlotType;
import org.jetbrains.annotations.UnmodifiableView;
Expand All @@ -13,15 +14,13 @@
*
* @author daoge_cmd
*/
@NoArgsConstructor
public class BaseContainerHolder implements ContainerHolder {
protected final Map<FullContainerType<?>, Container> typeToContainer = new HashMap<>();
protected final Map<ContainerSlotType, FullContainerType<?>> slotTypeToFullType = new HashMap<>();

public BaseContainerHolder() {
}

public BaseContainerHolder(Container... containers) {
for (Container container : containers) {
for (var container : containers) {
this.addContainer(container);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.allaymc.api.entity.attribute;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.HashMap;
import java.util.Map;

Expand All @@ -8,6 +11,8 @@
*
* @author JukeboxMC | daoge_cmd
*/
@Getter
@AllArgsConstructor
public enum AttributeType {

//Base Entity
Expand Down Expand Up @@ -46,18 +51,11 @@ public enum AttributeType {
private final float maxValue;
private final float defaultValue;

AttributeType(String key, float minValue, float maxValue, float defaultValue) {
this.key = key;
this.minValue = minValue;
this.maxValue = maxValue;
this.defaultValue = defaultValue;
public static AttributeType byKey(String key) {
return KEY_LOOK_UP.get(key);
}

public Attribute newAttributeInstance() {
return new Attribute(this.key, this.minValue, this.maxValue, this.defaultValue, this.defaultValue);
}

public static AttributeType byKey(String key) {
return KEY_LOOK_UP.get(key);
}
}
18 changes: 9 additions & 9 deletions Allay-API/src/main/java/org/allaymc/api/i18n/I18n.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ public interface I18n {

LangCode FALLBACK_LANG = LangCode.en_US;

String VANILLA_LANG_NAMESPACE = "minecraft";

ApiInstanceHolder<I18n> I18N = ApiInstanceHolder.create();

static I18n get() {
return I18N.get();
}

String VANILLA_LANG_NAMESPACE = "minecraft";
static boolean isValidKeyCharacter(char character) {
return character == '_' || character == '-' || character >= 'a' && character <= 'z' || character >= 'A' && character <= 'Z' || character >= '0' && character <= '9' || character == '.' || character == ':';
}

String tr(LangCode langCode, @MayContainTrKey String tr, String... args);

Expand All @@ -41,13 +45,9 @@ default String tr(@MayContainTrKey String tr) {
return tr(getDefaultLangCode(), tr);
}

void setDefaultLangCode(LangCode langCode);

LangCode getDefaultLangCode();

record KeyInfo(int startIndex, int endIndex, int colonIndex, String key, boolean hasStarter) {
public static final KeyInfo EMPTY = new KeyInfo(-1, -1, -1, null, false);
}
void setDefaultLangCode(LangCode langCode);

KeyInfo findI18nKey(@MayContainTrKey String str);

Expand All @@ -73,7 +73,7 @@ default Pair<String, Boolean> toClientFriendlyStyle0(LangCode langCode, @MayCont
if (VANILLA_LANG_NAMESPACE.equals(namespace)) {
String left;
if (keyInfo.hasStarter) {
// 保留 '%'
// Keep '%'
left = tr.substring(0, keyInfo.startIndex + 1);
} else {
left = "";
Expand All @@ -85,7 +85,7 @@ default Pair<String, Boolean> toClientFriendlyStyle0(LangCode langCode, @MayCont

}

static boolean isValidKeyCharacter(char character) {
return character == '_' || character == '-' || character >= 'a' && character <= 'z' || character >= 'A' && character <= 'Z' || character >= '0' && character <= '9' || character == '.' || character == ':';
record KeyInfo(int startIndex, int endIndex, int colonIndex, String key, boolean hasStarter) {
public static final KeyInfo EMPTY = new KeyInfo(-1, -1, -1, null, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static ItemAttributes fromJson(String json) {
return JSONUtils.from(json, ItemAttributes.class);
}

//TODO: test
// TODO: test
public static ItemAttributes fromNBT(NbtMap nbt) {
return ItemAttributes
.builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package org.allaymc.api.item.type;

import org.allaymc.api.block.type.BlockType;
import org.allaymc.api.utils.Identified;
import org.allaymc.api.utils.Identifier;
import org.allaymc.api.component.interfaces.ComponentProvider;
import org.allaymc.api.item.ItemStack;
import org.allaymc.api.item.component.ItemComponent;
import org.allaymc.api.item.init.ItemStackInitInfo;
import org.allaymc.api.item.init.SimpleItemStackInitInfo;
import org.allaymc.api.item.tag.ItemTag;
import org.allaymc.api.utils.Identified;
import org.allaymc.api.utils.Identifier;
import org.cloudburstmc.protocol.bedrock.data.definitions.ItemDefinition;
import org.cloudburstmc.protocol.bedrock.data.definitions.SimpleItemDefinition;
import org.jetbrains.annotations.Unmodifiable;
Expand Down Expand Up @@ -43,7 +43,6 @@ default ItemDefinition toNetworkDefinition() {
return new SimpleItemDefinition(getIdentifier().toString(), getRuntimeId(), false);
}


@Unmodifiable
Set<ItemTag> getItemTags();

Expand Down
Loading

0 comments on commit 1a081cb

Please sign in to comment.