Skip to content

Commit

Permalink
Revert "refactor: close #190 & code cleanup"
Browse files Browse the repository at this point in the history
This reverts commit 1a081cb.
  • Loading branch information
smartcmd committed May 10, 2024
1 parent 1a081cb commit c9d619c
Show file tree
Hide file tree
Showing 84 changed files with 1,100 additions and 947 deletions.
5 changes: 2 additions & 3 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: Confirm if only need to implement BlockDefinition::getRuntimeId(), the existing implementation is quite complex and inefficient
//TODO: 确认是否只需要实现BlockDefinition::getRuntimeId(), 现有实现较为复杂且低效
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.data.VanillaBlockId;
import org.allaymc.api.utils.Identifier;
import org.allaymc.api.data.VanillaBlockId;

import java.util.HashMap;
import java.util.List;
Expand Down
83 changes: 39 additions & 44 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,7 +6,6 @@
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 @@ -29,15 +28,12 @@
*
* @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 @@ -65,33 +61,30 @@ private void decodeChainData(List<String> chainData) {
}

for (String chain : chainData) {
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();
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();
}
this.identityPublicKey = chainMap.get("identityPublicKey").getAsString();
}
}

private void decodeSkinData(String skinData) {

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();
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();
this.deviceInfo = new DeviceInfo(deviceModel, deviceId, clientId, Device.getDevice(deviceOS), UIProfile.getById(uiProfile));
}

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

private JsonObject decodeToken(String token) {
var tokenSplit = token.split("\\.");
if (tokenSplit.length < 2) throw new IllegalArgumentException("Invalid token length");
String[] 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")) {
var skinImage = Base64.getDecoder().decode(skinMap.get(name + "Data").getAsString());
byte[] skinImage = Base64.getDecoder().decode(skinMap.get(name + "Data").getAsString());
if (skinMap.has(name + "ImageHeight") && skinMap.has(name + "ImageWidth")) {
var width = skinMap.get(name + "ImageWidth").getAsInt();
var height = skinMap.get(name + "ImageHeight").getAsInt();
int width = skinMap.get(name + "ImageWidth").getAsInt();
int height = skinMap.get(name + "ImageHeight").getAsInt();
return new Image(width, height, skinImage);
} else {
return Image.getImage(skinImage);
Expand All @@ -192,28 +187,28 @@ private Image getImage(JsonObject skinMap, String name) {
}

private SkinAnimation getSkinAnimationData(JsonObject animationData) {
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();
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();
return new SkinAnimation(new Image(width, height, data), type, frames, expression);
}

private PersonaPiece getPersonaPiece(JsonObject personaPiece) {
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();
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();
return new PersonaPiece(pieceId, pieceType, packId, productId, isDefault);
}

private PersonaPieceTint getPersonaPieceTint(JsonObject personaPiceTint) {
var pieceType = personaPiceTint.get("PieceType").getAsString();
String pieceType = personaPiceTint.get("PieceType").getAsString();
List<String> colors = new ArrayList<>();
for (var element : personaPiceTint.getAsJsonArray("Colors")) {
for (JsonElement 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,12 +1,10 @@
package org.allaymc.api.component.interfaces;

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

import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -45,6 +43,12 @@ 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 @@ -56,6 +60,7 @@ 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 @@ -66,16 +71,12 @@ static Identifier findComponentIdentifierInCertainClass(Class<?> clazz) {
return null;
}

T provide(ComponentInitInfo info);

Class<?> getComponentClass();
@SneakyThrows

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,6 +1,5 @@
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 @@ -14,13 +13,15 @@
*
* @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 (var container : containers) {
for (Container container : containers) {
this.addContainer(container);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package org.allaymc.api.entity.attribute;

import lombok.AllArgsConstructor;
import lombok.Getter;

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

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

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

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

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,17 +13,13 @@ 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();
}

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

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

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

void setDefaultLangCode(LangCode langCode);

LangCode getDefaultLangCode();

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

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

}

record KeyInfo(int startIndex, int endIndex, int colonIndex, String key, boolean hasStarter) {
public static final KeyInfo EMPTY = new KeyInfo(-1, -1, -1, null, false);
static boolean isValidKeyCharacter(char character) {
return character == '_' || character == '-' || character >= 'a' && character <= 'z' || character >= 'A' && character <= 'Z' || character >= '0' && character <= '9' || character == '.' || character == ':';
}
}
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,6 +43,7 @@ default ItemDefinition toNetworkDefinition() {
return new SimpleItemDefinition(getIdentifier().toString(), getRuntimeId(), false);
}


@Unmodifiable
Set<ItemTag> getItemTags();

Expand Down
Loading

0 comments on commit c9d619c

Please sign in to comment.