Center On BlockPos Not ChunkPos
All checks were successful
Twine/pipeline/head This commit looks good

This commit is contained in:
TheBrokenRail 2020-06-15 16:29:28 -04:00
parent 3ade8dd72f
commit 7da5501008
6 changed files with 39 additions and 42 deletions

View File

@ -5,11 +5,12 @@ import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.PersistentState;
import net.minecraft.world.World;
import java.util.*;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class StageDataComponent extends PersistentState {
public StageDataComponent(String key) {
@ -28,23 +29,23 @@ public class StageDataComponent extends PersistentState {
return data.computeIfAbsent(uuid, k -> new StageData[Twine.STAGE_COUNT]);
}
private static final int CHUNK_RADIUS = 16;
private static final int CHUNK_RADIUS = 16 * 16;
private static final String STAGE_DATA_ID = "twine_stage_data";
public static int findStageOfChunk(ChunkPos pos, StageDataComponent.StageData[] data) {
public static int findStageOfChunk(BlockPos pos, StageDataComponent.StageData[] data) {
for (int i = Twine.STAGE_COUNT - 1; i >= 0; i--) {
StageDataComponent.StageData stage = data[i];
if (stage != null && Math.abs(stage.x - pos.x) <= CHUNK_RADIUS && Math.abs(stage.z - pos.z) <= CHUNK_RADIUS) {
if (stage != null && Math.abs(stage.x - pos.getX()) <= CHUNK_RADIUS && Math.abs(stage.z - pos.getZ()) <= CHUNK_RADIUS) {
return i;
}
}
data[0] = new StageDataComponent.StageData();
data[0].x = pos.x;
data[0].z = pos.z;
data[0].x = pos.getX();
data[0].z = pos.getZ();
return 0;
}
public int findEffectiveStageOfChunk(ServerWorld world, ChunkPos pos) {
public int findEffectiveStageOfChunk(ServerWorld world, BlockPos pos) {
int stage = 0;
for (Map.Entry<UUID, StageData[]> entry : data.entrySet()) {
if (world.getPlayerByUuid(entry.getKey()) != null) {
@ -54,7 +55,7 @@ public class StageDataComponent extends PersistentState {
return stage;
}
public long findEffectiveTimeOfChunk(ServerWorld world, ChunkPos pos) {
public long findEffectiveTimeOfChunk(ServerWorld world, BlockPos pos) {
long time = 0;
for (Map.Entry<UUID, StageData[]> entry : data.entrySet()) {
if (world.getPlayerByUuid(entry.getKey()) != null) {

View File

@ -56,8 +56,8 @@ public class ExplodeArtificialBlockGoal extends MoveToTargetPosGoal {
@Override
public boolean canStart() {
StageDataComponent component = StageDataComponent.getFromWorld((ServerWorld) mob.getEntityWorld());
ChunkPos chunkPos = new ChunkPos(mob.getBlockPos());
int stage = component.findEffectiveStageOfChunk((ServerWorld) mob.getEntityWorld(), chunkPos);
BlockPos pos = mob.getBlockPos();
int stage = component.findEffectiveStageOfChunk((ServerWorld) mob.getEntityWorld(), pos);
return stage >= 3 && super.canStart();
}

View File

@ -9,14 +9,14 @@ import net.minecraft.entity.mob.MobEntity;
import net.minecraft.entity.passive.PassiveEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.util.math.BlockPos;
public class FollowPassiveEntityGoal extends FollowTargetGoal<LivingEntity> {
public FollowPassiveEntityGoal(MobEntity mob) {
super(mob, LivingEntity.class, 10, false, false, entity -> {
StageDataComponent component = StageDataComponent.getFromWorld((ServerWorld) mob.getEntityWorld());
ChunkPos chunkPos = new ChunkPos(mob.getBlockPos());
int stage = component.findEffectiveStageOfChunk((ServerWorld) mob.getEntityWorld(), chunkPos);
BlockPos pos = mob.getBlockPos();
int stage = component.findEffectiveStageOfChunk((ServerWorld) mob.getEntityWorld(), pos);
return entity instanceof PassiveEntity || (entity instanceof PlayerEntity && stage >= 4);
});
@ -24,8 +24,8 @@ public class FollowPassiveEntityGoal extends FollowTargetGoal<LivingEntity> {
public boolean canStart() {
StageDataComponent component = StageDataComponent.getFromWorld((ServerWorld) mob.getEntityWorld());
ChunkPos chunkPos = new ChunkPos(mob.getBlockPos());
int stage = component.findEffectiveStageOfChunk((ServerWorld) mob.getEntityWorld(), chunkPos);
BlockPos pos = mob.getBlockPos();
int stage = component.findEffectiveStageOfChunk((ServerWorld) mob.getEntityWorld(), pos);
return mob instanceof HostileEntity && (!(mob instanceof Angerable) || stage >= 4) && stage >= 2 && super.canStart();
}

View File

@ -5,7 +5,6 @@ import com.thebrokenrail.twine.component.StageDataComponent;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.SwordItem;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.LiteralText;
import net.minecraft.text.TranslatableText;
@ -13,7 +12,7 @@ import net.minecraft.util.ActionResult;
import net.minecraft.util.Formatting;
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
public class DivinerItem extends Item {
@ -26,17 +25,17 @@ public class DivinerItem extends Item {
ItemStack stack = user.getStackInHand(hand);
if (!world.isClient()) {
StageDataComponent component = StageDataComponent.getFromWorld((ServerWorld) world);
ChunkPos chunkPos = new ChunkPos(user.getBlockPos());
BlockPos pos = user.getBlockPos();
int stage = component.findEffectiveStageOfChunk((ServerWorld) world, chunkPos);
int stage = component.findEffectiveStageOfChunk((ServerWorld) world, pos);
StageDataComponent.StageData[] personalData = component.getData(user.getUuid());
int effectiveStage = StageDataComponent.findStageOfChunk(chunkPos, personalData);
int effectiveStage = StageDataComponent.findStageOfChunk(pos, personalData);
user.sendMessage(new TranslatableText("chat.twine.diviner_info_1", new LiteralText(String.valueOf(stage + 1)).formatted(Formatting.WHITE)).formatted(Formatting.YELLOW), false);
user.sendMessage(new TranslatableText("chat.twine.diviner_info_2", (stage + 1 >= Twine.STAGE_COUNT ? new TranslatableText("chat.twine.diviner_info_never") : new LiteralText(String.valueOf(Twine.STAGE_TIME - component.findEffectiveTimeOfChunk((ServerWorld) world, chunkPos)))).formatted(Formatting.WHITE)).formatted(Formatting.YELLOW), false);
user.sendMessage(new TranslatableText("chat.twine.diviner_info_2", (stage + 1 >= Twine.STAGE_COUNT ? new TranslatableText("chat.twine.diviner_info_never") : new LiteralText(String.valueOf(Twine.STAGE_TIME - component.findEffectiveTimeOfChunk((ServerWorld) world, pos)))).formatted(Formatting.WHITE)).formatted(Formatting.YELLOW), false);
user.sendMessage(new TranslatableText("chat.twine.diviner_info_3", new LiteralText(String.valueOf(effectiveStage + 1)).formatted(Formatting.WHITE)).formatted(Formatting.YELLOW), false);
user.sendMessage(new TranslatableText("chat.twine.diviner_info_4", new LiteralText(String.valueOf(personalData[effectiveStage].x << 4)).formatted(Formatting.WHITE), new LiteralText(String.valueOf(personalData[effectiveStage].z << 4)).formatted(Formatting.WHITE)).formatted(Formatting.YELLOW), false);
user.sendMessage(new TranslatableText("chat.twine.diviner_info_4", new LiteralText(String.valueOf(personalData[effectiveStage].x)).formatted(Formatting.WHITE), new LiteralText(String.valueOf(personalData[effectiveStage].z)).formatted(Formatting.WHITE)).formatted(Formatting.YELLOW), false);
user.sendMessage(new TranslatableText("chat.twine.diviner_info_5", (effectiveStage + 1 >= Twine.STAGE_COUNT ? new TranslatableText("chat.twine.diviner_info_never") : new LiteralText(String.valueOf(Twine.STAGE_TIME - personalData[effectiveStage].time))).formatted(Formatting.WHITE)).formatted(Formatting.YELLOW), false);
stack.damage(1, user, e -> e.sendToolBreakStatus(hand));

View File

@ -12,7 +12,7 @@ import net.minecraft.entity.mob.*;
import net.minecraft.entity.passive.IronGolemEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
@ -48,8 +48,8 @@ public class MixinMobEntity {
} else if ((Object) this instanceof IronGolemEntity) {
targetSelector.add(3, new FollowTargetGoal<>((IronGolemEntity) (Object) this, PlayerEntity.class, 10, true, false, entity -> {
StageDataComponent component = StageDataComponent.getFromWorld((ServerWorld) ((IronGolemEntity) (Object) this).getEntityWorld());
ChunkPos chunkPos = new ChunkPos(((IronGolemEntity) (Object) this).getBlockPos());
int stage = component.findEffectiveStageOfChunk((ServerWorld) (((IronGolemEntity) (Object) this).getEntityWorld()), chunkPos);
BlockPos blockPos = ((IronGolemEntity) (Object) this).getBlockPos();
int stage = component.findEffectiveStageOfChunk((ServerWorld) (((IronGolemEntity) (Object) this).getEntityWorld()), blockPos);
return stage >= 3 && !((IronGolemEntity) (Object) this).isPlayerCreated();
}));
@ -59,11 +59,13 @@ public class MixinMobEntity {
@Inject(at = @At("HEAD"), method = "isInDaylight", cancellable = true)
public void isInDaylight(CallbackInfoReturnable<Boolean> info) {
StageDataComponent component = StageDataComponent.getFromWorld((ServerWorld) ((ZombieEntity) (Object) this).getEntityWorld());
ChunkPos chunkPos = new ChunkPos(((ZombieEntity) (Object) this).getBlockPos());
int stage = component.findEffectiveStageOfChunk((ServerWorld) ((ZombieEntity) (Object) this).getEntityWorld(), chunkPos);
if (stage >= 5) {
info.setReturnValue(false);
if (!((MobEntity) (Object) this).getEntityWorld().isClient()) {
StageDataComponent component = StageDataComponent.getFromWorld((ServerWorld) ((MobEntity) (Object) this).getEntityWorld());
BlockPos blockPos = ((MobEntity) (Object) this).getBlockPos();
int stage = component.findEffectiveStageOfChunk((ServerWorld) ((MobEntity) (Object) this).getEntityWorld(), blockPos);
if (stage >= 5) {
info.setReturnValue(false);
}
}
}
}

View File

@ -4,7 +4,7 @@ import com.thebrokenrail.twine.Twine;
import com.thebrokenrail.twine.component.StageDataComponent;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.util.math.BlockPos;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
@ -17,11 +17,6 @@ public abstract class MixinServerPlayerEntity {
@Shadow
public abstract ServerWorld getServerWorld();
@Unique
private ChunkPos getChunkPos() {
return new ChunkPos(((ServerPlayerEntity) (Object) this).getBlockPos());
}
@Unique
private long lastTime = -1L;
@ -29,7 +24,7 @@ public abstract class MixinServerPlayerEntity {
public void tick(CallbackInfo info) {
StageDataComponent component = StageDataComponent.getFromWorld(getServerWorld());
StageDataComponent.StageData[] data = component.getData(((ServerPlayerEntity) (Object) this).getUuid());
int stage = StageDataComponent.findStageOfChunk(getChunkPos(), data);
int stage = StageDataComponent.findStageOfChunk(((ServerPlayerEntity) (Object) this).getBlockPos(), data);
StageDataComponent.StageData stageObj = data[stage];
if (stage + 1 < Twine.STAGE_COUNT) {
if (lastTime != -1L) {
@ -41,9 +36,9 @@ public abstract class MixinServerPlayerEntity {
old.time = 0;
}
StageDataComponent.StageData newData = new StageDataComponent.StageData();
ChunkPos pos = getChunkPos();
newData.x = pos.x;
newData.z = pos.z;
BlockPos pos = ((ServerPlayerEntity) (Object) this).getBlockPos();
newData.x = pos.getX();
newData.z = pos.getZ();
data[stage + 1] = newData;
data[stage] = old;
}