Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions api-internal/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@
</properties>

<dependencies>
<dependency>
<groupId>org.spongepowered</groupId>
<artifactId>configurate-core</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.spongepowered</groupId>
<artifactId>configurate-yaml</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
Expand All @@ -33,6 +43,20 @@
<version>1.18.30</version>
<scope>provided</scope>
</dependency>

<!-- Testing dependencies -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.11.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.18.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package at.pavlov.internal.adapters;

import at.pavlov.internal.Key;
import at.pavlov.internal.projectile.definition.CustomProjectileDefinition;
import org.spongepowered.configurate.serialize.TypeSerializerCollection;

public class CannonSerializingManager {

Check warning on line 7 in api-internal/src/main/java/at/pavlov/internal/adapters/CannonSerializingManager.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Add a private constructor to hide the implicit public one.

See more on https://sonarcloud.io/project/issues?id=Intybyte_Cannons&issues=AZsTwLO-VuT3WVuLrf--&open=AZsTwLO-VuT3WVuLrf--&pullRequest=50
public static final TypeSerializerCollection serializerCollection = TypeSerializerCollection.defaults().childBuilder()
.register(Key.class, new KeySerializer())
.register(CustomProjectileDefinition.class, new CustomProjectileDefinitionSerializer())
.build();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package at.pavlov.internal.adapters;

import at.pavlov.internal.Key;
import at.pavlov.internal.key.registries.Registries;
import at.pavlov.internal.projectile.definition.CustomProjectileDefinition;
import at.pavlov.internal.projectile.definition.ProjectilePhysics;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.serialize.SerializationException;
import org.spongepowered.configurate.serialize.TypeSerializer;

import java.lang.reflect.Type;

public class CustomProjectileDefinitionSerializer implements TypeSerializer<CustomProjectileDefinition> {

@Override
public CustomProjectileDefinition deserialize(@NotNull Type type, ConfigurationNode node) throws SerializationException {
// Use the node's key as the projectile key
Key key = Key.from(node.key().toString()); // top-level key
Key entityKey = Key.from(node.node("entity").getString("SNOWBALL"));

// Get default projectile physics if entityKey not registered
ProjectilePhysics dpd = Registries.DEFAULT_PROJECTILE_DEFINITION_REGISTRY.of(entityKey);
if (dpd == null) {
dpd = ProjectilePhysics.DEFAULT;
}

// Build the CustomProjectileDefinition
return CustomProjectileDefinition.builder()
.key(key)
.entityKey(entityKey)
.constantAcceleration(node.node("constantAcceleration").get(Double.class, dpd.getConstantAcceleration()))
.gravity(node.node("gravity").getDouble(dpd.getGravity()))
.drag(node.node("drag").getDouble(dpd.getDrag()))
.waterDrag(node.node("waterDrag").getDouble(dpd.getWaterDrag()))
.glowing(node.node("glowing").getBoolean(false))
.onFire(node.node("onFire").getBoolean(false))
.charged(node.node("charged").getBoolean(false))
.critical(node.node("critical").getBoolean(false))
.material(Key.from(node.node("material").getString("SNOWBALL")))
.customModelData(node.node("customModelData").get(Integer.class, (Integer) null))
.build();
}

@Override
public void serialize(@NotNull Type type, CustomProjectileDefinition obj, @NotNull ConfigurationNode node) throws SerializationException {
// Not needed, can throw UnsupportedOperationException if you never serialize
throw new UnsupportedOperationException("Serialization not supported for this deserializer");
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package at.pavlov.internal.adapters;

import at.pavlov.internal.Key;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.serialize.SerializationException;
import org.spongepowered.configurate.serialize.TypeSerializer;

import java.lang.reflect.Type;

public class KeySerializer implements TypeSerializer<Key> {

@Override
public Key deserialize(@NotNull Type type, ConfigurationNode node) throws SerializationException {
String value = node.getString();
if (value == null || !value.contains(":")) {
throw new SerializationException("Invalid Key format, expected namespace:key");
}
String[] parts = value.split(":", 2);
return new Key(parts[0], parts[1]);
}

@Override
public void serialize(@NotNull Type type, Key obj, @NotNull ConfigurationNode node) throws SerializationException {
if (obj == null) return;
node.set(obj.full());
}
}
Original file line number Diff line number Diff line change
@@ -1,56 +1,33 @@
package at.pavlov.internal.projectile.definition;

import at.pavlov.internal.Key;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.*;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.configurate.objectmapping.ConfigSerializable;

@Getter
@Builder
@EqualsAndHashCode
@ConfigSerializable
@NoArgsConstructor
@AllArgsConstructor
public class CustomProjectileDefinition implements ProjectilePhysics {
private final Key key; // for the name of the custom projectile definition
private final Key entityKey;
private Key key; // for the name of the custom projectile definition
private Key entityKey;

private final Double constantAcceleration;
private Double constantAcceleration;

private final double gravity;
private final double drag;
private final double waterDrag;
private double gravity;
private double drag;
private double waterDrag;

private final boolean glowing;
private boolean glowing;

private final boolean onFire; // visual fire for projectile
private final boolean charged; // works for wither skeletons
private final boolean critical; // for arrows and tridents
private boolean onFire; // visual fire for projectile
private boolean charged; // works for wither skeletons
private boolean critical; // for arrows and tridents

//for throwable projectiles only
private final @Nullable Key material;
private final @Nullable Integer customModelData;

/*
@Override
public double getGravity() {
return fromKey().getGravity();
}

@Override
public double getDrag() {
return fromKey().getDrag();
}

@Override
public double getWaterDrag() {
return fromKey().getWaterDrag();
}

private @NotNull ProjectilePhysics fromKey() {
DefaultProjectileDefinition value = Registries.DEFAULT_PROJECTILE_DEFINITION_REGISTRY.of(entityKey);
if (value == null) {
return ProjectilePhysics.DEFAULT;
}

return value;
}*/
private @Nullable Key material;
private @Nullable Integer customModelData;
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package at.pavlov.internal.projectile.definition;

import at.pavlov.internal.Key;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.*;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.configurate.objectmapping.ConfigSerializable;

@Getter
@Builder
@ConfigSerializable
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
public class DefaultProjectileDefinition implements ProjectilePhysics {
private final @NotNull Key key; // for the entity type
private final Double constantAcceleration;
private final double gravity;
private final double drag;
private final double waterDrag;
private @NotNull Key key; // for the entity type
private Double constantAcceleration;
private double gravity;
private double drag;
private double waterDrag;

@Override
public Key getEntityKey() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package adapters;

import org.junit.jupiter.api.Test;

public class CustomProjectileDefinitionSerializerTest {

Check warning on line 5 in api-internal/src/test/java/adapters/CustomProjectileDefinitionSerializerTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this 'public' modifier.

See more on https://sonarcloud.io/project/issues?id=Intybyte_Cannons&issues=AZsT2ql3ZsJX7UuJu9Pl&open=AZsT2ql3ZsJX7UuJu9Pl&pullRequest=50

@Test
void deserialize() throws Exception {

Check failure on line 8 in api-internal/src/test/java/adapters/CustomProjectileDefinitionSerializerTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Add at least one assertion to this test case.

See more on https://sonarcloud.io/project/issues?id=Intybyte_Cannons&issues=AZsT2ql3ZsJX7UuJu9Pk&open=AZsT2ql3ZsJX7UuJu9Pk&pullRequest=50
// todo: make testing

Check warning on line 9 in api-internal/src/test/java/adapters/CustomProjectileDefinitionSerializerTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Complete the task associated to this TODO comment.

See more on https://sonarcloud.io/project/issues?id=Intybyte_Cannons&issues=AZsT2ql3ZsJX7UuJu9Pm&open=AZsT2ql3ZsJX7UuJu9Pm&pullRequest=50
}
}