forked from retrooper/packetevents
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
435 additions
and
270 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
.../main/java/com/github/retrooper/packetevents/protocol/entity/data/EntityDataTypeImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.