Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] EntityData (mc-platform alternative DataWatcher) #11

Draft
wants to merge 2 commits into
base: v5
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.fairyproject.mc.entity;

import org.apache.commons.lang3.tuple.Pair;
import org.jetbrains.annotations.Nullable;

import java.util.Set;

public interface EntityData {

static EntityData createImpl() {
return new EntityDataImpl();
}

boolean add(Item<?> item);

Item<?> get(int index);

Item<?> remove(int index);

boolean isEmpty();

Set<Pair<Integer, Item>> all();

void clear();

<T> Item<T> define(int index, EntityDataSerializer<T> dataSerializer);

interface Item<T> {

int index();

EntityDataSerializer<T> serializer();

@Nullable
T getObject();

void setObject(@Nullable T t);

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package io.fairyproject.mc.entity;

import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.tuple.Pair;
import org.jetbrains.annotations.Nullable;

import java.util.Set;
import java.util.stream.Collectors;

public class EntityDataImpl implements EntityData {

private final Int2ObjectOpenHashMap<EntityData.Item<?>> items;

public EntityDataImpl() {
this.items = new Int2ObjectOpenHashMap<>();
}

@Override
public boolean add(Item<?> item) {
return this.items.put(item.index(), item) != item;
}

@Override
public Item<?> get(int index) {
return this.items.get(index);
}

@Override
public Item<?> remove(int index) {
return this.items.remove(index);
}

@Override
public boolean isEmpty() {
return this.items.isEmpty();
}

@Override
public Set<Pair<Integer, Item>> all() {
return this.items.int2ObjectEntrySet()
.stream()
.map(entry -> Pair.of(entry.getIntKey(), (Item)entry.getValue()))
.collect(Collectors.toSet());
}

@Override
public void clear() {
this.items.clear();
}

@Override
public <T> Item<T> define(int index, EntityDataSerializer<T> dataSerializer) {
return new ItemImpl<>(index, dataSerializer);
}

@RequiredArgsConstructor
public static class ItemImpl<T> implements EntityData.Item<T> {

private final int index;
private final EntityDataSerializer<T> serializer;
@Nullable
private T object;

@Override
public int index() {
return this.index;
}

@Override
public EntityDataSerializer<T> serializer() {
return this.serializer;
}

@Override
@Nullable
public T getObject() {
return this.object;
}

@Override
public void setObject(@Nullable T t) {
this.object = t;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.fairyproject.mc.entity;

import io.fairyproject.mc.protocol.netty.FriendlyByteBuf;

public abstract class EntityDataSerializer<T> {

public static EntityDataSerializer<Double> DOUBLE = new EntityDataSerializer<Double>() {
@Override
public Double read(FriendlyByteBuf byteBuf) {
return byteBuf.readDouble();
}

@Override
public void write(FriendlyByteBuf byteBuf, Double obj) {
byteBuf.writeDouble(obj);
}
};

public T read(FriendlyByteBuf byteBuf) {
throw new UnsupportedOperationException();
}

public void write(FriendlyByteBuf byteBuf, T obj) {
throw new UnsupportedOperationException();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.fairytest.mc;

import io.fairyproject.mc.entity.EntityData;
import io.fairyproject.mc.entity.EntityDataSerializer;
import org.apache.commons.lang3.tuple.Pair;
import org.junit.Assert;
import org.junit.Test;

import java.util.Collections;
import java.util.Set;

public class EntityDataTest {

@Test
public void basicOperation() {
EntityData entityData = EntityData.createImpl();
EntityData.Item<Double> item = entityData.define(2, EntityDataSerializer.DOUBLE);
item.setObject(20.0D);

// test empty
Assert.assertTrue(entityData.isEmpty());

// test adding
Assert.assertTrue(entityData.add(item));
Assert.assertEquals(item, entityData.get(2));

// test non-empty
Assert.assertFalse(entityData.isEmpty());

// test not exists index
Assert.assertNull(entityData.get(1));

// test all
Set<Pair<Integer, EntityData.Item>> all = entityData.all();
Set<Pair<Integer, EntityData.Item>> expected = Collections.singleton(Pair.of(2, item));
Assert.assertEquals(expected, all);

// test removing
Assert.assertEquals(item, entityData.remove(2));
Assert.assertNull(entityData.get(2));

// test clear
entityData.add(item);
entityData.clear();
Assert.assertTrue(entityData.isEmpty());
}

}