From 1edda9b56429f4188cb3bcaefd8d05332adbf50b Mon Sep 17 00:00:00 2001 From: simibubi <31564874+simibubi@users.noreply.github.com> Date: Sun, 15 Mar 2020 12:42:36 +0100 Subject: [PATCH] Compare, but do not fall - Yet another attempt at preventing elevator fall damage - Flexcrate now has comparator output - Basin now has comparator output - Kinetic Gauges now have comparator output - Stress Gauge -> Stressometer --- .../java/com/simibubi/create/AllPackets.java | 2 ++ .../create/foundation/item/ItemHelper.java | 30 ++++++++++++++++ .../contraptions/CancelPlayerFallPacket.java | 34 ++++++++++++++++++ .../contraptions/ContraptionCollider.java | 13 +++++++ .../piston/LinearActuatorTileEntity.java | 15 ++++++-- .../piston/MechanicalPistonTileEntity.java | 1 - .../contraptions/pulley/PulleyTileEntity.java | 1 + .../contraptions/processing/BasinBlock.java | 35 +++++++++++++------ .../contraptions/relays/gauge/GaugeBlock.java | 16 +++++++++ .../relays/gauge/SpeedGaugeTileEntity.java | 2 ++ .../relays/gauge/StressGaugeTileEntity.java | 13 ++++--- .../block/inventories/FlexcrateBlock.java | 16 +++++++++ .../resources/assets/create/lang/en_us.json | 12 +++---- 13 files changed, 166 insertions(+), 24 deletions(-) create mode 100644 src/main/java/com/simibubi/create/modules/contraptions/components/contraptions/CancelPlayerFallPacket.java diff --git a/src/main/java/com/simibubi/create/AllPackets.java b/src/main/java/com/simibubi/create/AllPackets.java index 00903d8df..a7ad8772e 100644 --- a/src/main/java/com/simibubi/create/AllPackets.java +++ b/src/main/java/com/simibubi/create/AllPackets.java @@ -11,6 +11,7 @@ import com.simibubi.create.foundation.item.AbstractToolItem; import com.simibubi.create.foundation.packet.NbtPacket; import com.simibubi.create.foundation.packet.SimplePacketBase; import com.simibubi.create.foundation.utility.ServerSpeedProvider; +import com.simibubi.create.modules.contraptions.components.contraptions.CancelPlayerFallPacket; import com.simibubi.create.modules.contraptions.components.contraptions.ContraptionStallPacket; import com.simibubi.create.modules.contraptions.relays.advanced.sequencer.ConfigureSequencedGearshiftPacket; import com.simibubi.create.modules.curiosities.symmetry.SymmetryEffectPacket; @@ -41,6 +42,7 @@ public enum AllPackets { CONFIGURE_FILTER(FilterScreenPacket.class, FilterScreenPacket::new), CONFIGURE_FILTERING_AMOUNT(FilteringCountUpdatePacket.class, FilteringCountUpdatePacket::new), CONFIGURE_SCROLLABLE(ScrollValueUpdatePacket.class, ScrollValueUpdatePacket::new), + CANCEL_FALL(CancelPlayerFallPacket.class, CancelPlayerFallPacket::new), // Server to Client SYMMETRY_EFFECT(SymmetryEffectPacket.class, SymmetryEffectPacket::new), diff --git a/src/main/java/com/simibubi/create/foundation/item/ItemHelper.java b/src/main/java/com/simibubi/create/foundation/item/ItemHelper.java index 6ebc10ffd..b75dcffc7 100644 --- a/src/main/java/com/simibubi/create/foundation/item/ItemHelper.java +++ b/src/main/java/com/simibubi/create/foundation/item/ItemHelper.java @@ -5,6 +5,8 @@ import java.util.List; import java.util.function.Function; import java.util.function.Predicate; +import javax.annotation.Nullable; + import org.apache.commons.lang3.mutable.MutableInt; import org.apache.commons.lang3.tuple.Pair; @@ -13,6 +15,7 @@ import com.simibubi.create.config.AllConfigs; import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.Ingredient; import net.minecraft.util.NonNullList; +import net.minecraft.util.math.MathHelper; import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.ItemHandlerHelper; @@ -55,6 +58,33 @@ public class ItemHelper { return true; } + public static int calcRedstoneFromInventory(@Nullable IItemHandler inv) { + if (inv == null) + return 0; + int i = 0; + float f = 0.0F; + int totalSlots = inv.getSlots(); + + for (int j = 0; j < inv.getSlots(); ++j) { + int slotLimit = inv.getSlotLimit(j); + if (slotLimit == 0) { + totalSlots--; + continue; + } + ItemStack itemstack = inv.getStackInSlot(j); + if (!itemstack.isEmpty()) { + f += (float) itemstack.getCount() / (float) Math.min(slotLimit, itemstack.getMaxStackSize()); + ++i; + } + } + + if (totalSlots == 0) + return 0; + + f = f / totalSlots; + return MathHelper.floor(f * 14.0F) + (i > 0 ? 1 : 0); + } + public static List> condenseIngredients(NonNullList recipeIngredients) { List> actualIngredients = new ArrayList<>(); Ingredients: for (Ingredient igd : recipeIngredients) { diff --git a/src/main/java/com/simibubi/create/modules/contraptions/components/contraptions/CancelPlayerFallPacket.java b/src/main/java/com/simibubi/create/modules/contraptions/components/contraptions/CancelPlayerFallPacket.java new file mode 100644 index 000000000..523c0ee14 --- /dev/null +++ b/src/main/java/com/simibubi/create/modules/contraptions/components/contraptions/CancelPlayerFallPacket.java @@ -0,0 +1,34 @@ +package com.simibubi.create.modules.contraptions.components.contraptions; + +import java.util.function.Supplier; + +import com.simibubi.create.foundation.packet.SimplePacketBase; + +import net.minecraft.entity.player.ServerPlayerEntity; +import net.minecraft.network.PacketBuffer; +import net.minecraftforge.fml.network.NetworkEvent.Context; + +public class CancelPlayerFallPacket extends SimplePacketBase { + + public CancelPlayerFallPacket() { + } + + public CancelPlayerFallPacket(PacketBuffer buffer) { + } + + @Override + public void write(PacketBuffer buffer) { + } + + @Override + public void handle(Supplier context) { + context.get().enqueueWork(() -> { + ServerPlayerEntity sender = context.get().getSender(); + sender.fall(sender.fallDistance, 1.0F); + sender.fallDistance = 0; + sender.onGround = true; + }); + context.get().setPacketHandled(true); + } + +} diff --git a/src/main/java/com/simibubi/create/modules/contraptions/components/contraptions/ContraptionCollider.java b/src/main/java/com/simibubi/create/modules/contraptions/components/contraptions/ContraptionCollider.java index 64f287aff..4452f949d 100644 --- a/src/main/java/com/simibubi/create/modules/contraptions/components/contraptions/ContraptionCollider.java +++ b/src/main/java/com/simibubi/create/modules/contraptions/components/contraptions/ContraptionCollider.java @@ -8,6 +8,7 @@ import com.simibubi.create.modules.contraptions.components.actors.BlockBreakingM import net.minecraft.block.BlockState; import net.minecraft.block.material.PushReaction; +import net.minecraft.client.Minecraft; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityType; import net.minecraft.entity.MoverType; @@ -23,10 +24,14 @@ import net.minecraft.util.math.shapes.ISelectionContext; import net.minecraft.util.math.shapes.VoxelShape; import net.minecraft.world.World; import net.minecraft.world.gen.feature.template.Template.BlockInfo; +import net.minecraftforge.api.distmarker.Dist; +import net.minecraftforge.api.distmarker.OnlyIn; +import net.minecraftforge.fml.DistExecutor; public class ContraptionCollider { static Map renderedBBs = new HashMap<>(); + public static boolean wasClientPlayerGrounded; public static void collideEntities(ContraptionEntity contraptionEntity) { if (Contraption.isFrozen()) @@ -71,6 +76,7 @@ public class ContraptionCollider { entity.fall(entity.fallDistance, 1); entity.fallDistance = 0; entity.onGround = true; + DistExecutor.runWhenOn(Dist.CLIENT, () -> () -> checkForClientPlayerCollision(entity)); } if (entity instanceof PlayerEntity && !world.isRemote) @@ -82,6 +88,13 @@ public class ContraptionCollider { } + @OnlyIn(Dist.CLIENT) + private static void checkForClientPlayerCollision(Entity entity) { + if (entity != Minecraft.getInstance().player) + return; + wasClientPlayerGrounded = true; + } + public static void pushEntityOutOfShape(Entity entity, VoxelShape voxelShape, Vec3d positionOffset, Vec3d shapeMotion) { AxisAlignedBB entityBB = entity.getBoundingBox().offset(positionOffset); diff --git a/src/main/java/com/simibubi/create/modules/contraptions/components/contraptions/piston/LinearActuatorTileEntity.java b/src/main/java/com/simibubi/create/modules/contraptions/components/contraptions/piston/LinearActuatorTileEntity.java index 88e5757b5..71713172c 100644 --- a/src/main/java/com/simibubi/create/modules/contraptions/components/contraptions/piston/LinearActuatorTileEntity.java +++ b/src/main/java/com/simibubi/create/modules/contraptions/components/contraptions/piston/LinearActuatorTileEntity.java @@ -2,12 +2,15 @@ package com.simibubi.create.modules.contraptions.components.contraptions.piston; import java.util.List; +import com.simibubi.create.AllPackets; import com.simibubi.create.foundation.behaviour.ValueBoxTransform; import com.simibubi.create.foundation.behaviour.base.TileEntityBehaviour; import com.simibubi.create.foundation.behaviour.scrollvalue.ScrollOptionBehaviour; import com.simibubi.create.foundation.utility.Lang; import com.simibubi.create.foundation.utility.ServerSpeedProvider; import com.simibubi.create.modules.contraptions.base.KineticTileEntity; +import com.simibubi.create.modules.contraptions.components.contraptions.CancelPlayerFallPacket; +import com.simibubi.create.modules.contraptions.components.contraptions.ContraptionCollider; import com.simibubi.create.modules.contraptions.components.contraptions.ContraptionEntity; import com.simibubi.create.modules.contraptions.components.contraptions.IControlContraption; @@ -202,12 +205,20 @@ public abstract class LinearActuatorTileEntity extends KineticTileEntity impleme protected abstract ValueBoxTransform getMovementModeSlot(); - protected abstract void visitNewPosition(); - protected abstract Vec3d toMotionVector(float speed); protected abstract Vec3d toPosition(float offset); + protected void visitNewPosition() { + if (!world.isRemote) + return; + if (!ContraptionCollider.wasClientPlayerGrounded) + return; + // Send falldamage-cancel for the colliding player + ContraptionCollider.wasClientPlayerGrounded = false; + AllPackets.channel.sendToServer(new CancelPlayerFallPacket()); + } + protected void tryDisassemble() { if (removed) { disassemble(); diff --git a/src/main/java/com/simibubi/create/modules/contraptions/components/contraptions/piston/MechanicalPistonTileEntity.java b/src/main/java/com/simibubi/create/modules/contraptions/components/contraptions/piston/MechanicalPistonTileEntity.java index 9918f772d..7dda90f67 100644 --- a/src/main/java/com/simibubi/create/modules/contraptions/components/contraptions/piston/MechanicalPistonTileEntity.java +++ b/src/main/java/com/simibubi/create/modules/contraptions/components/contraptions/piston/MechanicalPistonTileEntity.java @@ -3,7 +3,6 @@ package com.simibubi.create.modules.contraptions.components.contraptions.piston; import com.simibubi.create.AllBlocks; import com.simibubi.create.AllTileEntities; import com.simibubi.create.foundation.behaviour.ValueBoxTransform; -import com.simibubi.create.foundation.utility.Debug; import com.simibubi.create.foundation.utility.ServerSpeedProvider; import com.simibubi.create.modules.contraptions.base.IRotate; import com.simibubi.create.modules.contraptions.components.contraptions.ContraptionCollider; diff --git a/src/main/java/com/simibubi/create/modules/contraptions/components/contraptions/pulley/PulleyTileEntity.java b/src/main/java/com/simibubi/create/modules/contraptions/components/contraptions/pulley/PulleyTileEntity.java index 8edfd3583..327ad7c76 100644 --- a/src/main/java/com/simibubi/create/modules/contraptions/components/contraptions/pulley/PulleyTileEntity.java +++ b/src/main/java/com/simibubi/create/modules/contraptions/components/contraptions/pulley/PulleyTileEntity.java @@ -121,6 +121,7 @@ public class PulleyTileEntity extends LinearActuatorTileEntity { @Override protected void visitNewPosition() { + super.visitNewPosition(); if (world.isRemote) return; if (movedContraption != null) diff --git a/src/main/java/com/simibubi/create/modules/contraptions/processing/BasinBlock.java b/src/main/java/com/simibubi/create/modules/contraptions/processing/BasinBlock.java index 71e2b99ac..27f26b8d7 100644 --- a/src/main/java/com/simibubi/create/modules/contraptions/processing/BasinBlock.java +++ b/src/main/java/com/simibubi/create/modules/contraptions/processing/BasinBlock.java @@ -2,6 +2,7 @@ package com.simibubi.create.modules.contraptions.processing; import com.simibubi.create.AllBlocks; import com.simibubi.create.foundation.block.IWithTileEntity; +import com.simibubi.create.foundation.item.ItemHelper; import com.simibubi.create.foundation.utility.AllShapes; import net.minecraft.block.Block; @@ -40,12 +41,12 @@ public class BasinBlock extends Block implements IWithTileEntity