From d3e7b23d6e5714a658dbb0046c8297bb6a6d4b3d Mon Sep 17 00:00:00 2001 From: Zelophed Date: Mon, 20 Jul 2020 01:19:04 +0200 Subject: [PATCH] Seats, part I --- .../com/simibubi/create/AllEntityTypes.java | 4 + .../components/actors/SeatBlock.java | 33 ++++++- .../components/actors/SeatEntity.java | 97 +++++++++++++++++++ 3 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/simibubi/create/content/contraptions/components/actors/SeatEntity.java diff --git a/src/main/java/com/simibubi/create/AllEntityTypes.java b/src/main/java/com/simibubi/create/AllEntityTypes.java index 80e5bbf50..0e2e5b593 100644 --- a/src/main/java/com/simibubi/create/AllEntityTypes.java +++ b/src/main/java/com/simibubi/create/AllEntityTypes.java @@ -1,5 +1,6 @@ package com.simibubi.create; +import com.simibubi.create.content.contraptions.components.actors.SeatEntity; import com.simibubi.create.content.contraptions.components.structureMovement.ContraptionEntity; import com.simibubi.create.content.contraptions.components.structureMovement.ContraptionEntityRenderer; import com.simibubi.create.content.contraptions.components.structureMovement.glue.SuperGlueEntity; @@ -24,6 +25,8 @@ public class AllEntityTypes { register("stationary_contraption", ContraptionEntity::new, EntityClassification.MISC, 20, 40, false, ContraptionEntity::build); public static final RegistryEntry> SUPER_GLUE = register("super_glue", SuperGlueEntity::new, EntityClassification.MISC, 10, Integer.MAX_VALUE, false, SuperGlueEntity::build); + public static final RegistryEntry> SEAT = + register("seat", SeatEntity::new, EntityClassification.MISC, 0, Integer.MAX_VALUE, false, SeatEntity::build); private static RegistryEntry> register(String name, IFactory factory, EntityClassification group, int range, int updateFrequency, boolean sendVelocity, @@ -45,5 +48,6 @@ public class AllEntityTypes { RenderingRegistry.registerEntityRenderingHandler(STATIONARY_CONTRAPTION.get(), ContraptionEntityRenderer::new); RenderingRegistry.registerEntityRenderingHandler(CONTRAPTION.get(), ContraptionEntityRenderer::new); RenderingRegistry.registerEntityRenderingHandler(SUPER_GLUE.get(), SuperGlueRenderer::new); + RenderingRegistry.registerEntityRenderingHandler(SEAT.get(), SeatEntity.Render::new); } } diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/actors/SeatBlock.java b/src/main/java/com/simibubi/create/content/contraptions/components/actors/SeatBlock.java index 1f32e1d24..93c502916 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/actors/SeatBlock.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/actors/SeatBlock.java @@ -4,13 +4,20 @@ import com.simibubi.create.AllShapes; import net.minecraft.block.Block; import net.minecraft.block.BlockState; +import net.minecraft.block.Blocks; +import net.minecraft.entity.Entity; +import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemGroup; import net.minecraft.item.ItemStack; +import net.minecraft.util.ActionResultType; +import net.minecraft.util.Hand; import net.minecraft.util.NonNullList; import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.BlockRayTraceResult; import net.minecraft.util.math.shapes.ISelectionContext; import net.minecraft.util.math.shapes.VoxelShape; import net.minecraft.world.IBlockReader; +import net.minecraft.world.World; public class SeatBlock extends Block { @@ -27,11 +34,35 @@ public class SeatBlock extends Block { return; super.fillItemGroup(group, p_149666_2_); } - + + @Override + public void onFallenUpon(World p_180658_1_, BlockPos p_180658_2_, Entity p_180658_3_, float p_180658_4_) { + super.onFallenUpon(p_180658_1_, p_180658_2_, p_180658_3_, p_180658_4_ * 0.5F); + } + + @Override + public void onLanded(IBlockReader p_176216_1_, Entity p_176216_2_) { + Blocks.PINK_BED.onLanded(p_176216_1_, p_176216_2_); + } + @Override public VoxelShape getShape(BlockState p_220053_1_, IBlockReader p_220053_2_, BlockPos p_220053_3_, ISelectionContext p_220053_4_) { return AllShapes.SEAT; } + @Override + public ActionResultType onUse(BlockState p_225533_1_, World world, BlockPos pos, PlayerEntity player, Hand p_225533_5_, BlockRayTraceResult p_225533_6_) { + if (SeatEntity.TAKEN.containsKey(pos)) + return ActionResultType.FAIL; + + if (world.isRemote) + return ActionResultType.SUCCESS; + + SeatEntity seat = new SeatEntity(world, pos); + world.addEntity(seat); + player.startRiding(seat); + + return ActionResultType.SUCCESS; + } } diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/actors/SeatEntity.java b/src/main/java/com/simibubi/create/content/contraptions/components/actors/SeatEntity.java new file mode 100644 index 000000000..9e5cab53a --- /dev/null +++ b/src/main/java/com/simibubi/create/content/contraptions/components/actors/SeatEntity.java @@ -0,0 +1,97 @@ +package com.simibubi.create.content.contraptions.components.actors; + +import com.simibubi.create.AllEntityTypes; +import net.minecraft.client.renderer.culling.ClippingHelperImpl; +import net.minecraft.client.renderer.entity.EntityRenderer; +import net.minecraft.client.renderer.entity.EntityRendererManager; +import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityType; +import net.minecraft.nbt.CompoundNBT; +import net.minecraft.network.IPacket; +import net.minecraft.util.ResourceLocation; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Vec3d; +import net.minecraft.world.World; +import net.minecraftforge.fml.network.NetworkHooks; + +import java.util.HashMap; +import java.util.Map; + +public class SeatEntity extends Entity { + + public static final Map TAKEN = new HashMap<>(); + + public SeatEntity(EntityType p_i48580_1_, World p_i48580_2_) { + super(p_i48580_1_, p_i48580_2_); + } + + public SeatEntity(World world, BlockPos pos) { + this(AllEntityTypes.SEAT.get(), world); + this.setPos(pos.getX() + 0.5, pos.getY() + 0.30, pos.getZ() + 0.5); + noClip = true; + TAKEN.put(pos, this); + + } + + public static EntityType.Builder build(EntityType.Builder builder) { + @SuppressWarnings("unchecked") + EntityType.Builder entityBuilder = (EntityType.Builder) builder; + return entityBuilder.size(0, 0); + } + + @Override + public void tick() { + if (world.isRemote) + return; + + BlockPos blockPos = new BlockPos(getX(), getY(), getZ()); + if (isBeingRidden() && world.getBlockState(blockPos).getBlock() instanceof SeatBlock) + return; + + TAKEN.remove(blockPos); + this.remove(); + } + + @Override + protected boolean canBeRidden(Entity p_184228_1_) { + return true; + } + + @Override + protected void removePassenger(Entity entity) { + super.removePassenger(entity); + Vec3d pos = entity.getPositionVec(); + entity.setPosition(pos.x, pos.y + 0.7, pos.z); + } + + @Override + protected void registerData() {} + + @Override + protected void readAdditional(CompoundNBT p_70037_1_) {} + + @Override + protected void writeAdditional(CompoundNBT p_213281_1_) {} + + @Override + public IPacket createSpawnPacket() { + return NetworkHooks.getEntitySpawningPacket(this); + } + + public static class Render extends EntityRenderer { + + public Render(EntityRendererManager p_i46179_1_) { + super(p_i46179_1_); + } + + @Override + public boolean shouldRender(SeatEntity p_225626_1_, ClippingHelperImpl p_225626_2_, double p_225626_3_, double p_225626_5_, double p_225626_7_) { + return false; + } + + @Override + public ResourceLocation getEntityTexture(SeatEntity p_110775_1_) { + return null; + } + } +}