Skip to content

Commit

Permalink
More porting work
Browse files Browse the repository at this point in the history
  • Loading branch information
Treetrain1 committed Oct 19, 2024
1 parent 6dd4439 commit 8f738a0
Show file tree
Hide file tree
Showing 27 changed files with 195 additions and 159 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.Mth;
import net.minecraft.util.RandomSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityEvent;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.ai.attributes.AttributeInstance;
Expand All @@ -36,7 +38,9 @@
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.LightLayer;
import net.minecraft.world.level.ScheduledTickAccess;
import net.minecraft.world.level.Spawner;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
Expand Down Expand Up @@ -89,14 +93,14 @@ public static Direction getCoffinOrientation(@NotNull BlockGetter level, BlockPo
}

@Override
protected @NotNull BlockState updateShape(@NotNull BlockState state, Direction direction, BlockState neighborState, LevelAccessor level, BlockPos pos, BlockPos neighborPos) {
protected @NotNull BlockState updateShape(@NotNull BlockState state, LevelReader level, ScheduledTickAccess tickAccess, BlockPos pos, Direction direction, BlockPos neighborPos, BlockState neighborState, RandomSource random) {
if (direction == getNeighbourDirection(state.getValue(PART), state.getValue(FACING))) {
boolean isThisFoot = state.getValue(PART) == CoffinPart.FOOT;
return neighborState.is(this) && neighborState.getValue(PART) != state.getValue(PART)
? isThisFoot ? state : state.setValue(STATE, neighborState.getValue(STATE))
: Blocks.AIR.defaultBlockState();
} else {
return super.updateShape(state, direction, neighborState, level, pos, neighborPos);
return super.updateShape(state, level, tickAccess, pos, direction, neighborPos, neighborState, random);
}
}

Expand Down Expand Up @@ -236,7 +240,7 @@ public void appendHoverText(ItemStack stack, Item.TooltipContext tooltipContext,
Spawner.appendHoverText(stack, tooltip, "SpawnData");
}

public static void onCoffinUntrack(@Nullable Entity entity, @Nullable CoffinSpawner coffinSpawner, boolean remove) {
public static void onCoffinUntrack(ServerLevel level, @Nullable Entity entity, @Nullable CoffinSpawner coffinSpawner, boolean remove) {
if (FrozenLibConfig.IS_DEBUG && entity != null && !entity.isRemoved() && entity.level() instanceof ServerLevel serverLevel) {
FrozenNetworking.sendPacketToAllPlayers(
serverLevel,
Expand All @@ -259,9 +263,9 @@ public static void onCoffinUntrack(@Nullable Entity entity, @Nullable CoffinSpaw

if (entity instanceof Apparition apparition && remove) {
apparition.dropItem();
apparition.level().broadcastEntityEvent(apparition, (byte)60);
apparition.level().broadcastEntityEvent(apparition, EntityEvent.POOF);
apparition.discard();
apparition.dropPreservedEquipment();
apparition.dropPreservedEquipment(level);
if (coffinSpawner != null) {
coffinSpawner.onApparitionRemovedOrKilled(entity.level());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.ScheduledTickAccess;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.BonemealableBlock;
Expand Down Expand Up @@ -62,11 +63,11 @@ public static boolean canAttachTo(BlockGetter world, @NotNull Direction directio
}

@Override
protected @NotNull BlockState updateShape(BlockState state, Direction direction, BlockState neighborState, LevelAccessor world, BlockPos pos, BlockPos neighborPos) {
protected @NotNull BlockState updateShape(BlockState state, LevelReader level, ScheduledTickAccess tickAccess, BlockPos pos, Direction direction, BlockPos neighborPos, BlockState neighborState, RandomSource random) {
if (!hasAnyFace(state)) {
return Blocks.AIR.defaultBlockState();
} else {
return hasFace(state, direction) && !canAttachTo(world, direction, neighborPos, neighborState) ? removeFace(state, getFaceProperty(direction)) : state;
return hasFace(state, direction) && !canAttachTo(level, direction, neighborPos, neighborState) ? removeFace(state, getFaceProperty(direction)) : state;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,20 +392,20 @@ public boolean isAttemptingToSpawnMob(@NotNull ServerLevel level) {
return isPreparing && !finishedSpawningMobs && canSpawnInLevel;
}

public void tickServer(ServerLevel world, BlockPos pos, BlockState state, CoffinPart part, boolean ominous) {
if (part == CoffinPart.HEAD || world.isClientSide) {
public void tickServer(ServerLevel level, BlockPos pos, BlockState state, CoffinPart part, boolean ominous) {
if (part == CoffinPart.HEAD) {
return;
}

Direction coffinOrientation = CoffinBlock.getCoffinOrientation(world, pos);
Direction coffinOrientation = CoffinBlock.getCoffinOrientation(level, pos);
if (coffinOrientation != null) {
this.getState().emitParticles(world, pos, coffinOrientation);
this.getState().emitParticles(level, pos, coffinOrientation);
if (!this.data.soulsToSpawn.isEmpty()) {
IntArrayList newList = new IntArrayList();
this.data.soulsToSpawn.forEach(spawnTime -> {
if (spawnTime <= 0) {
CoffinBlock.spawnParticlesFrom(world, TTParticleTypes.COFFIN_SOUL_ENTER, 4, 0D, coffinOrientation, pos, 0.35D);
this.addPower(1, world);
CoffinBlock.spawnParticlesFrom(level, TTParticleTypes.COFFIN_SOUL_ENTER, 4, 0D, coffinOrientation, pos, 0.35D);
this.addPower(1, level);
} else {
newList.add(spawnTime - 1);
}
Expand All @@ -416,35 +416,35 @@ public void tickServer(ServerLevel world, BlockPos pos, BlockState state, Coffin
}

this.data.currentMobs.removeIf(uiid -> {
Entity entity = world.getEntity(uiid);
boolean shouldUntrack = shouldMobBeUntracked(world, pos, entity);
Entity entity = level.getEntity(uiid);
boolean shouldUntrack = shouldMobBeUntracked(level, pos, entity);
if (shouldUntrack) {
CoffinBlock.onCoffinUntrack(entity, this, false);
CoffinBlock.onCoffinUntrack(level, entity, this, false);
}
return shouldUntrack;
});

this.data.currentApparitions.removeIf(uiid -> {
Entity entity = world.getEntity(uiid);
boolean shouldUntrack = shouldMobBeUntracked(world, pos, entity);
Entity entity = level.getEntity(uiid);
boolean shouldUntrack = shouldMobBeUntracked(level, pos, entity);
if (shouldUntrack) {
CoffinBlock.onCoffinUntrack(entity, this, true);
CoffinBlock.onCoffinUntrack(level, entity, this, true);
}
return shouldUntrack;
});

CoffinSpawnerState currentState = this.getState();
if (!this.canSpawnInLevel(world)) {
if (!this.canSpawnInLevel(level)) {
if (currentState.isCapableOfSpawning()) {
this.setState(world, CoffinSpawnerState.INACTIVE);
this.setState(level, CoffinSpawnerState.INACTIVE);
}
} else {
CoffinSpawnerState nextState = currentState.tickAndGetNext(pos, this, state, world);
CoffinSpawnerState nextState = currentState.tickAndGetNext(pos, this, state, level);
if (nextState != currentState) {
this.setState(world, nextState);
this.setState(level, nextState);
}
}
this.updateAttemptingToSpawn(world);
this.updateAttemptingToSpawn(level);
}

private static boolean shouldMobBeUntracked(@NotNull ServerLevel level, BlockPos pos, UUID uuid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void tick(LivingEntity entity, @NotNull Level level) {
boolean canUntrackFromTime = (gameTime - this.lastInteractionAt) > 1800 && !(entity instanceof Apparition);
Optional<CoffinSpawner> optionalCoffinSpawner = this.getSpawner(level);
if (optionalCoffinSpawner.isEmpty() || canUntrackFromTime) {
CoffinBlock.onCoffinUntrack(entity, null, true);
CoffinBlock.onCoffinUntrack(serverLevel, entity, null, true);
} else {
if (FrozenLibConfig.IS_DEBUG) {
FrozenNetworking.sendPacketToAllPlayers(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void generate(@NotNull BiConsumer<ResourceKey<LootTable>, LootTable.Build
HolderLookup.Provider registryLookup = this.registries.join();

output.accept(
TTEntityTypes.APPARITION.getDefaultLootTable(),
TTEntityTypes.APPARITION.getDefaultLootTable().orElseThrow(),
LootTable.lootTable()
.withPool(
LootPool.lootPool()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
import net.frozenblock.trailiertales.registry.TTParticleTypes;
import net.frozenblock.trailiertales.registry.TTSounds;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.sounds.SoundSource;
import net.minecraft.util.Mth;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectCategory;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntitySpawnReason;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.GameRules;
Expand Down Expand Up @@ -54,24 +56,23 @@ public void onEffectAdded(LivingEntity entity, int amplifier) {
}

@Override
public void onMobRemoved(LivingEntity entity, int amplifier, Entity.RemovalReason reason) {
public void onMobRemoved(ServerLevel level, LivingEntity entity, int amplifier, Entity.RemovalReason reason) {
if (reason == Entity.RemovalReason.KILLED && entity.getType() != SPAWNED_ENTITY_TYPE) {
Level level = entity.level();
int j = level.getGameRules().getInt(GameRules.RULE_MAX_ENTITY_CRAMMING);
int k = numberOfApparitionsToSpawn(j, NearbyApparitions.closeTo(entity));

for (int l = 0; l < k; l++) {
this.spawnApparitionOffspring(entity.level(), entity.getX(), entity.getY() + 0.5D, entity.getZ());
this.spawnApparitionOffspring(level, entity.getX(), entity.getY() + 0.5D, entity.getZ());
}
}
}

private void spawnApparitionOffspring(Level world, double x, double y, double z) {
Apparition apparition = SPAWNED_ENTITY_TYPE.create(world);
private void spawnApparitionOffspring(ServerLevel level, double x, double y, double z) {
Apparition apparition = SPAWNED_ENTITY_TYPE.create(level, EntitySpawnReason.BREEDING);
if (apparition != null) {
apparition.moveTo(x, y, z, world.getRandom().nextFloat() * 360F, 0F);
ApparitionAi.rememberHome(apparition, world, BlockPos.containing(x, y, z));
world.addFreshEntity(apparition);
apparition.moveTo(x, y, z, level.getRandom().nextFloat() * 360F, 0F);
ApparitionAi.rememberHome(apparition, level, BlockPos.containing(x, y, z));
level.addFreshEntity(apparition);
}
}

Expand Down
56 changes: 26 additions & 30 deletions src/main/java/net/frozenblock/trailiertales/entity/Apparition.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import net.minecraft.tags.DamageTypeTags;
import net.minecraft.util.Mth;
import net.minecraft.util.Unit;
import net.minecraft.util.profiling.Profiler;
import net.minecraft.util.profiling.ProfilerFiller;
import net.minecraft.world.Difficulty;
import net.minecraft.world.DifficultyInstance;
import net.minecraft.world.SimpleContainer;
Expand Down Expand Up @@ -152,8 +154,8 @@ public void recreateFromPacket(ClientboundAddEntityPacket packet) {
}

@Override
public boolean isInvulnerableTo(DamageSource damageSource) {
return super.isInvulnerableTo(damageSource) || this.isHiding();
public boolean isInvulnerableTo(ServerLevel level, DamageSource damageSource) {
return super.isInvulnerableTo(level, damageSource) || this.isHiding();
}

@Override
Expand Down Expand Up @@ -284,26 +286,26 @@ private boolean isOnPickupCooldown() {
return this.getBrain().checkMemory(MemoryModuleType.ITEM_PICKUP_COOLDOWN_TICKS, MemoryStatus.VALUE_PRESENT);
}

public boolean wantsToPickUp(@NotNull ItemEntity itemEntity) {
return this.wantsToPickUp(itemEntity.getItem()) && this.getTarget() != null;
public boolean wantsToPickUp(ServerLevel level, @NotNull ItemEntity item) {
return this.wantsToPickUp(level, item.getItem()) && this.getTarget() != null;
}

@Override
public boolean wantsToPickUp(ItemStack stack) {
public boolean wantsToPickUp(ServerLevel level, ItemStack stack) {
return this.inventory.getItems().getFirst().isEmpty();
}

@Override
protected void pickUpItem(@NotNull ItemEntity item) {
protected void pickUpItem(ServerLevel level, ItemEntity item) {
ItemEntity newItemEntity = new ItemEntity(this.level(), item.getX(), item.getY(), item.getZ(), item.getItem().split(1));
this.level().addFreshEntity(newItemEntity);
InventoryCarrier.pickUpItem(this, this, newItemEntity);
InventoryCarrier.pickUpItem(level, this, this, newItemEntity);
}

@Override
protected void dropEquipment() {
super.dropEquipment();
this.inventory.removeAllItems().forEach(this::spawnAtLocation);
protected void dropEquipment(ServerLevel level) {
super.dropEquipment(level);
this.inventory.removeAllItems().forEach(it -> this.spawnAtLocation(level, it));
}

public float getInnerTransparency() {
Expand Down Expand Up @@ -435,7 +437,7 @@ public boolean canBeHitByProjectile() {
}

@Override
public boolean hurt(@NotNull DamageSource source, float amount) {
public boolean hurtServer(ServerLevel level, DamageSource source, float amount) {
if (source.is(DamageTypeTags.IS_PROJECTILE)) {
if (source.getDirectEntity() instanceof Projectile projectile) {
if (projectile instanceof AbstractArrow abstractArrow) {
Expand All @@ -449,14 +451,9 @@ public boolean hurt(@NotNull DamageSource source, float amount) {
}
}
}
boolean bl = super.hurt(source, amount);
if (this.level().isClientSide) {
return false;
}
if (bl) {
if (source.getEntity() instanceof LivingEntity livingEntity) {
ApparitionAi.wasHurtBy(this, livingEntity);
}
boolean bl = super.hurtServer(level, source, amount);
if (bl && source.getEntity() instanceof LivingEntity livingEntity) {
ApparitionAi.wasHurtBy(level, this, livingEntity);
}
return bl;
}
Expand Down Expand Up @@ -602,14 +599,15 @@ public Brain<Apparition> getBrain() {
}

@Override
protected void customServerAiStep() {
this.level().getProfiler().push("apparitionBrain");
this.getBrain().tick((ServerLevel) this.level(), this);
this.level().getProfiler().pop();
this.level().getProfiler().push("apparitionActivityUpdate");
protected void customServerAiStep(ServerLevel level) {
ProfilerFiller profiler = Profiler.get();
profiler.push("apparitionBrain");
this.getBrain().tick(level, this);
profiler.pop();
profiler.push("apparitionActivityUpdate");
ApparitionAi.updateActivity(this);
this.level().getProfiler().pop();
super.customServerAiStep();
profiler.pop();
super.customServerAiStep(level);
}

@Override
Expand All @@ -619,7 +617,7 @@ protected void sendDebugPackets() {
}

@Contract("null->false")
public boolean canTargetEntity(@Nullable Entity entity) {
public boolean canTargetEntity(@Nullable Entity entity, ServerLevel level) {
return entity instanceof LivingEntity livingEntity
&& this.level() == livingEntity.level()
&& !this.level().getDifficulty().equals(Difficulty.PEACEFUL)
Expand Down Expand Up @@ -665,9 +663,7 @@ public void performRangedAttack(LivingEntity target, float pullProgress) {
if (singleItem.getItem() instanceof ProjectileItem projectileItem) {
projectile = projectileItem.asProjectile(this.level(), this.getEyePosition(), singleItem, this.getDirection());
} else {
ThrownItemProjectile thrownItem = new ThrownItemProjectile(this.level(), this);
thrownItem.setItem(singleItem);
projectile = thrownItem;
projectile = new ThrownItemProjectile(this.level(), this, singleItem);
}
projectile.setOwner(this);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.projectile.ThrowableItemProjectile;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.BlockHitResult;
Expand All @@ -25,12 +26,12 @@ public ThrownItemProjectile(@NotNull EntityType<? extends ThrownItemProjectile>
super(entityType, level);
}

public ThrownItemProjectile(@NotNull Level level, @NotNull LivingEntity shooter) {
super(TTEntityTypes.THROWN_ITEM_PROJECTILE, shooter, level);
public ThrownItemProjectile(@NotNull Level level, LivingEntity shooter, ItemStack stack) {
super(TTEntityTypes.THROWN_ITEM_PROJECTILE, shooter, level, stack);
}

public ThrownItemProjectile(@NotNull Level level, double x, double y, double z) {
super(TTEntityTypes.THROWN_ITEM_PROJECTILE, x, y, z, level);
public ThrownItemProjectile(double x, double y, double z, Level level, @NotNull ItemStack stack) {
super(TTEntityTypes.THROWN_ITEM_PROJECTILE, x, y, z, level, stack);
}

@Override
Expand Down
Loading

0 comments on commit 8f738a0

Please sign in to comment.