Skip to content
25 changes: 15 additions & 10 deletions src/main/java/twilightforest/entity/CharmEffect.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@

import net.minecraft.core.particles.ItemParticleOption;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.Mth;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.projectile.ItemSupplier;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.storage.ValueInput;
import net.minecraft.world.level.storage.ValueOutput;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.Nullable;

import javax.annotation.Nonnull;

public class CharmEffect extends Entity implements ItemSupplier {
private static final double DISTANCE = 0.75D;
private double interpTargetX;
Expand All @@ -42,7 +43,7 @@ public CharmEffect(EntityType<? extends CharmEffect> type, Level level, LivingEn
this.orbiter = owner;
this.displayItem = item;

this.moveTo(owner.getX(), owner.getY() + owner.getEyeHeight(), owner.getZ(), owner.getYRot(), owner.getXRot());
this.snapTo(owner.getX(), owner.getY() + owner.getEyeHeight(), owner.getZ(), owner.getYRot(), owner.getXRot());

Vec3 look = new Vec3(DISTANCE, 0, 0);
double x = getX() + (look.x() * DISTANCE);
Expand Down Expand Up @@ -73,15 +74,15 @@ public void tick() {
if (this.orbiter != null) {
float rotation = this.tickCount / 10.0F + this.offset;
Vec3 look = new Vec3(DISTANCE, 0, 0).yRot(rotation);
this.moveTo(this.orbiter.getX() + look.x(), this.orbiter.getY() + this.orbiter.getEyeHeight(), this.orbiter.getZ() + look.z(), this.orbiter.getYRot(), this.orbiter.getXRot());
this.snapTo(this.orbiter.getX() + look.x(), this.orbiter.getY() + this.orbiter.getEyeHeight(), this.orbiter.getZ() + look.z(), this.orbiter.getYRot(), this.orbiter.getXRot());
}

if (!this.displayItem.isEmpty()) {
double dx = getX() + 0.25 * (this.random.nextDouble() - this.random.nextDouble());
double dy = getY() + 0.25 * (this.random.nextDouble() - this.random.nextDouble());
double dz = getZ() + 0.25 * (this.random.nextDouble() - this.random.nextDouble());

this.level().addParticle(new ItemParticleOption(ParticleTypes.ITEM, this.displayItem), dx, dy, dz, 0, 0.2, 0);
this.level().addParticle(new ItemParticleOption(ParticleTypes.ITEM, this.displayItem.getItem()), dx, dy, dz, 0, 0.2, 0);
}

if (this.tickCount > 200 || (this.orbiter != null && (!this.orbiter.isAlive() || this.orbiter.isInvisible()))) {
Expand All @@ -90,7 +91,7 @@ public void tick() {
}

@Override
public void lerpTo(double x, double y, double z, float yaw, float pitch, int posRotationIncrements) {
public void lerpPositionAndRotationStep(int posRotationIncrements, double x, double y, double z, double yaw, double pitch) {
this.interpTargetX = x;
this.interpTargetY = y;
this.interpTargetZ = z;
Expand All @@ -105,14 +106,18 @@ protected void defineSynchedData(SynchedEntityData.Builder builder) {
}

@Override
protected void readAdditionalSaveData(CompoundTag cmp) {
protected void readAdditionalSaveData(ValueInput valueInput) {
}

@Override
protected void addAdditionalSaveData(CompoundTag cmp) {
protected void addAdditionalSaveData(ValueOutput valueOutput) {
}

@Override
public boolean hurtServer(ServerLevel serverLevel, DamageSource damageSource, float v) {
return false;
}

@Nonnull
@Override
public ItemStack getItem() {
return this.displayItem;
Expand Down
23 changes: 11 additions & 12 deletions src/main/java/twilightforest/entity/EnforcedHomePoint.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package twilightforest.entity;

import com.mojang.serialization.Codec;
import net.minecraft.core.BlockPos;
import net.minecraft.core.GlobalPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.NbtOps;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.PathfinderMob;
Expand All @@ -13,10 +11,11 @@
import net.minecraft.world.level.storage.ValueInput;
import net.minecraft.world.level.storage.ValueOutput;
import org.jetbrains.annotations.Nullable;
import twilightforest.TwilightForestMod;
import twilightforest.entity.ai.goal.AttemptToGoHomeGoal;
import twilightforest.init.TFDimension;

import java.util.List;

public interface EnforcedHomePoint {

default <T extends PathfinderMob & EnforcedHomePoint> void addRestrictionGoals(T entity, GoalSelector selector) {
Expand All @@ -25,21 +24,21 @@ default <T extends PathfinderMob & EnforcedHomePoint> void addRestrictionGoals(T

default void saveHomePointToNbt(ValueOutput tag) {
if (this.getRestrictionPoint() != null) {
GlobalPos.CODEC.encodeStart(NbtOps.INSTANCE, this.getRestrictionPoint()).resultOrPartial(TwilightForestMod.LOGGER::error).ifPresent(tag1 -> tag.put("HomePos", tag1));
tag.store("HomePos", GlobalPos.CODEC, this.getRestrictionPoint());
}
}

default void loadHomePointFromNbt(ValueInput tag) {
//properly load old home points, just assume theyre set in TF
if (tag.contains("Home", 9)) {
ListTag nbttaglist = tag.getList("Home", 6);
double hx = nbttaglist.getDouble(0);
double hy = nbttaglist.getDouble(1);
double hz = nbttaglist.getDouble(2);
if (tag.read("Home", Codec.DOUBLE.listOf()).isPresent()) {
List<Double> nbttaglist = tag.read("Home", Codec.DOUBLE.listOf()).get();
double hx = nbttaglist.get(0);
double hy = nbttaglist.get(1);
double hz = nbttaglist.get(2);
this.setRestrictionPoint(GlobalPos.of(TFDimension.DIMENSION_KEY, BlockPos.containing(hx, hy, hz)));
} else {
if (tag.contains("HomePos")) {
this.setRestrictionPoint(GlobalPos.CODEC.parse(NbtOps.INSTANCE, tag.get("HomePos")).resultOrPartial(TwilightForestMod.LOGGER::error).orElse(null));
if (tag.read("HomePos", GlobalPos.CODEC).isPresent()) {
this.setRestrictionPoint(tag.read("HomePos", GlobalPos.CODEC).get());
}
}
}
Expand Down
53 changes: 28 additions & 25 deletions src/main/java/twilightforest/entity/MagicPainting.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package twilightforest.entity;

import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.Util;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.Holder;
import net.minecraft.core.Registry;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
Expand All @@ -16,14 +16,16 @@
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.MoverType;
import net.minecraft.world.entity.decoration.HangingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.GameRules;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.gamerules.GameRules;
import net.minecraft.world.level.storage.ValueInput;
import net.minecraft.world.level.storage.ValueOutput;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.NotNull;
import twilightforest.TFRegistries;
import twilightforest.init.TFDataComponents;
import twilightforest.init.TFDataSerializers;
Expand All @@ -39,6 +41,8 @@
public class MagicPainting extends HangingEntity {
private static final EntityDataAccessor<Holder<MagicPaintingVariant>> MAGIC_PAINTING_VARIANT = SynchedEntityData.defineId(MagicPainting.class, TFDataSerializers.MAGIC_PAINTING_VARIANT.value());

private Direction direction;

public MagicPainting(EntityType<? extends MagicPainting> entityType, Level level) {
super(entityType, level);
}
Expand All @@ -49,7 +53,7 @@ private MagicPainting(Level level, BlockPos pos) {

@Override
protected void defineSynchedData(SynchedEntityData.Builder builder) {
builder.define(MAGIC_PAINTING_VARIANT, this.getReg().getHolderOrThrow(MagicPaintingVariants.DEFAULT));
builder.define(MAGIC_PAINTING_VARIANT, this.getReg().getOrThrow(MagicPaintingVariants.DEFAULT));
}

@Override
Expand All @@ -69,8 +73,8 @@ public Holder<MagicPaintingVariant> getVariant() {

public static Optional<MagicPainting> create(Level level, BlockPos pos, Direction direction) {
MagicPainting magicPainting = new MagicPainting(level, pos);
List<Holder<MagicPaintingVariant>> list = new ArrayList<>();
level.registryAccess().registryOrThrow(TFRegistries.Keys.MAGIC_PAINTINGS).holders().forEach(list::add);
List<Holder.Reference<MagicPaintingVariant>> list = new ArrayList<>();
level.registryAccess().lookupOrThrow(TFRegistries.Keys.MAGIC_PAINTINGS).listElements().forEach(list::add);
if (list.isEmpty()) {
return Optional.empty();
} else {
Expand All @@ -83,8 +87,8 @@ public static Optional<MagicPainting> create(Level level, BlockPos pos, Directio
return Optional.empty();
} else {
int biggestPossibleArea = list.stream().mapToInt(MagicPainting::variantArea).max().orElse(0);
list.removeIf((variantArea) -> variantArea(variantArea) < biggestPossibleArea);
Optional<Holder<MagicPaintingVariant>> optional = Util.getRandomSafe(list, magicPainting.random);
list.removeIf((variant) -> variantArea(variant) < biggestPossibleArea);
Optional<Holder.Reference<MagicPaintingVariant>> optional = Util.getRandomSafe(list, magicPainting.random);
if (optional.isEmpty()) {
return Optional.empty();
} else {
Expand All @@ -105,29 +109,29 @@ private static int variantArea(MagicPaintingVariant variant) {
}

@Override
public void addAdditionalSaveData(CompoundTag tag) {
protected void addAdditionalSaveData(ValueOutput output) {
Identifier location = this.getReg().getKey(this.getVariant().value());
if (location != null) tag.putString("variant", location.toString());
tag.putByte("facing", (byte) this.direction.get2DDataValue());
super.addAdditionalSaveData(tag);
if (location != null) output.putString("variant", location.toString());
output.putByte("facing", (byte) this.direction.get2DDataValue());
super.addAdditionalSaveData(output);
}

@Override
public void readAdditionalSaveData(CompoundTag tag) {
if (tag.contains("variant")) {
Identifier location = Identifier.tryParse(tag.getString("variant"));
public void readAdditionalSaveData(ValueInput input) {
if (input.getString("variant").isPresent()) {
Identifier location = Identifier.tryParse(input.getString("variant").get());
if (location != null) {
this.setVariant(this.getReg().getHolder(location).orElse(this.getReg().getHolderOrThrow(MagicPaintingVariants.DEFAULT)));
this.setVariant(this.getReg().get(location).orElse(this.getReg().getOrThrow(MagicPaintingVariants.DEFAULT)));
}
}

this.direction = Direction.from2DDataValue(tag.getByte("facing"));
super.readAdditionalSaveData(tag);
this.direction = Direction.from2DDataValue(input.getByteOr("facing", (byte) 0));
super.readAdditionalSaveData(input);
this.setDirection(this.direction);
}

protected Registry<MagicPaintingVariant> getReg() {
return this.registryAccess().registryOrThrow(TFRegistries.Keys.MAGIC_PAINTINGS);
return this.registryAccess().lookupOrThrow(TFRegistries.Keys.MAGIC_PAINTINGS);
}

@Override
Expand All @@ -150,16 +154,16 @@ private double offsetForPaintingSize(int size) {
}

@Override
public void dropItem(@Nullable Entity entity) {
if (this.level().getGameRules().getBoolean(GameRules.RULE_DOENTITYDROPS)) {
public void dropItem(ServerLevel serverLevel, @Nullable Entity entity) {
if (serverLevel.getGameRules().get(GameRules.ENTITY_DROPS)) {
this.playSound(SoundEvents.PAINTING_BREAK, 1.0F, 1.0F);
if (entity instanceof Player player) {
if (player.getAbilities().instabuild) {
return;
}
}

this.spawnAtLocation(this.getPickResult());
this.spawnAtLocation(serverLevel, this.getPickResult());
}
}

Expand All @@ -169,12 +173,12 @@ public void playPlacementSound() {
}

@Override
public void moveTo(double x, double y, double z, float yaw, float pitch) {
public void snapTo(double x, double y, double z, float yaw, float pitch) {
this.setPos(x, y, z);
}

@Override
public void lerpTo(double x, double y, double z, float yaw, float pitch, int posRotationIncrements) {
public void lerpPositionAndRotationStep(int stepsToTarget, double x, double y, double z, double targetYRot, double targetXRot) {
this.setPos(x, y, z);
}

Expand All @@ -195,7 +199,6 @@ public void recreateFromPacket(ClientboundAddEntityPacket packet) {
}

@Override
@NotNull
public ItemStack getPickResult() {
ItemStack itemStack = new ItemStack(TFItems.MAGIC_PAINTING.get());
itemStack.set(TFDataComponents.MAGIC_PAINTING_VARIANT, this.getVariant());
Expand Down
17 changes: 13 additions & 4 deletions src/main/java/twilightforest/entity/ProtectionBox.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package twilightforest.entity;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityDimensions;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.levelgen.structure.BoundingBox;
import net.minecraft.world.level.storage.ValueInput;
import net.minecraft.world.level.storage.ValueOutput;
import twilightforest.init.TFEntities;

public class ProtectionBox extends Entity {
Expand All @@ -31,7 +34,8 @@ public ProtectionBox(Level world, BoundingBox sbb) {

this.sbb = sbb;

this.moveTo(sbb.minX(), sbb.minY(), sbb.minZ(), 0.0F, 0.0F);
this.snapTo(sbb.minX(), sbb.minY(), sbb.minZ(), 0.0F, 0.0F);
this.setRot(0.0F, 0.0F);
Comment thread
albazavr-alba marked this conversation as resolved.
Outdated

this.sizeX = sbb.getXSpan();
this.sizeY = sbb.getYSpan();
Expand Down Expand Up @@ -67,11 +71,16 @@ protected void defineSynchedData(SynchedEntityData.Builder builder) {
}

@Override
protected void readAdditionalSaveData(CompoundTag compound) {
protected void readAdditionalSaveData(ValueInput compound) {
}

@Override
protected void addAdditionalSaveData(CompoundTag compound) {
protected void addAdditionalSaveData(ValueOutput compound) {
}

@Override
public boolean hurtServer(ServerLevel serverLevel, DamageSource damageSource, float v) {
return false;
}

@Override
Expand Down
Loading
Loading