Skip to content

Commit

Permalink
Fallen Star dispenser block functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
weirtz committed Aug 24, 2023
1 parent cf4e129 commit b96c9e8
Show file tree
Hide file tree
Showing 7 changed files with 977 additions and 33 deletions.
856 changes: 856 additions & 0 deletions hs_err_pid31984.log

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -170,20 +170,16 @@ private static void locateStars(Level plevel, BlockPos pPos, BlockState pState,
for (FallenStarEntity foundStar : fallenStars) {
// make sure star can only be targeted by one star catcher

if (foundStar.isFalling == false) {

if (foundStar instanceof FallenStarEntity) {

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

foundStar.setIsTargeted(true);
foundStar.toStarCatcher(pBlockEntity.getBlockPos());

if (plevel instanceof ServerLevel)
ManaMessages.sendToNear(plevel, pBlockEntity.getBlockPos(), 60,
new FallenStarS2CPacket(pBlockEntity.getBlockPos(), foundStar.getId()));
if (foundStar.isInGround()) {

if (foundStar.getIsTargeted() == false) {
if (plevel instanceof ServerLevel) {
ManaMessages.sendToNear(plevel, pBlockEntity.getBlockPos(), 60,
new FallenStarS2CPacket(pBlockEntity.getBlockPos(), foundStar.getId()));
}
foundStar.setIsTargeted(true);
foundStar.toStarCatcher(pBlockEntity.getBlockPos());

}

}
Expand Down Expand Up @@ -230,7 +226,7 @@ public void craftItem() {
}
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,19 @@
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraftforge.registries.RegistryObject;

public abstract class AbstractStarEntity extends AbstractArrow {

// protected boolean inGround;
private int age; // server var
private final int maxAge = 23000;
private double catchSpeed = 0.2D;
private double catchSpeed = 0.8D;
private int timeTillStartCatch = 130;
private boolean moveToCatcher;
public boolean moveToCatcher;
private boolean isTargeted;
private BlockEntityStarCatcher catcher;
private BlockPos catcherPos;
public BlockEntityStarCatcher catcher;
public BlockPos catcherPos;
private int clientSideCatchStarTickCount;
public Boolean isFalling;
private Player ownPlayer;
Expand All @@ -74,7 +75,7 @@ public abstract class AbstractStarEntity extends AbstractArrow {
private float waterInertia = 0.7F;
private EntityDimensions dimensions;
float currentTime;
protected int inGroundTime;

private Vec3 lastPosition = this.position();
private double travelDistance;
private SoundEvent soundEvent = this.getDefaultHitGroundSoundEvent();
Expand Down Expand Up @@ -103,14 +104,22 @@ public AbstractStarEntity(EntityType<? extends AbstractStarEntity> getEntity, Le

}

protected AbstractStarEntity(EntityType<? extends AbstractStarEntity> p_36711_, double p_36712_, double p_36713_,
double p_36714_, Level p_36715_) {
this(p_36711_, p_36715_);
this.setPos(p_36712_, p_36713_, p_36714_);
}

@Override
public void tick() {

this.baseTick();

// If moving to catcher and catcher is removed, stop the catch
if (moveToCatcher && catcher.isRemoved()) {
stopStarCatch();
if (catcher != null) {
// If moving to catcher and catcher is removed or full, stop the catch
if (catcher.isRemoved() || !catcher.hasNotReachedStackLimit()) {
stopStarCatch();
}
}

// distance for splash particles
Expand Down Expand Up @@ -146,7 +155,9 @@ public void tick() {
discard();
}
}
++this.clientSideCatchStarTickCount;
if (this.inGround) {
++this.clientSideCatchStarTickCount;
}

// despawn checker
currentTime = this.level().getTimeOfDay(1.0F);
Expand Down Expand Up @@ -402,7 +413,6 @@ protected void doWaterSplashEffect() {
.sqrt(vec3.x * vec3.x * (double) 0.2F + vec3.y * vec3.y + vec3.z * vec3.z * (double) 0.2F)
* f);

ManaMod.LOGGER.info("f1 " + f1);
if (f1 < 0.22F) {
// big splash
this.playSound(this.getSwimSplashSound(), f1,
Expand Down Expand Up @@ -495,6 +505,7 @@ public void toStarCatcher(BlockPos catcherPos) {
setCatcher(catcherPos);
this.moveToCatcher = true;
this.isFalling = false;

}

@Override
Expand Down Expand Up @@ -598,6 +609,8 @@ public void stopStarCatch() {
this.setNoPhysics(false);
this.isTargeted = false;
this.pickup = AbstractArrow.Pickup.ALLOWED;
this.catcher = null;
this.catcherPos = null;
}

// Despawn timer
Expand Down Expand Up @@ -728,14 +741,19 @@ public void setCatcher(BlockPos catcherPos) {
if (blockEntity instanceof BlockEntityStarCatcher) {
this.catcher = (BlockEntityStarCatcher) blockEntity;
} else {
ManaMod.LOGGER.warn("setCatcher() called with invalid starCatcher: " + catcherPos);
ManaMod.LOGGER
.warn("setCatcher() called with invalid starCatcher: " + catcherPos + "cathcer : " + blockEntity);
}
}

// ---------------------------------
// BASIC SETTERS AND GETTERS
// ---------------------------------

public boolean isInGround() {
return this.inGround;
}

public int getAge() {
return this.age;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,43 @@
package com.seabreyh.mana.content.entities;

import com.seabreyh.mana.ManaMod;
import com.seabreyh.mana.registries.ManaEntities;
import com.seabreyh.mana.registries.ManaItems;

import net.minecraft.core.BlockPos;
import net.minecraft.core.BlockSource;
import net.minecraft.core.Direction;
import net.minecraft.core.Position;
import net.minecraft.core.dispenser.DefaultDispenseItemBehavior;
import net.minecraft.core.dispenser.DispenseItemBehavior;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.projectile.AbstractArrow;
import net.minecraft.world.entity.projectile.Projectile;
import net.minecraft.world.entity.vehicle.AbstractMinecart;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.BaseRailBlock;
import net.minecraft.world.level.block.DispenserBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.RailShape;

public class FallenStarEntity extends AbstractStarEntity {

public FallenStarEntity(EntityType<? extends FallenStarEntity> p_19870_, Level p_19871_) {
super(p_19870_, p_19871_);

}

public FallenStarEntity(EntityType<? extends FallenStarEntity> p_19870_, Level p_19871_, Player player) {
super(p_19870_, p_19871_, player);
}

public FallenStarEntity(Level p_36861_, double p_36862_, double p_36863_, double p_36864_) {
super(ManaEntities.FALLEN_STAR.get(), p_36862_, p_36863_, p_36864_, p_36861_);
}

public void tick() {
super.tick();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,44 @@
import javax.annotation.Nullable;

import com.seabreyh.mana.ManaMod;
import com.seabreyh.mana.content.entities.FallenStarEntity;
import com.seabreyh.mana.foundation.client.renderers.item.ManaItemStackRenderer;
import com.seabreyh.mana.foundation.event.player.PlayerManaEvent;

import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.BlockEntityWithoutLevelRenderer;
import net.minecraft.core.BlockSource;
import net.minecraft.core.dispenser.DefaultDispenseItemBehavior;
import net.minecraft.core.dispenser.DispenseItemBehavior;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.util.RandomSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.projectile.AbstractArrow;
import net.minecraft.world.entity.projectile.Arrow;
import net.minecraft.world.entity.projectile.Projectile;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.DispenserBlock;
import net.minecraftforge.client.extensions.common.IClientItemExtensions;

import net.minecraft.core.BlockPos;
import net.minecraft.core.BlockSource;
import net.minecraft.core.Direction;
import net.minecraft.core.Position;
import net.minecraft.core.dispenser.DefaultDispenseItemBehavior;
import net.minecraft.core.dispenser.DispenseItemBehavior;
import net.minecraft.tags.BlockTags;

public class FallenStarItem extends Item {

public FallenStarItem(Properties p_41383_) {
super(p_41383_);
DispenserBlock.registerBehavior(this, DISPENSER_BEHAVIOR);
}

// Use Block Entity model for Item model
Expand All @@ -35,8 +52,8 @@ public void initializeClient(java.util.function.Consumer<IClientItemExtensions>
consumer.accept((IClientItemExtensions) ManaMod.PROXY.getISTERProperties());
}

// Right click functionality
@Override
// Called when player right clicks star
public InteractionResultHolder<ItemStack> use(Level world, Player player,
InteractionHand hand) {
ItemStack itemstack = player.getItemInHand(hand);
Expand Down Expand Up @@ -69,8 +86,46 @@ private void playSound(Level level, Player player) {
(random.nextFloat() - random.nextFloat()) * 0.2F + 1.5F);
}

// Make fuel source for furnace
@Override
public int getBurnTime(ItemStack itemStack, @Nullable RecipeType<?> recipeType) {
return 3200;
}

// Block Dispenser functionality
private static final DispenseItemBehavior DISPENSER_BEHAVIOR = new DefaultDispenseItemBehavior() {

@Override
public ItemStack execute(BlockSource source, ItemStack stack) {
Level level = source.getLevel();
Position position = DispenserBlock.getDispensePosition(source);
Direction direction = source.getBlockState().getValue(DispenserBlock.FACING);
Projectile projectile = this.getProjectile(level, position, stack);
projectile.shoot((double) direction.getStepX(), (double) ((float) direction.getStepY() + 0.1F),
(double) direction.getStepZ(), this.getPower(), this.getUncertainty());
level.addFreshEntity(projectile);
stack.shrink(1);
return stack;
}

protected Projectile getProjectile(Level level, Position pos, ItemStack stack) {
FallenStarEntity fallenStar = new FallenStarEntity(level, pos.x(), pos.y(), pos.z());
fallenStar.pickup = AbstractArrow.Pickup.ALLOWED;
return fallenStar;
}

@Override
protected void playSound(BlockSource source) {
source.getLevel()
.levelEvent(1000, source.getPos(), 0);
}

protected float getUncertainty() {
return 6.0F;
}

protected float getPower() {
return 1.1F;
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,11 @@ public void render(T fallenStar, float floatOne, float floatTwo, PoseStack poseS
this.renderNameTag(fallenStar, Component.literal(
"ID:" + fallenStar.getId() +
" Age:" + fallenStar.getAge() +
" PickUp:" + fallenStar.pickup
// " Targeted:" + fallenStar.getIsTargeted() +
// " MoveToCatcher: " + fallenStar.getMoveToCatcher() +
// " isFalling: " + fallenStar.isFalling +
// " moveToCatcher: " + fallenStar.getSyncMoveToCatcher() + " catcher: "
// + fallenStar.getCatcher()
" PickUp:" + fallenStar.pickup +
" Targeted:" + fallenStar.getIsTargeted() +
" MoveToCatcher: " + fallenStar.moveToCatcher +
" isFalling: " + fallenStar.isFalling +
" catcher: " + fallenStar.catcherPos
+ " [CLIENT THREAD]"

), poseStack, multBuff, intOne);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public FallenStarS2CPacket(FriendlyByteBuf buf) {
}

public void toBytes(FriendlyByteBuf buf) {
// buf.writeInt(star_catcher);
buf.writeBlockPos(catcherPos);
buf.writeInt(entityId);
}
Expand All @@ -54,7 +53,9 @@ public boolean handle(Supplier<NetworkEvent.Context> supplier) {
fsE.setIsTargeted(true);

} else {
ManaMod.LOGGER.warn("setCatcher() called with invalid starCatcher: " + catcherPos);
ManaMod.LOGGER.warn(
"HANDEL() called with invalid Entity Lookup by id is: " + entityByID + " Entity Number(id):"
+ entityId);
}
ClientFallenStarData.setCatcherPos(catcherPos);
});
Expand Down

0 comments on commit b96c9e8

Please sign in to comment.