| 1 | package net.minecraft.world.entity.vehicle.minecart;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.server.level.ServerLevel;
|
| 5 | import net.minecraft.world.entity.EntitySelector;
|
| 6 | import net.minecraft.world.entity.EntityType;
|
| 7 | import net.minecraft.world.entity.item.ItemEntity;
|
| 8 | import net.minecraft.world.entity.player.Inventory;
|
| 9 | import net.minecraft.world.inventory.AbstractContainerMenu;
|
| 10 | import net.minecraft.world.inventory.HopperMenu;
|
| 11 | import net.minecraft.world.item.Item;
|
| 12 | import net.minecraft.world.item.ItemStack;
|
| 13 | import net.minecraft.world.item.Items;
|
| 14 | import net.minecraft.world.level.Level;
|
| 15 | import net.minecraft.world.level.block.Blocks;
|
| 16 | import net.minecraft.world.level.block.entity.Hopper;
|
| 17 | import net.minecraft.world.level.block.entity.HopperBlockEntity;
|
| 18 | import net.minecraft.world.level.block.state.BlockState;
|
| 19 | import net.minecraft.world.level.block.state.properties.RailShape;
|
| 20 | import net.minecraft.world.level.storage.ValueInput;
|
| 21 | import net.minecraft.world.level.storage.ValueOutput;
|
| 22 |
|
| 23 | public class MinecartHopper extends AbstractMinecartContainer implements Hopper {
|
| 24 | private static final boolean DEFAULT_ENABLED = true;
|
| 25 | private boolean enabled = true;
|
| 26 | private boolean consumedItemThisFrame = false;
|
| 27 |
|
| 28 | public MinecartHopper(final EntityType<? extends MinecartHopper> type, final Level level) {
|
| 29 | super(type, level);
|
| 30 | }
|
| 31 |
|
| 32 | @Override
|
| 33 | public BlockState getDefaultDisplayBlockState() {
|
| 34 | return Blocks.HOPPER.defaultBlockState();
|
| 35 | }
|
| 36 |
|
| 37 | @Override
|
| 38 | public int getDefaultDisplayOffset() {
|
| 39 | return 1;
|
| 40 | }
|
| 41 |
|
| 42 | @Override
|
| 43 | public int getContainerSize() {
|
| 44 | return 5;
|
| 45 | }
|
| 46 |
|
| 47 | @Override
|
| 48 | public void activateMinecart(final ServerLevel level, final int xt, final int yt, final int zt, final boolean state) {
|
| 49 | boolean newEnabled = !state;
|
| 50 | if (newEnabled != this.isEnabled()) {
|
| 51 | this.setEnabled(newEnabled);
|
| 52 | }
|
| 53 | }
|
| 54 |
|
| 55 | public boolean isEnabled() {
|
| 56 | return this.enabled;
|
| 57 | }
|
| 58 |
|
| 59 | public void setEnabled(final boolean enabled) {
|
| 60 | this.enabled = enabled;
|
| 61 | }
|
| 62 |
|
| 63 | @Override
|
| 64 | public double getLevelX() {
|
| 65 | return this.getX();
|
| 66 | }
|
| 67 |
|
| 68 | @Override
|
| 69 | public double getLevelY() {
|
| 70 | return this.getY() + 0.5;
|
| 71 | }
|
| 72 |
|
| 73 | @Override
|
| 74 | public double getLevelZ() {
|
| 75 | return this.getZ();
|
| 76 | }
|
| 77 |
|
| 78 | @Override
|
| 79 | public boolean isGridAligned() {
|
| 80 | return false;
|
| 81 | }
|
| 82 |
|
| 83 | @Override
|
| 84 | public void tick() {
|
| 85 | this.consumedItemThisFrame = false;
|
| 86 | super.tick();
|
| 87 | this.tryConsumeItems();
|
| 88 | }
|
| 89 |
|
| 90 | @Override
|
| 91 | protected double makeStepAlongTrack(final BlockPos pos, final RailShape shape, final double movementLeft) {
|
| 92 | double left = super.makeStepAlongTrack(pos, shape, movementLeft);
|
| 93 | this.tryConsumeItems();
|
| 94 | return left;
|
| 95 | }
|
| 96 |
|
| 97 | private void tryConsumeItems() {
|
| 98 | if (!this.level().isClientSide() && this.isAlive() && this.isEnabled() && !this.consumedItemThisFrame && this.suckInItems()) {
|
| 99 | this.consumedItemThisFrame = true;
|
| 100 | this.setChanged();
|
| 101 | }
|
| 102 | }
|
| 103 |
|
| 104 | public boolean suckInItems() {
|
| 105 | if (HopperBlockEntity.suckInItems(this.level(), this)) {
|
| 106 | return true;
|
| 107 | }
|
| 108 |
|
| 109 | for (ItemEntity entity : this.level().getEntitiesOfClass(ItemEntity.class, this.getBoundingBox().inflate(0.25, 0.0, 0.25), EntitySelector.ENTITY_STILL_ALIVE)) {
|
| 110 | if (HopperBlockEntity.addItem(this, entity)) {
|
| 111 | return true;
|
| 112 | }
|
| 113 | }
|
| 114 |
|
| 115 | return false;
|
| 116 | }
|
| 117 |
|
| 118 | @Override
|
| 119 | protected Item getDropItem() {
|
| 120 | return Items.HOPPER_MINECART;
|
| 121 | }
|
| 122 |
|
| 123 | @Override
|
| 124 | public ItemStack getPickResult() {
|
| 125 | return new ItemStack(Items.HOPPER_MINECART);
|
| 126 | }
|
| 127 |
|
| 128 | @Override
|
| 129 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 130 | super.addAdditionalSaveData(output);
|
| 131 | output.putBoolean("Enabled", this.enabled);
|
| 132 | }
|
| 133 |
|
| 134 | @Override
|
| 135 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 136 | super.readAdditionalSaveData(input);
|
| 137 | this.enabled = input.getBooleanOr("Enabled", true);
|
| 138 | }
|
| 139 |
|
| 140 | @Override
|
| 141 | public AbstractContainerMenu createMenu(final int containerId, final Inventory inventory) {
|
| 142 | return new HopperMenu(containerId, inventory, this);
|
| 143 | }
|
| 144 | }
|
| 145 |
|