Skip to content

Commit

Permalink
Star Catcher networking
Browse files Browse the repository at this point in the history
  • Loading branch information
weirtz committed Aug 22, 2023
1 parent ecc4949 commit c3b3078
Show file tree
Hide file tree
Showing 18 changed files with 2,028 additions and 66 deletions.
885 changes: 885 additions & 0 deletions hs_err_pid25856.log

Large diffs are not rendered by default.

854 changes: 854 additions & 0 deletions hs_err_pid34324.log

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.seabreyh.mana.ManaMod;
import com.seabreyh.mana.content.entities.FallenStarEntity;
import com.seabreyh.mana.foundation.client.gui.screens.StarCatcherMenu;
import com.seabreyh.mana.foundation.networking.ManaMessages;
import com.seabreyh.mana.foundation.networking.packet.FallenStarS2CPacket;
import com.seabreyh.mana.registries.ManaBlockEntities;
import com.seabreyh.mana.registries.ManaItems;

Expand All @@ -16,7 +18,7 @@
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;

import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.Mth;
import net.minecraft.world.Containers;
import net.minecraft.world.MenuProvider;
Expand All @@ -40,8 +42,8 @@
public class BlockEntityStarCatcher extends BlockEntity implements MenuProvider {
public int tickCount;
public int catchCount;
private float activeRotation;
private float rotationSpeed = 6F;
public float activeRotation;
public float rotationSpeed = 1F;
private final List<BlockPos> effectBlocks = Lists.newArrayList();

private final ItemStackHandler itemHandler = new ItemStackHandler(4) {
Expand All @@ -67,9 +69,14 @@ public float getRotationSpeed() {
return rotationSpeed;
}

public static void setRotationSpeed(float speed, BlockEntityStarCatcher entity) {
entity.rotationSpeed = speed;
}
// public static void setRotationSpeed(float speed, BlockEntityStarCatcher
// entity) {
// entity.rotationSpeed = speed;
// }

// private void setRotationSpeedInternal(float speed) {
// this.rotationSpeed = speed;
// }

@Nullable
@Override
Expand Down Expand Up @@ -122,19 +129,27 @@ public void drops() {
}

public static void tick(Level pLevel, BlockPos pPos, BlockState pState, BlockEntityStarCatcher pBlockEntity) {
if (pLevel.isClientSide()) {

if (pLevel.isClientSide) {
// ManaMod.LOGGER
// .info("ID" + pBlockEntity.worldPosition.getZ() + " speed: " +
// pBlockEntity.rotationSpeed
// + " catchCount: "
// + pBlockEntity.catchCount);

if (pBlockEntity.catchCount > 0) {
float modifier = Math.min((float) pBlockEntity.catchCount * 10.0f, 20.0f);
BlockEntityStarCatcher.setRotationSpeed(6.0f + modifier, pBlockEntity);
pBlockEntity.rotationSpeed = (3.0f + modifier);
} else {
BlockEntityStarCatcher.setRotationSpeed(3.0F, pBlockEntity);

pBlockEntity.rotationSpeed = (1.0f);
}
}

locateStars(pLevel, pBlockEntity, pPos, pState);
pBlockEntity.locateStars(pLevel, pPos, pState, pBlockEntity);

List<BlockPos> list = pBlockEntity.effectBlocks;
animationTick(pLevel, pPos, list, pBlockEntity.tickCount);
// animationTick(pLevel, pPos, list, pBlockEntity.tickCount);
++pBlockEntity.activeRotation;
}

Expand All @@ -143,29 +158,32 @@ private static void animationTick(Level p_155419_, BlockPos p_155420_, List<Bloc
d0 = (d0 * d0 + d0) * (double) 0.3F;
}

public float getActiveRotation(float p_59198_) {
return (this.activeRotation + p_59198_) * -0.0375F * getRotationSpeed();
}

private static void locateStars(Level plevel, BlockEntityStarCatcher pBlockEntity, BlockPos pPos,
BlockState pState) {

private static void locateStars(Level plevel, BlockPos pPos, BlockState pState,
BlockEntityStarCatcher pBlockEntity) {
if (!plevel.isClientSide()) {
if (hasNotReachedStackLimit(pBlockEntity)) {

if (pBlockEntity.hasNotReachedStackLimit()) {

AABB area = pBlockEntity.getRenderBoundingBox().inflate(80.0D, 80.0D, 80.0D);
List<FallenStarEntity> fallenStars = plevel.getEntitiesOfClass(FallenStarEntity.class, area);

for (FallenStarEntity foundStar : fallenStars) {
// make sure star can only be targeted by one star catcher
if (foundStar.isFalling == false
&& foundStar.getIsTargeted() == false) {

if (foundStar.isFalling == false) {

if (foundStar instanceof FallenStarEntity) {

if (foundStar.getIsTargeted() == false) {

ManaMod.LOGGER.info("FOUDN STAR: " + foundStar);
foundStar.setIsTargeted(true);
foundStar.toStarCatcher(pBlockEntity.getBlockPos(), pBlockEntity);
pBlockEntity.catchCount++;
foundStar.toStarCatcher(pBlockEntity.getBlockPos());

if (plevel instanceof ServerLevel)
ManaMessages.sendToNear(plevel, pBlockEntity.getBlockPos(), 60,
new FallenStarS2CPacket(pBlockEntity.getBlockPos(), foundStar.getId()));
// this.catchCount++;
}
}

Expand All @@ -179,7 +197,7 @@ private static void locateStars(Level plevel, BlockEntityStarCatcher pBlockEntit
for (FallenStarEntity foundStar : fallenStars) {
if (foundStar.getIsTargeted()) {
foundStar.stopStarCatch();
pBlockEntity.catchCount--;
// this.catchCount--;
}
}
}
Expand All @@ -192,23 +210,29 @@ private static void locateStars(Level plevel, BlockEntityStarCatcher pBlockEntit

pBlockEntity.catchCount = 0;
for (FallenStarEntity foundStar : fallenStars) {
pBlockEntity.catchCount++;

// Dummy statement to get linter to not complain about unused vars
foundStar.equals(foundStar);
// ManaMod.LOGGER.info("Stars catcher: " + foundStar.getCatcher() + " this
// catcher: " + pBlockEntity);

if (foundStar.getIsTargeted() && foundStar.getCatcher() == pBlockEntity) {
++pBlockEntity.catchCount;
}

// // Dummy statement to get linter to not complain about unused vars
// foundStar.equals(foundStar);
}
}
}

public static void craftItem(BlockEntityStarCatcher entity) {
if (hasNotReachedStackLimit(entity)) {
entity.itemHandler.setStackInSlot(0, new ItemStack(ManaItems.FALLEN_STAR_ITEM.get(),
entity.itemHandler.getStackInSlot(0).getCount() + 1));
public void craftItem() {
if (hasNotReachedStackLimit()) {
this.itemHandler.setStackInSlot(0, new ItemStack(ManaItems.FALLEN_STAR_ITEM.get(),
this.itemHandler.getStackInSlot(0).getCount() + 1));
}
}

private static boolean hasNotReachedStackLimit(BlockEntityStarCatcher entity) {
return entity.itemHandler.getStackInSlot(0).getCount() < entity.itemHandler.getStackInSlot(0).getMaxStackSize();
private boolean hasNotReachedStackLimit() {
return this.itemHandler.getStackInSlot(0).getCount() < this.itemHandler.getStackInSlot(0).getMaxStackSize();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
import com.seabreyh.mana.foundation.client.renderers.entity.FallenStarSyncData;
import com.seabreyh.mana.foundation.event.player.PlayerWishEvent;
import com.seabreyh.mana.foundation.event.world.ShootingStarEvent;
import com.seabreyh.mana.foundation.networking.ManaMessages;
import com.seabreyh.mana.foundation.networking.packet.FallenStarS2CPacket;
import com.seabreyh.mana.foundation.networking.packet.ManaStatSyncS2CPacket;
import com.seabreyh.mana.registries.ManaEntityDataSerializers;
import com.seabreyh.mana.registries.ManaParticles;

import net.minecraft.core.BlockPos;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.network.protocol.game.ClientboundGameEventPacket;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.EntityDataSerializers;
Expand All @@ -38,6 +42,7 @@
import net.minecraft.world.level.ClipContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.gameevent.GameEvent;
import net.minecraft.world.phys.AABB;
Expand Down Expand Up @@ -90,7 +95,6 @@ protected AbstractStarEntity(EntityType<? extends AbstractStarEntity> entityType
this.isFalling = true;
this.dimensions = entityType.getDimensions();

ManaMod.LOGGER.info("[Mana]: Creating AbstractStarEntity");
}

public AbstractStarEntity(EntityType<? extends AbstractStarEntity> getEntity, Level world, Player ownPlayer) {
Expand All @@ -101,6 +105,7 @@ public AbstractStarEntity(EntityType<? extends AbstractStarEntity> getEntity, Le

@Override
public void tick() {
ManaMod.LOGGER.info("catcher: " + this.catcher);

this.baseTick();

Expand Down Expand Up @@ -142,7 +147,7 @@ public void tick() {
// If within 0.7 blocks of catcher, catch and discard entity.
if (Math.abs(vec3.x) < 0.7 && Math.abs(vec3.y) < 0.7 && Math.abs(vec3.z) < 0.7) {
this.playSound(SoundEvents.BOTTLE_FILL_DRAGONBREATH, 2.0F, 1.0F);
BlockEntityStarCatcher.craftItem(catcher);
catcher.craftItem();
discard();
}
}
Expand Down Expand Up @@ -502,10 +507,11 @@ public boolean isNoPhysics() {
}
}

public void toStarCatcher(BlockPos catcherPos, BlockEntityStarCatcher pBlockEntity) {
public void toStarCatcher(BlockPos catcherPos) {
// MAKE THIS RUN IN CLIENT TOO WHEN CALLED FROM SERVER
ManaMod.LOGGER.info("To catcher");
this.catcherPos = catcherPos;
setCatcher(pBlockEntity);
setCatcher(catcherPos);
this.moveToCatcher = true;
this.isFalling = false;
}
Expand Down Expand Up @@ -657,13 +663,21 @@ protected void defineSynchedData() {
public void readAdditionalSaveData(CompoundTag compoundTag) {
super.readAdditionalSaveData(compoundTag);
this.moveToCatcher = compoundTag.getBoolean("moveToCatcher");
if (compoundTag.contains("catcherPos")) {
catcherPos = NbtUtils.readBlockPos(compoundTag.getCompound("catcherPos"));
setCatcher(catcherPos);
}
}

// Runs when saving/pausing/leaving world - Save entity data
@Override
public void addAdditionalSaveData(CompoundTag compoundTag) {
super.addAdditionalSaveData(compoundTag);
compoundTag.putBoolean("moveToCatcher", this.moveToCatcher);
if (catcherPos != null) {
compoundTag.put("catcherPos", NbtUtils.writeBlockPos(catcherPos));
}

}

// Data Sync Getters/Setters---------------------
Expand Down Expand Up @@ -750,7 +764,7 @@ public boolean getIsTargeted() {
// isTargeted SETTER
public void setIsTargeted(boolean bool) {
this.isTargeted = bool;
this.updateSyncedData("isTargeted");
// this.updateSyncedData("isTargeted");
}

// catcher GETTER - returns client/server value
Expand All @@ -763,9 +777,14 @@ public BlockEntityStarCatcher getCatcher() {
}

// catcher SETTER
public void setCatcher(BlockEntityStarCatcher starCatcher) {
this.catcher = starCatcher;
this.updateSyncedData("catcher");
public void setCatcher(BlockPos catcherPos) {

BlockEntity blockEntity = level().getBlockEntity(catcherPos);
if (blockEntity instanceof BlockEntityStarCatcher) {
this.catcher = (BlockEntityStarCatcher) blockEntity;
} else {
ManaMod.LOGGER.warn("setCatcher() called with invalid starCatcher: " + catcherPos);
}
}

// moveToCatcher GETTER - returns client/server value
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.seabreyh.mana.content.items;

import com.seabreyh.mana.content.mana_stat.PlayerManaStatProvider;
import com.seabreyh.mana.foundation.event.player.PlayerManaEvent;
import com.seabreyh.mana.foundation.mana_stat.PlayerManaStatProvider;

import java.util.List;
import java.util.Random;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.seabreyh.mana.foundation.client;

import com.seabreyh.mana.content.blocks.block_entities.BlockEntityStarCatcher;

import net.minecraft.core.BlockPos;

public class ClientFallenStarData {
private static BlockPos catcherPos;

public static void setCatcherPos(BlockPos catcherPos) {
ClientFallenStarData.catcherPos = catcherPos;
}

public static BlockPos getCatcherPos() {
return catcherPos;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.seabreyh.mana.foundation.client.renderers.block_entity;
package com.seabreyh.mana.foundation.client.renderers.block_entities;

import com.seabreyh.mana.ManaMod;
import com.seabreyh.mana.content.blocks.block_entities.BlockEntityStarCatcher;
Expand Down Expand Up @@ -41,7 +41,7 @@ public RendererBlockEntityStarCatcher(BlockEntityRendererProvider.Context contex
this.cube = modelpart.getChild("cube");
this.context = context;
}

public static LayerDefinition createBodyLayer() {
MeshDefinition meshdefinition = new MeshDefinition();
PartDefinition partdefinition = meshdefinition.getRoot();
Expand All @@ -57,7 +57,8 @@ public void render(BlockEntityStarCatcher entityBlock, float partialTicks, PoseS
MultiBufferSource buffer, int combinedOverlay, int packedLight) {

stack.pushPose();
float f1 = entityBlock.getActiveRotation(partialTicks) * (180F / (float) Math.PI);
float f1 = ((entityBlock.activeRotation + partialTicks) * -0.0375F * entityBlock.rotationSpeed)
* (180F / (float) Math.PI);

VertexConsumer vertexconsumer = buffer.getBuffer(RenderType.entityTranslucent(TEXTURE, true));
stack.pushPose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public void write(FriendlyByteBuf buffer) {
buffer.writeBoolean(noPhysics);
}

public void read(FriendlyByteBuf buffer) {
noPhysics = buffer.readBoolean();
public BlockEntityStarCatcher read(FriendlyByteBuf buffer) {
return catcher;
}

}
Loading

0 comments on commit c3b3078

Please sign in to comment.