Skip to content

Commit

Permalink
Created custom EntityDataSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
Edivad99 committed Mar 12, 2024
1 parent cd5741e commit d51fba7
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 48 deletions.
39 changes: 25 additions & 14 deletions src/main/java/mods/railcraft/network/RailcraftDataSerializers.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@
import java.util.Optional;
import com.mojang.authlib.GameProfile;
import mods.railcraft.api.core.RailcraftConstants;
import mods.railcraft.season.Season;
import mods.railcraft.world.entity.vehicle.MaintenanceMinecart;
import mods.railcraft.world.entity.vehicle.locomotive.Locomotive;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.EntityDataSerializer;
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;

public class RailcraftDataSerializers {

private static final DeferredRegister<EntityDataSerializer<?>> deferredRegister =
DeferredRegister.create(ForgeRegistries.Keys.ENTITY_DATA_SERIALIZERS, RailcraftConstants.ID);

public static final EntityDataSerializer<FluidStack> FLUID_STACK =
new EntityDataSerializer<>() {
@Override
Expand Down Expand Up @@ -55,23 +59,30 @@ public byte[] copy(byte[] value) {
EntityDataSerializer
.optional(FriendlyByteBuf::writeGameProfile, FriendlyByteBuf::readGameProfile);

private static final DeferredRegister<EntityDataSerializer<?>> deferredRegister =
DeferredRegister.create(ForgeRegistries.Keys.ENTITY_DATA_SERIALIZERS, RailcraftConstants.ID);
public static final EntityDataSerializer<Locomotive.Mode> LOCOMOTIVE_MODE =
EntityDataSerializer.simpleEnum(Locomotive.Mode.class);

public static final EntityDataSerializer<Locomotive.Speed> LOCOMOTIVE_SPEED =
EntityDataSerializer.simpleEnum(Locomotive.Speed.class);

public static final EntityDataSerializer<Locomotive.Lock> LOCOMOTIVE_LOCK =
EntityDataSerializer.simpleEnum(Locomotive.Lock.class);

public static final EntityDataSerializer<MaintenanceMinecart.Mode> MAINTENANCE_MODE =
EntityDataSerializer.simpleEnum(MaintenanceMinecart.Mode.class);

public static final EntityDataSerializer<Season> MINECART_SEASON =
EntityDataSerializer.simpleEnum(Season.class);

public static void register(IEventBus modEventBus) {
deferredRegister.register("fluid_stack", () -> FLUID_STACK);
deferredRegister.register("byte_array", () -> BYTE_ARRAY);
deferredRegister.register("optional_game_profile", () -> OPTIONAL_GAME_PROFILE);
deferredRegister.register("locomotive_mode", () -> LOCOMOTIVE_MODE);
deferredRegister.register("locomotive_speed", () -> LOCOMOTIVE_SPEED);
deferredRegister.register("locomotive_lock", () -> LOCOMOTIVE_LOCK);
deferredRegister.register("maintenance_mode", () -> MAINTENANCE_MODE);
deferredRegister.register("minecart_season", () -> MINECART_SEASON);
deferredRegister.register(modEventBus);
}

public static <T extends Enum<T>> void setEnum(SynchedEntityData dataManager,
EntityDataAccessor<Byte> parameter, Enum<T> value) {
dataManager.set(parameter, (byte) value.ordinal());
}

public static <T extends Enum<T>> T getEnum(SynchedEntityData dataManager,
EntityDataAccessor<Byte> parameter, T[] values) {
return values[dataManager.get(parameter)];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public abstract class MaintenanceMinecart extends RailcraftMinecart {
SynchedEntityData.defineId(MaintenanceMinecart.class, EntityDataSerializers.BYTE);
protected static final double DRAG_FACTOR = 0.9;
private static final int BLINK_DURATION = 3;
private static final EntityDataAccessor<Byte> MODE =
SynchedEntityData.defineId(MaintenanceMinecart.class, EntityDataSerializers.BYTE);
private static final EntityDataAccessor<Mode> MODE =
SynchedEntityData.defineId(MaintenanceMinecart.class, RailcraftDataSerializers.MAINTENANCE_MODE);

protected MaintenanceMinecart(EntityType<?> type, Level level) {
super(type, level);
Expand All @@ -47,7 +47,7 @@ protected MaintenanceMinecart(EntityType<?> type, double x, double y, double z,
protected void defineSynchedData() {
super.defineSynchedData();
this.entityData.define(BLINK, (byte) 0);
this.entityData.define(MODE, (byte) Mode.ON.ordinal());
this.entityData.define(MODE, Mode.ON);
}

@Override
Expand All @@ -56,11 +56,11 @@ public float getMaxCartSpeedOnRail() {
}

public Mode mode() {
return RailcraftDataSerializers.getEnum(this.entityData, MODE, Mode.values());
return this.entityData.get(MODE);
}

public void setMode(Mode mode) {
RailcraftDataSerializers.setEnum(this.entityData, MODE, mode);
this.entityData.set(MODE, mode);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import mods.railcraft.api.carts.ItemTransferHandler;
import mods.railcraft.api.carts.RollingStock;
import mods.railcraft.api.track.TrackUtil;
import mods.railcraft.network.RailcraftDataSerializers;
import mods.railcraft.season.Season;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.EntityDataSerializers;
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
Expand All @@ -32,8 +32,8 @@
public abstract class RailcraftMinecart extends AbstractMinecartContainer
implements SeasonalCart, ItemTransferHandler {

private static final EntityDataAccessor<Byte> SEASON =
SynchedEntityData.defineId(RailcraftMinecart.class, EntityDataSerializers.BYTE);
private static final EntityDataAccessor<Season> SEASON =
SynchedEntityData.defineId(RailcraftMinecart.class, RailcraftDataSerializers.MINECART_SEASON);

private final Direction[] travelDirectionHistory = new Direction[2];
@Nullable
Expand All @@ -60,18 +60,18 @@ public Optional<Direction> verticalTravelDirection() {
@Override
protected void defineSynchedData() {
super.defineSynchedData();
this.entityData.define(SEASON, (byte) Season.DEFAULT.ordinal());
this.entityData.define(SEASON, Season.DEFAULT);
}

@Override
public Season getSeason() {
// TODO: 1.20.4+ use Season.fromName(this.entityData.get(SEASON));
return Season.values()[this.entityData.get(SEASON)];
return this.entityData.get(SEASON);
}

@Override
public void setSeason(Season season) {
this.entityData.set(SEASON, (byte) season.ordinal());
this.entityData.set(SEASON, season);
}

@Override
Expand Down Expand Up @@ -190,7 +190,8 @@ private Direction determineTravelDirection(RailShape shape) {
};
}

private @Nullable Direction determineVerticalTravelDirection(RailShape shape) {
@Nullable
private Direction determineVerticalTravelDirection(RailShape shape) {
return shape.isAscending() ? this.yo < getY() ? Direction.UP : Direction.DOWN : null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ public abstract class Locomotive extends RailcraftMinecart implements

private static final EntityDataAccessor<Boolean> HAS_FUEL =
SynchedEntityData.defineId(Locomotive.class, EntityDataSerializers.BOOLEAN);
private static final EntityDataAccessor<Byte> MODE =
SynchedEntityData.defineId(Locomotive.class, EntityDataSerializers.BYTE);
private static final EntityDataAccessor<Byte> SPEED =
SynchedEntityData.defineId(Locomotive.class, EntityDataSerializers.BYTE);
private static final EntityDataAccessor<Byte> LOCK =
SynchedEntityData.defineId(Locomotive.class, EntityDataSerializers.BYTE);
private static final EntityDataAccessor<Mode> MODE =
SynchedEntityData.defineId(Locomotive.class, RailcraftDataSerializers.LOCOMOTIVE_MODE);
private static final EntityDataAccessor<Speed> SPEED =
SynchedEntityData.defineId(Locomotive.class, RailcraftDataSerializers.LOCOMOTIVE_SPEED);
private static final EntityDataAccessor<Lock> LOCK =
SynchedEntityData.defineId(Locomotive.class, RailcraftDataSerializers.LOCOMOTIVE_LOCK);
private static final EntityDataAccessor<Boolean> REVERSE =
SynchedEntityData.defineId(Locomotive.class, EntityDataSerializers.BOOLEAN);
private static final EntityDataAccessor<Integer> PRIMARY_COLOR =
Expand Down Expand Up @@ -119,9 +119,9 @@ protected void defineSynchedData() {
this.entityData.define(HAS_FUEL, false);
this.entityData.define(PRIMARY_COLOR, this.getDefaultPrimaryColor().getId());
this.entityData.define(SECONDARY_COLOR, this.getDefaultSecondaryColor().getId());
this.entityData.define(MODE, (byte) Mode.SHUTDOWN.ordinal());
this.entityData.define(SPEED, (byte) Speed.NORMAL.ordinal());
this.entityData.define(LOCK, (byte) Lock.UNLOCKED.ordinal());
this.entityData.define(MODE, Mode.SHUTDOWN);
this.entityData.define(SPEED, Speed.NORMAL);
this.entityData.define(LOCK, Lock.UNLOCKED);
this.entityData.define(REVERSE, false);
this.entityData.define(DESTINATION, "");
this.entityData.define(OWNER, Optional.empty());
Expand Down Expand Up @@ -270,26 +270,26 @@ public boolean canControl(Player player) {
* Gets the lock status.
*/
public Lock getLock() {
return Lock.values()[this.entityData.get(LOCK)];
return this.entityData.get(LOCK);
}

/**
* Sets the lock from the status.
*/
public void setLock(Lock lock) {
this.entityData.set(LOCK, (byte) lock.ordinal());
this.entityData.set(LOCK, lock);
}

@Override
public String getDestination() {
return this.getEntityData().get(DESTINATION);
return this.entityData.get(DESTINATION);
}

/**
* Set the destination used by routing the train around your train network.
*/
public void setDestination(String destination) {
this.getEntityData().set(DESTINATION, destination);
this.entityData.set(DESTINATION, destination);
}

/**
Expand Down Expand Up @@ -324,7 +324,7 @@ public boolean setDestination(ItemStack ticket) {
* Gets the current train's mode. Returns an enum mode.
*/
public Mode getMode() {
return RailcraftDataSerializers.getEnum(this.getEntityData(), MODE, Mode.values());
return this.entityData.get(MODE);
}

/**
Expand All @@ -334,7 +334,7 @@ public void setMode(Mode mode) {
if (!this.isAllowedMode(mode)) {
return;
}
RailcraftDataSerializers.setEnum(this.getEntityData(), MODE, mode);
this.entityData.set(MODE, mode);
}

/**
Expand Down Expand Up @@ -362,7 +362,7 @@ public boolean isAllowedMode(Mode mode) {
* @see Speed
*/
public Speed getSpeed() {
return RailcraftDataSerializers.getEnum(this.getEntityData(), SPEED, Speed.values());
return this.entityData.get(SPEED);
}

/**
Expand All @@ -374,7 +374,7 @@ public void setSpeed(Speed speed) {
if (this.isReverse() && (speed.getLevel() > this.getMaxReverseSpeed().getLevel())) {
return;
}
RailcraftDataSerializers.setEnum(this.getEntityData(), SPEED, speed);
this.entityData.set(SPEED, speed);
}

/**
Expand Down Expand Up @@ -403,19 +403,19 @@ public void decreaseSpeed() {
}

public boolean hasFuel() {
return this.getEntityData().get(HAS_FUEL);
return this.entityData.get(HAS_FUEL);
}

public void setHasFuel(boolean hasFuel) {
this.getEntityData().set(HAS_FUEL, hasFuel);
this.entityData.set(HAS_FUEL, hasFuel);
}

public boolean isReverse() {
return this.getEntityData().get(REVERSE);
return this.entityData.get(REVERSE);
}

public void setReverse(boolean reverse) {
this.getEntityData().set(REVERSE, reverse);
this.entityData.set(REVERSE, reverse);
}

public boolean isRunning() {
Expand Down Expand Up @@ -746,7 +746,7 @@ public void readAdditionalSaveData(CompoundTag tag) {
this.fuel = tag.getInt(CompoundTagKeys.FUEL);

if (tag.contains(CompoundTagKeys.REVERSE, Tag.TAG_BYTE)) {
this.getEntityData().set(REVERSE, tag.getBoolean(CompoundTagKeys.REVERSE));
this.entityData.set(REVERSE, tag.getBoolean(CompoundTagKeys.REVERSE));
}
if (tag.contains(CompoundTagKeys.OWNER, Tag.TAG_COMPOUND)) {
this.setOwner(NbtUtils.readGameProfile(tag.getCompound(CompoundTagKeys.OWNER)));;
Expand Down

0 comments on commit d51fba7

Please sign in to comment.