| 1 | package net.minecraft.world.entity.vehicle.minecart;
|
| 2 |
|
| 3 | import net.minecraft.core.Direction;
|
| 4 | import net.minecraft.core.particles.ParticleTypes;
|
| 5 | import net.minecraft.network.syncher.EntityDataAccessor;
|
| 6 | import net.minecraft.network.syncher.EntityDataSerializers;
|
| 7 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 8 | import net.minecraft.server.level.ServerLevel;
|
| 9 | import net.minecraft.tags.ItemTags;
|
| 10 | import net.minecraft.world.InteractionHand;
|
| 11 | import net.minecraft.world.InteractionResult;
|
| 12 | import net.minecraft.world.entity.EntityType;
|
| 13 | import net.minecraft.world.entity.player.Player;
|
| 14 | import net.minecraft.world.item.Item;
|
| 15 | import net.minecraft.world.item.ItemStack;
|
| 16 | import net.minecraft.world.item.Items;
|
| 17 | import net.minecraft.world.level.Level;
|
| 18 | import net.minecraft.world.level.block.Blocks;
|
| 19 | import net.minecraft.world.level.block.FurnaceBlock;
|
| 20 | import net.minecraft.world.level.block.state.BlockState;
|
| 21 | import net.minecraft.world.level.storage.ValueInput;
|
| 22 | import net.minecraft.world.level.storage.ValueOutput;
|
| 23 | import net.minecraft.world.phys.Vec3;
|
| 24 |
|
| 25 | public class MinecartFurnace extends AbstractMinecart {
|
| 26 | private static final EntityDataAccessor<Boolean> DATA_ID_FUEL = SynchedEntityData.defineId(MinecartFurnace.class, EntityDataSerializers.BOOLEAN);
|
| 27 | private static final int FUEL_TICKS_PER_ITEM = 3600;
|
| 28 | private static final int MAX_FUEL_TICKS = 32000;
|
| 29 | private static final short DEFAULT_FUEL = 0;
|
| 30 | private static final Vec3 DEFAULT_PUSH = Vec3.ZERO;
|
| 31 | private int fuel = 0;
|
| 32 | public Vec3 push = DEFAULT_PUSH;
|
| 33 |
|
| 34 | public MinecartFurnace(final EntityType<? extends MinecartFurnace> type, final Level level) {
|
| 35 | super(type, level);
|
| 36 | }
|
| 37 |
|
| 38 | @Override
|
| 39 | public boolean isFurnace() {
|
| 40 | return true;
|
| 41 | }
|
| 42 |
|
| 43 | @Override
|
| 44 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 45 | super.defineSynchedData(entityData);
|
| 46 | entityData.define(DATA_ID_FUEL, false);
|
| 47 | }
|
| 48 |
|
| 49 | @Override
|
| 50 | public void tick() {
|
| 51 | super.tick();
|
| 52 | if (!this.level().isClientSide()) {
|
| 53 | if (this.fuel > 0) {
|
| 54 | this.fuel--;
|
| 55 | }
|
| 56 |
|
| 57 | if (this.fuel <= 0) {
|
| 58 | this.push = Vec3.ZERO;
|
| 59 | }
|
| 60 |
|
| 61 | this.setHasFuel(this.fuel > 0);
|
| 62 | }
|
| 63 |
|
| 64 | if (this.hasFuel() && this.random.nextInt(4) == 0) {
|
| 65 | this.level().addParticle(ParticleTypes.LARGE_SMOKE, this.getX(), this.getY() + 0.8, this.getZ(), 0.0, 0.0, 0.0);
|
| 66 | }
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | protected double getMaxSpeed(final ServerLevel level) {
|
| 71 | return this.isInWater() ? super.getMaxSpeed(level) * 0.75 : super.getMaxSpeed(level) * 0.5;
|
| 72 | }
|
| 73 |
|
| 74 | @Override
|
| 75 | protected Item getDropItem() {
|
| 76 | return Items.FURNACE_MINECART;
|
| 77 | }
|
| 78 |
|
| 79 | @Override
|
| 80 | public ItemStack getPickResult() {
|
| 81 | return new ItemStack(Items.FURNACE_MINECART);
|
| 82 | }
|
| 83 |
|
| 84 | @Override
|
| 85 | protected Vec3 applyNaturalSlowdown(final Vec3 deltaMovement) {
|
| 86 | Vec3 newDeltaMovement;
|
| 87 | if (this.push.lengthSqr() > 1.0E-7) {
|
| 88 | this.push = this.calculateNewPushAlong(deltaMovement);
|
| 89 | newDeltaMovement = deltaMovement.multiply(0.8, 0.0, 0.8).add(this.push);
|
| 90 | if (this.isInWater()) {
|
| 91 | newDeltaMovement = newDeltaMovement.scale(0.1);
|
| 92 | }
|
| 93 | } else {
|
| 94 | newDeltaMovement = deltaMovement.multiply(0.98, 0.0, 0.98);
|
| 95 | }
|
| 96 |
|
| 97 | return super.applyNaturalSlowdown(newDeltaMovement);
|
| 98 | }
|
| 99 |
|
| 100 | private Vec3 calculateNewPushAlong(final Vec3 deltaMovement) {
|
| 101 | double epsilonPushCheck = 1.0E-4;
|
| 102 | double epsilonMovementCheck = 0.001;
|
| 103 | return this.push.horizontalDistanceSqr() > 1.0E-4 && deltaMovement.horizontalDistanceSqr() > 0.001
|
| 104 | ? this.push.projectedOn(deltaMovement).normalize().scale(this.push.length())
|
| 105 | : this.push;
|
| 106 | }
|
| 107 |
|
| 108 | @Override
|
| 109 | public InteractionResult interact(final Player player, final InteractionHand hand, final Vec3 location) {
|
| 110 | ItemStack itemStack = player.getItemInHand(hand);
|
| 111 | if (this.addFuel(player.position(), itemStack)) {
|
| 112 | itemStack.consume(1, player);
|
| 113 | }
|
| 114 |
|
| 115 | return InteractionResult.SUCCESS;
|
| 116 | }
|
| 117 |
|
| 118 | public boolean addFuel(final Vec3 interactingPos, final ItemStack itemStack) {
|
| 119 | if (itemStack.is(ItemTags.FURNACE_MINECART_FUEL) && this.fuel + 3600 <= 32000) {
|
| 120 | this.fuel += 3600;
|
| 121 | if (this.fuel > 0) {
|
| 122 | this.push = this.position().subtract(interactingPos).horizontal();
|
| 123 | }
|
| 124 |
|
| 125 | return true;
|
| 126 | } else {
|
| 127 | return false;
|
| 128 | }
|
| 129 | }
|
| 130 |
|
| 131 | @Override
|
| 132 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 133 | super.addAdditionalSaveData(output);
|
| 134 | output.putDouble("PushX", this.push.x);
|
| 135 | output.putDouble("PushZ", this.push.z);
|
| 136 | output.putShort("Fuel", (short)this.fuel);
|
| 137 | }
|
| 138 |
|
| 139 | @Override
|
| 140 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 141 | super.readAdditionalSaveData(input);
|
| 142 | double xPush = input.getDoubleOr("PushX", DEFAULT_PUSH.x);
|
| 143 | double zPush = input.getDoubleOr("PushZ", DEFAULT_PUSH.z);
|
| 144 | this.push = new Vec3(xPush, 0.0, zPush);
|
| 145 | this.fuel = input.getShortOr("Fuel", (short)0);
|
| 146 | }
|
| 147 |
|
| 148 | protected boolean hasFuel() {
|
| 149 | return this.entityData.get(DATA_ID_FUEL);
|
| 150 | }
|
| 151 |
|
| 152 | protected void setHasFuel(final boolean fuel) {
|
| 153 | this.entityData.set(DATA_ID_FUEL, fuel);
|
| 154 | }
|
| 155 |
|
| 156 | @Override
|
| 157 | public BlockState getDefaultDisplayBlockState() {
|
| 158 | return Blocks.FURNACE.defaultBlockState().setValue(FurnaceBlock.FACING, Direction.NORTH).setValue(FurnaceBlock.LIT, this.hasFuel());
|
| 159 | }
|
| 160 | }
|
| 161 |
|