PrettyPipes/src/main/java/de/ellpeck/prettypipes/entities/PipeFrameEntity.java

168 lines
5.8 KiB
Java
Raw Normal View History

2020-04-20 03:37:23 +02:00
package de.ellpeck.prettypipes.entities;
2020-04-20 13:12:26 +02:00
import de.ellpeck.prettypipes.Registry;
2020-04-20 03:37:23 +02:00
import de.ellpeck.prettypipes.network.PipeNetwork;
2020-04-20 13:12:26 +02:00
import de.ellpeck.prettypipes.pipe.PipeBlock;
2021-12-02 12:31:04 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
2021-12-02 15:25:46 +01:00
import net.minecraft.network.FriendlyByteBuf;
2021-12-02 12:31:04 +01:00
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.EntityDataSerializers;
import net.minecraft.network.syncher.SynchedEntityData;
2021-12-02 14:44:26 +01:00
import net.minecraft.sounds.SoundEvents;
2023-07-07 19:54:52 +02:00
import net.minecraft.tags.DamageTypeTags;
2021-12-02 14:44:26 +01:00
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.Entity;
2021-12-02 12:31:04 +01:00
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.decoration.ItemFrame;
2021-12-02 14:44:26 +01:00
import net.minecraft.world.entity.player.Player;
2021-12-02 12:31:04 +01:00
import net.minecraft.world.item.ItemStack;
2021-12-02 14:44:26 +01:00
import net.minecraft.world.level.GameRules;
2021-12-02 12:31:04 +01:00
import net.minecraft.world.level.Level;
2021-12-02 14:44:26 +01:00
import net.minecraft.world.phys.HitResult;
2021-12-02 12:31:04 +01:00
import net.minecraftforge.entity.IEntityAdditionalSpawnData;
2020-04-20 03:37:23 +02:00
2020-04-20 13:12:26 +02:00
import javax.annotation.Nullable;
2020-04-20 03:37:23 +02:00
2021-12-02 12:31:04 +01:00
public class PipeFrameEntity extends ItemFrame implements IEntityAdditionalSpawnData {
2020-04-20 03:37:23 +02:00
2021-12-02 12:31:04 +01:00
private static final EntityDataAccessor<Integer> AMOUNT = SynchedEntityData.defineId(PipeFrameEntity.class, EntityDataSerializers.INT);
2020-04-20 03:37:23 +02:00
2021-12-02 12:31:04 +01:00
public PipeFrameEntity(EntityType<PipeFrameEntity> type, Level world) {
2020-04-20 03:37:23 +02:00
super(type, world);
}
2021-12-02 12:31:04 +01:00
public PipeFrameEntity(EntityType<PipeFrameEntity> type, Level world, BlockPos pos, Direction dir) {
2020-04-20 03:37:23 +02:00
this(type, world);
2021-12-02 12:31:04 +01:00
this.pos = pos;
this.setDirection(dir);
2020-04-20 03:37:23 +02:00
}
@Override
2021-12-02 12:31:04 +01:00
protected void defineSynchedData() {
super.defineSynchedData();
2022-06-27 13:57:06 +02:00
this.entityData.define(PipeFrameEntity.AMOUNT, -1);
2020-04-20 03:37:23 +02:00
}
@Override
public void tick() {
super.tick();
2023-07-07 19:54:52 +02:00
if (this.level().isClientSide)
2020-04-20 03:37:23 +02:00
return;
2021-12-02 12:31:04 +01:00
if (this.tickCount % 40 != 0)
2020-04-20 03:37:23 +02:00
return;
2023-07-07 19:54:52 +02:00
var network = PipeNetwork.get(this.level());
var attached = PipeFrameEntity.getAttachedPipe(this.level(), this.pos, this.direction);
2020-04-20 13:12:26 +02:00
if (attached != null) {
2021-12-02 15:25:46 +01:00
var node = network.getNodeFromPipe(attached);
2020-04-20 13:12:26 +02:00
if (node != null) {
2021-12-02 15:25:46 +01:00
var stack = this.getItem();
2020-04-20 13:12:26 +02:00
if (!stack.isEmpty()) {
2021-12-02 15:25:46 +01:00
var items = network.getOrderedNetworkItems(node);
2023-07-07 19:54:52 +02:00
var amount = items.stream().mapToInt(i -> i.getItemAmount(this.level(), stack)).sum();
2022-06-27 13:57:06 +02:00
this.entityData.set(PipeFrameEntity.AMOUNT, amount);
2020-04-20 13:12:26 +02:00
return;
}
}
}
2022-06-27 13:57:06 +02:00
this.entityData.set(PipeFrameEntity.AMOUNT, -1);
2020-04-20 13:12:26 +02:00
}
@Override
2021-12-02 14:44:26 +01:00
public boolean survives() {
2023-07-07 19:54:52 +02:00
return super.survives() && PipeFrameEntity.canPlace(this.level(), this.pos, this.direction);
2020-04-20 13:12:26 +02:00
}
2021-12-02 12:31:04 +01:00
private static BlockPos getAttachedPipe(Level world, BlockPos pos, Direction direction) {
2021-12-02 15:25:46 +01:00
for (var i = 1; i <= 2; i++) {
var offset = pos.relative(direction.getOpposite(), i);
var state = world.getBlockState(offset);
2020-04-20 13:12:26 +02:00
if (state.getBlock() instanceof PipeBlock)
return offset;
}
return null;
}
2021-12-02 14:44:26 +01:00
public static boolean canPlace(Level world, BlockPos pos, Direction direction) {
2022-06-27 13:57:06 +02:00
return PipeFrameEntity.getAttachedPipe(world, pos, direction) != null;
2020-04-20 03:37:23 +02:00
}
public int getAmount() {
2022-06-27 13:57:06 +02:00
return this.entityData.get(PipeFrameEntity.AMOUNT);
2020-04-20 03:37:23 +02:00
}
2020-04-20 13:12:26 +02:00
@Override
2021-12-02 14:44:26 +01:00
public boolean hurt(DamageSource source, float amount) {
2020-04-20 13:12:26 +02:00
if (this.isInvulnerableTo(source)) {
return false;
2023-07-07 19:54:52 +02:00
} else if (!source.is(DamageTypeTags.IS_EXPLOSION) && !this.getItem().isEmpty()) {
if (!this.level().isClientSide) {
2021-12-02 14:44:26 +01:00
this.dropItemOrSelf(source.getDirectEntity(), false);
this.playSound(SoundEvents.ITEM_FRAME_REMOVE_ITEM, 1.0F, 1.0F);
2020-04-20 13:12:26 +02:00
}
return true;
} else {
2021-12-02 14:44:26 +01:00
return super.hurt(source, amount);
2020-04-20 13:12:26 +02:00
}
}
@Override
2021-12-02 14:44:26 +01:00
public void dropItem(@Nullable Entity brokenEntity) {
this.playSound(SoundEvents.ITEM_FRAME_BREAK, 1.0F, 1.0F);
2020-04-20 13:12:26 +02:00
this.dropItemOrSelf(brokenEntity, true);
}
private void dropItemOrSelf(@Nullable Entity entityIn, boolean b) {
2023-07-07 19:54:52 +02:00
if (!this.level().getGameRules().getBoolean(GameRules.RULE_DOENTITYDROPS)) {
2020-04-20 13:12:26 +02:00
if (entityIn == null)
2021-12-02 14:44:26 +01:00
this.getItem().setEntityRepresentation(null);
2020-04-20 13:12:26 +02:00
} else {
2021-12-02 15:25:46 +01:00
var itemstack = this.getItem();
2021-12-02 14:44:26 +01:00
this.setItem(ItemStack.EMPTY);
if (entityIn instanceof Player playerentity) {
if (playerentity.isCreative()) {
itemstack.setEntityRepresentation(null);
2020-04-20 13:12:26 +02:00
return;
}
}
if (b)
2021-12-02 14:44:26 +01:00
this.spawnAtLocation(Registry.pipeFrameItem);
2020-04-20 13:12:26 +02:00
if (!itemstack.isEmpty()) {
itemstack = itemstack.copy();
2021-12-02 14:44:26 +01:00
itemstack.setEntityRepresentation(null);
this.spawnAtLocation(itemstack);
2020-04-20 13:12:26 +02:00
}
}
}
@Override
2021-12-02 14:44:26 +01:00
public InteractionResult interact(Player player, InteractionHand hand) {
if (this.getItem().isEmpty())
return super.interact(player, hand);
return InteractionResult.FAIL;
2020-04-20 13:12:26 +02:00
}
@Override
2021-12-02 14:44:26 +01:00
public ItemStack getPickedResult(HitResult target) {
return new ItemStack(Registry.pipeFrameItem);
}
2020-04-20 03:37:23 +02:00
@Override
2021-12-02 15:25:46 +01:00
public void writeSpawnData(FriendlyByteBuf buffer) {
buffer.writeBlockPos(this.pos);
buffer.writeInt(this.direction.ordinal());
2020-04-20 16:07:44 +02:00
}
@Override
2021-12-02 15:25:46 +01:00
public void readSpawnData(FriendlyByteBuf additionalData) {
this.pos = additionalData.readBlockPos();
this.direction = Direction.values()[additionalData.readInt()];
2020-04-20 16:07:44 +02:00
}
2020-04-20 03:37:23 +02:00
}