Skip to content

Commit

Permalink
some jukebpx fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Uraneptus committed Apr 6, 2024
1 parent 72b4966 commit d18f47c
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ public class Tortoise extends Animal implements GeoEntity, WorkstationAttachable
private final AnimatableInstanceCache instanceCache = GeckoLibUtil.createInstanceCache(this);
int layEggCounter;
FollowJukeboxEntitySoundInstance soundInstance;
long recordTickCount;
long recordStartedTick;
boolean isPlaying;
int ticksSinceLastEvent;

public Tortoise(EntityType<? extends Animal> entityType, Level level) {
super(entityType, level);
Expand Down Expand Up @@ -166,6 +170,8 @@ public void tick() {
super.tick();
Level level = this.level();

this.handleJukeboxTick(this, level);

//Hiding core stuff
if (this.getHideTimerDuration() > 0) {
setHideTimerDuration(getHideTimerDuration() - 1);
Expand Down Expand Up @@ -373,6 +379,46 @@ public void setSoundInstance(FollowJukeboxEntitySoundInstance soundInstance) {
this.soundInstance = soundInstance;
}

@Override
public long getRecordTickCount() {
return this.recordTickCount;
}

@Override
public void setRecordTickCount(long tickCount) {
this.recordTickCount = tickCount;
}

@Override
public long getRecordStartedTick() {
return this.recordStartedTick;
}

@Override
public void setRecordStartedTick(long startedTick) {
this.recordStartedTick = startedTick;
}

@Override
public boolean isRecordPlaying() {
return this.isPlaying;
}

@Override
public void setRecordPlaying(boolean isPlaying) {
this.isPlaying = isPlaying;
}

@Override
public int getTicksSinceLastEvent() {
return this.ticksSinceLastEvent;
}

@Override
public void setTicksSinceLastEvent(int ticksSinceLastEvent) {
this.ticksSinceLastEvent = ticksSinceLastEvent;
}

@Override
public ItemStack getRecordItem() {
return this.entityData.get(RECORD_ITEM);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public class TortoiseShell extends Entity implements OwnableEntity, WorkstationA
private static final EntityDataAccessor<ItemStack> WORKSTATION = SynchedEntityData.defineId(TortoiseShell.class, EntityDataSerializers.ITEM_STACK);
private static final EntityDataAccessor<ItemStack> RECORD_ITEM = SynchedEntityData.defineId(TortoiseShell.class, EntityDataSerializers.ITEM_STACK);
FollowJukeboxEntitySoundInstance soundInstance;
long recordTickCount;
long recordStartedTick;
boolean isPlaying;
int ticksSinceLastEvent;

public TortoiseShell(EntityType<? extends TortoiseShell> pEntityType, Level pLevel) {
super(pEntityType, pLevel);
Expand Down Expand Up @@ -315,10 +319,11 @@ private void blockKnockBack() {

@Override
public void tick() {
Level level = this.level();
if (this.getSpinTicksEntityData() > 0) {
this.hitShield(this.level().getEntities(this, this.getBoundingBox().inflate(0.50D), EntitySelector.NO_CREATIVE_OR_SPECTATOR));
this.hurtEntity(this.level().getEntities(this, this.getBoundingBox().inflate(0.10D), EntitySelector.NO_CREATIVE_OR_SPECTATOR));
this.knockBack(this.level().getEntities(this, this.getBoundingBox().inflate(0.10D), EntitySelector.NO_CREATIVE_OR_SPECTATOR));
this.hitShield(level.getEntities(this, this.getBoundingBox().inflate(0.50D), EntitySelector.NO_CREATIVE_OR_SPECTATOR));
this.hurtEntity(level.getEntities(this, this.getBoundingBox().inflate(0.10D), EntitySelector.NO_CREATIVE_OR_SPECTATOR));
this.knockBack(level.getEntities(this, this.getBoundingBox().inflate(0.10D), EntitySelector.NO_CREATIVE_OR_SPECTATOR));
blockKnockBack();
this.entityData.set(SPIN_TICKS, this.getSpinTicksEntityData() - 1);
while (this.getDeltaMovement().x() < -0.7 || this.getDeltaMovement().z() < -0.7) {
Expand All @@ -338,6 +343,9 @@ public void tick() {

super.tick();


this.handleJukeboxTick(this, level);

if (!this.isNoGravity()) {
double yVelocity = -0.04D;
FluidType fluidType = this.getEyeInFluidType();
Expand All @@ -349,7 +357,7 @@ public void tick() {
this.setDeltaMovement(this.getDeltaMovement().add(0.0D, yVelocity, 0.0D));
}

Level level = this.level();

BlockPos bottomPosition = this.getBlockPosBelowThatAffectsMyMovement();
float friction = this.onGround() ? level.getBlockState(bottomPosition).getFriction(level, bottomPosition, this) * 1.55F : 1.55F;
float defaultFriction = this.level().getBlockState(bottomPosition).getFriction(level, bottomPosition, this);
Expand Down Expand Up @@ -415,64 +423,83 @@ protected void readAdditionalSaveData(CompoundTag pCompound) {
public float getDamage() {
return this.entityData.get(DATA_ID_DAMAGE);
}

public void setDamage(float pDamageTaken) {
this.entityData.set(DATA_ID_DAMAGE, pDamageTaken);
}

public int getHurtTime() {
return this.entityData.get(DATA_ID_HURT);
}

public void setHurtTime(int pHurtTime) {
this.entityData.set(DATA_ID_HURT, pHurtTime);
}

public int getHurtDir() {
return this.entityData.get(DATA_ID_HURTDIR);
}

public void setHurtDir(int pHurtDirection) {
this.entityData.set(DATA_ID_HURTDIR, pHurtDirection);
}

@Override
public ItemStack getPickResult() {
return new ItemStack(this.getDropItem());
}

@Override
public ItemStack getAppliedWorkstation() {
return this.entityData.get(WORKSTATION);
}

@Override
public void setAppliedWorkstation(ItemStack itemStack) {
this.entityData.set(WORKSTATION, itemStack);
}

@Override
public boolean hasAppliedWorkstation() {
return !getAppliedWorkstation().isEmpty();
}

@Override
public ItemStack getRecordItem() {
return this.entityData.get(RECORD_ITEM);
}

@Override
public void setRecordItem(ItemStack itemStack) {
this.entityData.set(RECORD_ITEM, itemStack);
}

@Override
public FollowJukeboxEntitySoundInstance getSoundInstance() {
return this.soundInstance;
}

@Override
public void setSoundInstance(FollowJukeboxEntitySoundInstance soundInstance) {
this.soundInstance = soundInstance;
}
@Override
public long getRecordTickCount() {
return this.tickCount;
}
@Override
public void setRecordTickCount(long tickCount) {
this.recordTickCount = tickCount;
}
@Override
public long getRecordStartedTick() {
return this.recordStartedTick;
}
@Override
public void setRecordStartedTick(long startedTick) {
this.recordStartedTick = startedTick;
}
@Override
public boolean isRecordPlaying() {
return this.isPlaying;
}
@Override
public void setRecordPlaying(boolean isPlaying) {
this.isPlaying = isPlaying;
}
@Override
public int getTicksSinceLastEvent() {
return this.ticksSinceLastEvent;
}
@Override
public void setTicksSinceLastEvent(int ticksSinceLastEvent) {
this.ticksSinceLastEvent = ticksSinceLastEvent;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import com.uraneptus.sullysmod.core.other.SMItemUtil;
import com.uraneptus.sullysmod.core.other.tags.SMItemTags;
import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.stats.Stats;
import net.minecraft.tags.ItemTags;
Expand All @@ -15,8 +18,12 @@
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.ContainerLevelAccess;
import net.minecraft.world.inventory.CraftingMenu;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.RecordItem;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.gameevent.GameEvent;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.network.NetworkHooks;
import org.jetbrains.annotations.NotNull;

Expand All @@ -34,15 +41,29 @@ default boolean isJukebox() {
void setRecordItem(ItemStack itemStack);
FollowJukeboxEntitySoundInstance getSoundInstance();
void setSoundInstance(FollowJukeboxEntitySoundInstance itemStack);
long getRecordTickCount();
void setRecordTickCount(long tickCount);
long getRecordStartedTick();
void setRecordStartedTick(long startedTick);
boolean isRecordPlaying();
void setRecordPlaying(boolean isPlaying);
int getTicksSinceLastEvent();
void setTicksSinceLastEvent(int ticksSinceLastEvent);

default void addSaveData(@NotNull CompoundTag nbt) {
nbt.put("AppliedWorkstation", this.getAppliedWorkstation().save(new CompoundTag()));
nbt.put("RecordItem", this.getRecordItem().save(new CompoundTag()));
nbt.putBoolean("IsPlaying", this.isRecordPlaying());
nbt.putLong("RecordStartTick", this.getRecordStartedTick());
nbt.putLong("TickCount", this.getRecordTickCount());
}

default void readSaveData(@NotNull CompoundTag nbt) {
this.setAppliedWorkstation(ItemStack.of(nbt.getCompound("AppliedWorkstation")));
this.setRecordItem(ItemStack.of(nbt.getCompound("RecordItem")));
this.setRecordPlaying(nbt.getBoolean("IsPlaying"));
this.setRecordStartedTick(nbt.getLong("RecordStartTick"));
this.setRecordTickCount(nbt.getLong("TickCount"));
}

InteractionResult customInteraction(Player pPlayer, @NotNull InteractionHand pHand);
Expand All @@ -58,6 +79,9 @@ default InteractionResult workstationInteraction(Player pPlayer, @NotNull Intera
if (!entity.level().isClientSide()) {
pPlayer.addItem(getRecordItem());
setRecordItem(ItemStack.EMPTY);
this.setRecordPlaying(false);
this.setRecordTickCount(0);
this.setTicksSinceLastEvent(0);
} else {
Minecraft.getInstance().getSoundManager().stop(getSoundInstance());
}
Expand All @@ -82,6 +106,9 @@ default InteractionResult workstationInteraction(Player pPlayer, @NotNull Intera
if (!entity.level().isClientSide()) {
pPlayer.addItem(getRecordItem());
setRecordItem(ItemStack.EMPTY);
this.setRecordPlaying(false);
this.setRecordTickCount(0);
this.setTicksSinceLastEvent(0);
} else {
Minecraft.getInstance().getSoundManager().stop(getSoundInstance());
}
Expand All @@ -92,6 +119,8 @@ default InteractionResult workstationInteraction(Player pPlayer, @NotNull Intera
setRecordItem(recordItem.getDefaultInstance());
SMItemUtil.nonCreativeShrinkStack(pPlayer, itemInHand);
setSoundInstance(new FollowJukeboxEntitySoundInstance(entity, recordItem.getSound()));
this.setRecordStartedTick(this.getRecordTickCount());
this.setRecordPlaying(true);

if (entity.level().isClientSide()) {
Minecraft mc = Minecraft.getInstance();
Expand Down Expand Up @@ -135,6 +164,30 @@ public boolean stillValid(Player pPlayer) {
}, entity.getName().copy().append(" Crafting")));
}

default void handleJukeboxTick(Entity entity, Level level) {
if (!isJukebox()) return;
BlockPos pos = entity.blockPosition();
this.setTicksSinceLastEvent(1 + this.getTicksSinceLastEvent());
if (!this.getRecordItem().isEmpty() && this.isRecordPlaying()) {
if (this.getRecordItem().getItem() instanceof RecordItem recorditem) {
if (this.getRecordTickCount() >= this.getRecordStartedTick() + (long)recorditem.getLengthInTicks() + 20L) {
this.setRecordPlaying(false);
level.gameEvent(GameEvent.JUKEBOX_STOP_PLAY, pos, GameEvent.Context.of(entity));
level.levelEvent(1011, pos, 0);
} else if (getTicksSinceLastEvent() >= 20) {
this.setTicksSinceLastEvent(0);
level.gameEvent(GameEvent.JUKEBOX_PLAY, pos, GameEvent.Context.of(entity));
if (level instanceof ServerLevel serverlevel) {
Vec3 vec3 = Vec3.atBottomCenterOf(pos).add(0.0D, 1.2F, 0.0D);
float xOffset = (float)level.getRandom().nextInt(4) / 24.0F;
serverlevel.sendParticles(ParticleTypes.NOTE, vec3.x(), vec3.y(), vec3.z(), 0, xOffset, 0.0D, 0.0D, 1.0D);
}
}
}
}
this.setRecordTickCount(1 + getRecordTickCount());
}

default void handleServerRemoval(Entity entity) {
if (this.hasAppliedWorkstation()) {
entity.spawnAtLocation(new ItemStack(getAppliedWorkstation().getItem()));
Expand All @@ -143,6 +196,9 @@ default void handleServerRemoval(Entity entity) {
entity.spawnAtLocation(new ItemStack(getRecordItem().getItem()));
setRecordItem(ItemStack.EMPTY);
}
this.setRecordPlaying(false);
this.setRecordTickCount(0);
this.setTicksSinceLastEvent(0);
}
}
}

0 comments on commit d18f47c

Please sign in to comment.