Skip to content

Commit

Permalink
Start refactoring entity data types
Browse files Browse the repository at this point in the history
  • Loading branch information
booky10 committed Nov 25, 2024
1 parent d4b4b24 commit 025fac7
Show file tree
Hide file tree
Showing 8 changed files with 435 additions and 270 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package com.github.retrooper.packetevents.protocol.entity.data;

@Deprecated
public class EntityData {
private int index;
private EntityDataType<?> type;
Expand Down Expand Up @@ -52,4 +53,4 @@ public Object getValue() {
public void setValue(Object value) {
this.value = value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,29 @@
import java.util.function.BiConsumer;
import java.util.function.Function;

public class EntityDataType<T> {
private final String name;
private final int[] ids;
private final Function<PacketWrapper<?>, T> dataDeserializer;
private final BiConsumer<PacketWrapper<?>, Object> dataSerializer;

public EntityDataType(String name, int[] ids, Function<PacketWrapper<?>, T> dataDeserializer, BiConsumer<PacketWrapper<?>, Object> dataSerializer) {
this.name = name;
this.ids = ids;
this.dataDeserializer = dataDeserializer;
this.dataSerializer = dataSerializer;
@Deprecated
public final class EntityDataType<T> {

private final IEntityDataType<T> delegate;

EntityDataType(IEntityDataType<T> delegate) {
this.delegate = delegate;
}

public String getName() {
return name;
return this.delegate.getName().toString();
}

public int getId(ClientVersion version) {
int index = EntityDataTypes.TYPES_BUILDER.getDataIndex(version);
return ids[index];
return this.delegate.getId(version);
}

public Function<PacketWrapper<?>, T> getDataDeserializer() {
return dataDeserializer;
return this.delegate::deserialize;
}

@SuppressWarnings("unchecked")
public BiConsumer<PacketWrapper<?>, Object> getDataSerializer() {
return dataSerializer;
return (wrapper, value) -> this.delegate.serialize(wrapper, (T) value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* This file is part of packetevents - https://github.com/retrooper/packetevents
* Copyright (C) 2024 retrooper and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.github.retrooper.packetevents.protocol.entity.data;

import com.github.retrooper.packetevents.protocol.mapper.AbstractMappedEntity;
import com.github.retrooper.packetevents.util.mappings.TypesBuilderData;
import com.github.retrooper.packetevents.wrapper.PacketWrapper;

import java.util.function.Function;

public class EntityDataTypeImpl<T> extends AbstractMappedEntity implements IEntityDataType<T> {

@Deprecated
private final EntityDataType<T> legacy;

private final PacketWrapper.Reader<T> reader;
private final PacketWrapper.Writer<T> writer;

@SuppressWarnings("deprecation")
public EntityDataTypeImpl(
TypesBuilderData data,
PacketWrapper.Reader<T> reader,
PacketWrapper.Writer<T> writer
) {
super(data);
this.legacy = new EntityDataType<>(this);
this.reader = reader;
this.writer = writer;
}

@Override
public T deserialize(PacketWrapper<?> wrapper) {
return this.reader.apply(wrapper);
}

@Override
public void serialize(PacketWrapper<?> wrapper, T value) {
this.writer.accept(wrapper, value);
}

@Override
public <Z> IEntityDataType<Z> map(Function<T, Z> processor, Function<Z, T> unprocessor) {
return new EntityDataTypeImpl<>(this.data,
wrapper -> processor.apply(this.reader.apply(wrapper)),
(wrapper, value) -> this.writer.accept(wrapper, unprocessor.apply(value)));
}

@Deprecated
@Override
public EntityDataType<T> asLegacy() {
return this.legacy;
}
}
Loading

0 comments on commit 025fac7

Please sign in to comment.