Skip to content

Commit

Permalink
Attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Tofaa2 committed May 23, 2024
1 parent 6e90d0d commit dcdbe85
Show file tree
Hide file tree
Showing 20 changed files with 302 additions and 74 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

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

102 changes: 57 additions & 45 deletions .idea/workspace.xml

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

7 changes: 5 additions & 2 deletions api/src/main/java/me/tofaa/entitylib/EntityLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ public static void init(Platform<?> platform, APIConfig settings) {
if (api.getSettings().shouldCheckForUpdate()) {
try {
if (api.getSettings().isDebugMode()) {
platform.getLogger().log(Level.CONFIG, "Checking for updates...");
platform.getLogger().log(Level.INFO, "Checking for updates...");
}
if (api.getSettings().requiresUpdate()) {
platform.getLogger().log(Level.WARNING, "You are using an outdated version of EntityLib. Please take a look at the Github releases page.");
}
else {
platform.getLogger().log(Level.INFO, "No EntityLib updates found.");
}

} catch (IOException e) {
platform.getLogger().log(Level.WARNING, e, () -> "EntityLib failed to check for updates.");
Expand All @@ -43,6 +46,6 @@ public static Platform<?> getPlatform() {
}

public static String getVersion() {
return "1.2.0-SNAPSHOT";
return "2.1.2-SNAPSHOT";
}
}
7 changes: 6 additions & 1 deletion api/src/main/java/me/tofaa/entitylib/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ public interface Platform<P> {

/**
* Sets up the API for the platform. This method should be called automatically by the platform. Don't call it yourself.
* @param settings
*/
void setupApi(@NotNull APIConfig settings);

Expand All @@ -83,4 +82,10 @@ public interface Platform<P> {

@NotNull P getHandle();

default void logIfNeeded(String message) {
if (getAPI().getSettings().isDebugMode()) {
getLogger().info(message);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
@ApiStatus.Internal
@SuppressWarnings("unused")
public final class MetaOffsetConverter {
private MetaOffsetConverter() {

}
private MetaOffsetConverter() {}

public static final class EntityMetaOffsets {
public static byte airTicksOffset() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package me.tofaa.entitylib.storage;

import me.tofaa.entitylib.wrapper.WrapperEntity;

import java.nio.ByteBuffer;

public class ByteEntitySerializer implements EntitySerializer<ByteBuffer, ByteBuffer> {
@Override
public WrapperEntity read(ByteBuffer reader) {
return null;
}

@Override
public void write(ByteBuffer writer, WrapperEntity wrapper) {

}
}
16 changes: 16 additions & 0 deletions api/src/main/java/me/tofaa/entitylib/storage/EntitySerializer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package me.tofaa.entitylib.storage;

import me.tofaa.entitylib.wrapper.WrapperEntity;

/**
* An interface that represents a way to serialize and deserialize {@link WrapperEntity} and its subclasses.
* @param <R> the reader generic
* @param <W> the writer generic
*/
public interface EntitySerializer<R, W> {

WrapperEntity read(R reader);

void write(W writer, WrapperEntity wrapper);

}
14 changes: 14 additions & 0 deletions api/src/main/java/me/tofaa/entitylib/storage/EntityStorage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package me.tofaa.entitylib.storage;

import me.tofaa.entitylib.wrapper.WrapperEntity;

import java.util.Collection;

public interface EntityStorage {


Collection<WrapperEntity> readAll();

void writeAll(Collection<WrapperEntity> entities);

}
18 changes: 18 additions & 0 deletions api/src/main/java/me/tofaa/entitylib/storage/FSEntityStorage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package me.tofaa.entitylib.storage;

import me.tofaa.entitylib.wrapper.WrapperEntity;

import java.util.Collection;
import java.util.Collections;

public class FSEntityStorage implements EntityStorage{
@Override
public Collection<WrapperEntity> readAll() {
return Collections.emptyList();
}

@Override
public void writeAll(Collection<WrapperEntity> entities) {

}
}
9 changes: 2 additions & 7 deletions api/src/main/java/me/tofaa/entitylib/utils/GithubUpdater.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@ public GithubUpdater(String org, String repo, String currentVersion) {
}

@Blocking
public boolean isLatestVersion() {
public boolean isLatestVersion() throws IOException {
String latest = getLatestVersion();
return latest != null && latest.equals(currentVersion);
}


@Blocking
public String getLatestVersion() {
try {
public String getLatestVersion() throws IOException {
URL url = new URL("https://api.github.com/repos/" + org + "/" + repo + "/releases/latest");
URLConnection connection = url.openConnection();
connection.addRequestProperty("User-Agent", "Mozilla/5.0");
Expand All @@ -45,10 +44,6 @@ public String getLatestVersion() {
return json.get("name").getAsString();
}
throw new IOException("Could not find name attribute in github api fetch");
}
catch (IOException e) {
throw new RuntimeException(e);
}
}

public String getCurrentVersion() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package me.tofaa.entitylib.wrapper;

import com.github.retrooper.packetevents.protocol.entity.data.EntityData;
import com.github.retrooper.packetevents.protocol.entity.data.EntityDataType;
import com.github.retrooper.packetevents.protocol.entity.data.EntityDataTypes;
import com.github.retrooper.packetevents.protocol.entity.pose.EntityPose;
import com.github.retrooper.packetevents.protocol.entity.type.EntityType;
import com.github.retrooper.packetevents.protocol.player.User;
import com.github.retrooper.packetevents.protocol.world.Location;
Expand Down Expand Up @@ -284,7 +280,7 @@ public EntityMeta getEntityMeta() {
return entityMeta;
}

public UUID getUuid() {
public @NotNull UUID getUuid() {
return uuid;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package me.tofaa.entitylib.wrapper;

import com.github.retrooper.packetevents.protocol.attribute.Attribute;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerUpdateAttributes;
import org.w3c.dom.Attr;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Consumer;

public final class WrapperEntityAttributes {


public static final WrapperPlayServerUpdateAttributes.PropertyModifier.Operation ADDITION = WrapperPlayServerUpdateAttributes.PropertyModifier.Operation.ADDITION;
public static final WrapperPlayServerUpdateAttributes.PropertyModifier.Operation MULTIPLY_BASE = WrapperPlayServerUpdateAttributes.PropertyModifier.Operation.MULTIPLY_BASE;
public static final WrapperPlayServerUpdateAttributes.PropertyModifier.Operation MULTIPLY_TOTAL = WrapperPlayServerUpdateAttributes.PropertyModifier.Operation.MULTIPLY_TOTAL;

private final WrapperEntity entity;
private final List<WrapperPlayServerUpdateAttributes.Property> properties;

public WrapperEntityAttributes(WrapperEntity entity) {
this.entity = entity;
this.properties = new CopyOnWriteArrayList<>();
}

public void setAttribute(Attribute attribute, double value, List<WrapperPlayServerUpdateAttributes.PropertyModifier> modifiers) {
this.properties.stream()
.filter(property -> property.getAttribute() == attribute)
.findFirst().ifPresent(properties::remove);
this.properties.add(new WrapperPlayServerUpdateAttributes.Property(attribute, value, modifiers));
refresh();
}

public void setAttribute(Attribute attribute, double value, WrapperPlayServerUpdateAttributes.PropertyModifier modifier) {
setAttribute(attribute, value, Collections.singletonList(modifier));
}

public void setAttribute(Attribute attribute, double value) {
setAttribute(attribute, value, Collections.emptyList());
}

public List<WrapperPlayServerUpdateAttributes.Property> getProperties() {
return new ArrayList<>(properties);
}

public void forEach(Consumer<WrapperPlayServerUpdateAttributes.Property> action) {
properties.forEach(action);
}

public void clear() {
properties.clear();
refresh();
}

public void removeAttribute(Attribute attribute, WrapperPlayServerUpdateAttributes.PropertyModifier modifier) {
this.properties.stream()
.filter(property -> property.getAttribute() == attribute)
.findFirst().ifPresent(property -> {
property.getModifiers().remove(modifier);
if (property.getModifiers().isEmpty()) {
properties.remove(property);
}
});
refresh();
}

public void removeAttribute(Attribute attribute) {
this.properties.stream()
.filter(property -> property.getAttribute() == attribute)
.findFirst().ifPresent(properties::remove);
refresh();
}


public void refresh() {
entity.sendPacketToViewers(createPacket());
}

public WrapperPlayServerUpdateAttributes createPacket() {
return new WrapperPlayServerUpdateAttributes(entity.getEntityId(), properties);
}

}
Loading

0 comments on commit dcdbe85

Please sign in to comment.