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.NetworkLocation;
|
|
|
|
import de.ellpeck.prettypipes.network.PipeNetwork;
|
2020-04-20 13:12:26 +02:00
|
|
|
import de.ellpeck.prettypipes.pipe.PipeBlock;
|
|
|
|
import net.minecraft.block.BlockState;
|
2021-12-02 12:31:04 +01:00
|
|
|
import net.minecraft.core.BlockPos;
|
|
|
|
import net.minecraft.core.Direction;
|
2020-04-20 13:12:26 +02:00
|
|
|
import net.minecraft.entity.Entity;
|
2020-04-20 03:37:23 +02:00
|
|
|
import net.minecraft.entity.EntityType;
|
|
|
|
import net.minecraft.entity.item.ItemFrameEntity;
|
2020-04-20 13:12:26 +02:00
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
|
|
|
import net.minecraft.item.FilledMapItem;
|
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;
|
|
|
|
import net.minecraft.world.entity.EntityType;
|
|
|
|
import net.minecraft.world.entity.decoration.ItemFrame;
|
|
|
|
import net.minecraft.world.item.ItemStack;
|
2020-04-20 13:12:26 +02:00
|
|
|
import net.minecraft.item.Items;
|
2020-04-20 03:37:23 +02:00
|
|
|
import net.minecraft.network.IPacket;
|
2020-04-20 16:07:44 +02:00
|
|
|
import net.minecraft.network.PacketBuffer;
|
2020-04-20 03:37:23 +02:00
|
|
|
import net.minecraft.network.datasync.DataParameter;
|
|
|
|
import net.minecraft.network.datasync.DataSerializers;
|
|
|
|
import net.minecraft.network.datasync.EntityDataManager;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
2020-04-20 19:22:30 +02:00
|
|
|
import net.minecraft.util.math.RayTraceResult;
|
2020-04-20 13:12:26 +02:00
|
|
|
import net.minecraft.world.GameRules;
|
2020-04-20 03:37:23 +02:00
|
|
|
import net.minecraft.world.World;
|
2021-12-02 12:31:04 +01:00
|
|
|
import net.minecraft.world.level.Level;
|
|
|
|
import net.minecraft.world.level.block.state.BlockState;
|
2020-04-20 13:12:26 +02:00
|
|
|
import net.minecraft.world.storage.MapData;
|
2021-12-02 12:31:04 +01:00
|
|
|
import net.minecraftforge.entity.IEntityAdditionalSpawnData;
|
2020-04-20 16:07:44 +02:00
|
|
|
import net.minecraftforge.fml.common.registry.IEntityAdditionalSpawnData;
|
2020-04-20 03:37:23 +02:00
|
|
|
import net.minecraftforge.fml.network.NetworkHooks;
|
|
|
|
|
2020-04-20 13:12:26 +02:00
|
|
|
import javax.annotation.Nullable;
|
2020-04-20 03:37:23 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
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();
|
|
|
|
this.entityData.define(AMOUNT, -1);
|
2020-04-20 03:37:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void tick() {
|
|
|
|
super.tick();
|
2021-12-02 12:31:04 +01: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;
|
2021-12-02 12:31:04 +01:00
|
|
|
PipeNetwork network = PipeNetwork.get(this.level);
|
|
|
|
BlockPos attached = getAttachedPipe(this.level, this.pos, this.direction);
|
2020-04-20 13:12:26 +02:00
|
|
|
if (attached != null) {
|
|
|
|
BlockPos node = network.getNodeFromPipe(attached);
|
|
|
|
if (node != null) {
|
2021-12-02 12:31:04 +01:00
|
|
|
ItemStack stack = this.getItem();
|
2020-04-20 13:12:26 +02:00
|
|
|
if (!stack.isEmpty()) {
|
|
|
|
List<NetworkLocation> items = network.getOrderedNetworkItems(node);
|
2021-12-02 12:31:04 +01:00
|
|
|
int amount = items.stream().mapToInt(i -> i.getItemAmount(this.level, stack)).sum();
|
|
|
|
this.entityData.set(AMOUNT, amount);
|
2020-04-20 13:12:26 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-02 12:31:04 +01:00
|
|
|
this.entityData.set(AMOUNT, -1);
|
2020-04-20 13:12:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onValidSurface() {
|
|
|
|
return super.onValidSurface() && canPlace(this.world, this.hangingPosition, this.facingDirection);
|
|
|
|
}
|
|
|
|
|
2021-12-02 12:31:04 +01:00
|
|
|
private static BlockPos getAttachedPipe(Level world, BlockPos pos, Direction direction) {
|
2020-04-20 13:12:26 +02:00
|
|
|
for (int i = 1; i <= 2; i++) {
|
2021-12-02 12:31:04 +01:00
|
|
|
BlockPos offset = pos.relative(direction.getOpposite(), i);
|
2020-04-20 13:12:26 +02:00
|
|
|
BlockState state = world.getBlockState(offset);
|
|
|
|
if (state.getBlock() instanceof PipeBlock)
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean canPlace(World world, BlockPos pos, Direction direction) {
|
|
|
|
return getAttachedPipe(world, pos, direction) != null;
|
2020-04-20 03:37:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getAmount() {
|
|
|
|
return this.dataManager.get(AMOUNT);
|
|
|
|
}
|
|
|
|
|
2020-04-20 13:12:26 +02:00
|
|
|
@Override
|
|
|
|
public boolean attackEntityFrom(DamageSource source, float amount) {
|
|
|
|
if (this.isInvulnerableTo(source)) {
|
|
|
|
return false;
|
|
|
|
} else if (!source.isExplosion() && !this.getDisplayedItem().isEmpty()) {
|
|
|
|
if (!this.world.isRemote) {
|
|
|
|
this.dropItemOrSelf(source.getTrueSource(), false);
|
|
|
|
this.playSound(SoundEvents.ENTITY_ITEM_FRAME_REMOVE_ITEM, 1.0F, 1.0F);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return super.attackEntityFrom(source, amount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBroken(@Nullable Entity brokenEntity) {
|
|
|
|
this.playSound(SoundEvents.ENTITY_ITEM_FRAME_BREAK, 1.0F, 1.0F);
|
|
|
|
this.dropItemOrSelf(brokenEntity, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void dropItemOrSelf(@Nullable Entity entityIn, boolean b) {
|
|
|
|
if (!this.world.getGameRules().getBoolean(GameRules.DO_ENTITY_DROPS)) {
|
|
|
|
if (entityIn == null)
|
2020-09-22 19:14:07 +02:00
|
|
|
this.getDisplayedItem().setAttachedEntity(null);
|
2020-04-20 13:12:26 +02:00
|
|
|
} else {
|
|
|
|
ItemStack itemstack = this.getDisplayedItem();
|
|
|
|
this.setDisplayedItem(ItemStack.EMPTY);
|
|
|
|
if (entityIn instanceof PlayerEntity) {
|
|
|
|
PlayerEntity playerentity = (PlayerEntity) entityIn;
|
|
|
|
if (playerentity.abilities.isCreativeMode) {
|
2020-09-22 19:14:07 +02:00
|
|
|
itemstack.setAttachedEntity(null);
|
2020-04-20 13:12:26 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (b)
|
|
|
|
this.entityDropItem(Registry.pipeFrameItem);
|
|
|
|
|
|
|
|
if (!itemstack.isEmpty()) {
|
|
|
|
itemstack = itemstack.copy();
|
2020-09-22 19:14:07 +02:00
|
|
|
itemstack.setAttachedEntity(null);
|
2020-04-20 13:12:26 +02:00
|
|
|
this.entityDropItem(itemstack);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-09-22 19:14:07 +02:00
|
|
|
public ActionResultType processInitialInteract(PlayerEntity player, Hand hand) {
|
2020-04-20 13:12:26 +02:00
|
|
|
if (this.getDisplayedItem().isEmpty())
|
|
|
|
return super.processInitialInteract(player, hand);
|
2020-09-22 19:14:07 +02:00
|
|
|
return ActionResultType.FAIL;
|
2020-04-20 13:12:26 +02:00
|
|
|
}
|
|
|
|
|
2020-04-20 19:22:30 +02:00
|
|
|
@Override
|
|
|
|
public ItemStack getPickedResult(RayTraceResult target) {
|
|
|
|
return new ItemStack(Registry.pipeFrameItem);
|
|
|
|
}
|
|
|
|
|
2020-04-20 03:37:23 +02:00
|
|
|
@Override
|
|
|
|
public IPacket<?> createSpawnPacket() {
|
|
|
|
return NetworkHooks.getEntitySpawningPacket(this);
|
|
|
|
}
|
2020-04-20 16:07:44 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeSpawnData(PacketBuffer buffer) {
|
|
|
|
buffer.writeBlockPos(this.hangingPosition);
|
|
|
|
buffer.writeInt(this.facingDirection.getIndex());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void readSpawnData(PacketBuffer additionalData) {
|
|
|
|
this.hangingPosition = additionalData.readBlockPos();
|
|
|
|
this.updateFacingWithBoundingBox(Direction.values()[additionalData.readInt()]);
|
|
|
|
}
|
2020-04-20 03:37:23 +02:00
|
|
|
}
|