Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor and fix PressingBehaviour.java #7168

Open
wants to merge 13 commits into
base: mc1.20.1/dev
Choose a base branch
from
Prev Previous commit
Next Next commit
This is breaking but I want to save this work
  • Loading branch information
Shroopy committed Nov 27, 2024
commit e1816ec8fe01ce7c13c0d5d8bb4936048dbbddce
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class PressingBehaviour extends BeltProcessingBehaviour {
public int prevRunningTicks;
public int runningTicks;
public boolean running;
public boolean finished;
public boolean wentDown;
public Mode mode;

int entityScanCooldown;
Expand Down Expand Up @@ -72,13 +72,13 @@ public <T extends SmartBlockEntity & PressingBehaviourSpecifics> PressingBehavio
public void read(CompoundTag compound, boolean clientPacket) {
running = compound.getBoolean("Running");
mode = Mode.values()[compound.getInt("Mode")];
finished = compound.getBoolean("Finished");
wentDown = compound.getBoolean("WentDown");
prevRunningTicks = runningTicks = compound.getInt("Ticks");
super.read(compound, clientPacket);

if (clientPacket) {
NBTHelper.iterateCompoundList(compound.getList("ParticleItems", Tag.TAG_COMPOUND),
c -> particleItems.add(ItemStack.of(c)));
c -> particleItems.add(ItemStack.of(c)));
spawnParticles();
}
}
Expand All @@ -87,7 +87,7 @@ public void read(CompoundTag compound, boolean clientPacket) {
public void write(CompoundTag compound, boolean clientPacket) {
compound.putBoolean("Running", running);
compound.putInt("Mode", mode.ordinal());
compound.putBoolean("Finished", finished);
compound.putBoolean("WentDown", wentDown);
compound.putInt("Ticks", runningTicks);
super.write(compound, clientPacket);

Expand All @@ -110,6 +110,7 @@ public float getRenderedHeadOffset(float partialTicks) {
public void start(Mode mode) {
this.mode = mode;
running = true;
wentDown = false;
prevRunningTicks = 0;
runningTicks = 0;
particleItems.clear();
Expand All @@ -128,38 +129,40 @@ public boolean onBasin() {
public void tick() {
super.tick();

finished = false;
if (specifics.getKineticSpeed() == 0) {
running = false;
return;
}

Level level = getWorld();

if (level == null)
return;

BlockPos worldPosition = getPos();

if (!running || level == null) {
if (level != null && !level.isClientSide) {
if (!running) {
if (level.isClientSide)
return;

if (entityScanCooldown > 0)
entityScanCooldown--;
if (entityScanCooldown <= 0) {
entityScanCooldown = ENTITY_SCAN;
if (entityScanCooldown > 0)
entityScanCooldown--;
if (entityScanCooldown <= 0) {
entityScanCooldown = ENTITY_SCAN;

if (BlockEntityBehaviour.get(level, worldPosition.below(2),
if (BlockEntityBehaviour.get(level, worldPosition.below(2),
TransportedItemStackHandlerBehaviour.TYPE) != null)
return;
if (BasinBlock.isBasin(level, worldPosition.below(2)))
return;

for (ItemEntity itemEntity : level.getEntitiesOfClass(ItemEntity.class,
new AABB(worldPosition.below()).deflate(.125f))) {
if (!itemEntity.isAlive() || !itemEntity.onGround())
continue;
if (!specifics.tryProcessInWorld(itemEntity, true))
continue;
start(Mode.WORLD);
return;
}
return;
if (BasinBlock.isBasin(level, worldPosition.below(2)))
return;

for (ItemEntity itemEntity : level.getEntitiesOfClass(ItemEntity.class, new AABB(worldPosition.below()).deflate(.125f))) {
if (!itemEntity.isAlive() || !itemEntity.onGround())
continue;
if (!specifics.tryProcessInWorld(itemEntity, true))
continue;
start(Mode.WORLD);
return;
}

}
Expand All @@ -171,7 +174,7 @@ public void tick() {
return;
}

if (runningTicks == CYCLE / 2 && specifics.getKineticSpeed() != 0) {
if (runningTicks >= CYCLE / 2 && !wentDown) {
if (inWorld())
applyInWorld();
if (onBasin())
Expand All @@ -186,22 +189,22 @@ public void tick() {

if (!level.isClientSide)
blockEntity.sendData();

wentDown = true;
}

if (!level.isClientSide && runningTicks > CYCLE) {
finished = true;
wentDown = false;
particleItems.clear();
specifics.onPressingCompleted();
blockEntity.sendData();
prevRunningTicks = runningTicks - 240;
runningTicks += getRunningTickSpeed() - 240;
runningTicks -= 240;
return;
}

prevRunningTicks = runningTicks;
runningTicks += getRunningTickSpeed();
if (prevRunningTicks < CYCLE / 2 && runningTicks >= CYCLE / 2) {
runningTicks = CYCLE / 2;
// Pause the ticks until a packet is received
if (level.isClientSide && !blockEntity.isVirtual())
runningTicks = -(CYCLE / 2);
Expand Down Expand Up @@ -257,13 +260,14 @@ protected void spawnParticles() {

if (mode == Mode.BASIN)
particleItems
.forEach(stack -> makeCompactingParticleEffect(VecHelper.getCenterOf(worldPosition.below(2)), stack));
.forEach(stack -> makeCompactingParticleEffect(VecHelper.getCenterOf(worldPosition.below(2)),
stack));
if (mode == Mode.BELT)
particleItems.forEach(stack -> makePressingParticleEffect(VecHelper.getCenterOf(worldPosition.below(2))
.add(0, 8 / 16f, 0), stack));
.add(0, 8 / 16f, 0), stack));
if (mode == Mode.WORLD)
particleItems.forEach(stack -> makePressingParticleEffect(VecHelper.getCenterOf(worldPosition.below(1))
.add(0, -1 / 4f, 0), stack));
.add(0, -1 / 4f, 0), stack));

particleItems.clear();
}
Expand All @@ -278,10 +282,10 @@ public void makePressingParticleEffect(Vec3 pos, ItemStack stack, int amount) {
return;
for (int i = 0; i < amount; i++) {
Vec3 motion = VecHelper.offsetRandomly(Vec3.ZERO, level.random, .125f)
.multiply(1, 0, 1);
.multiply(1, 0, 1);
motion = motion.add(0, amount != 1 ? 0.125f : 1 / 16f, 0);
level.addParticle(new ItemParticleOption(ParticleTypes.ITEM, stack), pos.x, pos.y - .25f, pos.z, motion.x,
motion.y, motion.z);
motion.y, motion.z);
}
}

Expand All @@ -291,9 +295,9 @@ public void makeCompactingParticleEffect(Vec3 pos, ItemStack stack) {
return;
for (int i = 0; i < 20; i++) {
Vec3 motion = VecHelper.offsetRandomly(Vec3.ZERO, level.random, .175f)
.multiply(1, 0, 1);
.multiply(1, 0, 1);
level.addParticle(new ItemParticleOption(ParticleTypes.ITEM, stack), pos.x, pos.y, pos.z, motion.x,
motion.y + .25f, motion.z);
motion.y + .25f, motion.z);
}
}

Expand Down