2020-07-13 20:37:21 +00:00
|
|
|
package com.thebrokenrail.energonrelics.energy.core;
|
|
|
|
|
2020-07-17 16:14:27 +00:00
|
|
|
import com.thebrokenrail.energonrelics.config.HardcodedConfig;
|
2020-07-13 20:37:21 +00:00
|
|
|
import com.thebrokenrail.energonrelics.EnergonRelics;
|
|
|
|
import com.thebrokenrail.energonrelics.component.NetworkComponent;
|
|
|
|
import com.thebrokenrail.energonrelics.energy.core.util.Action;
|
2020-07-26 22:31:05 +00:00
|
|
|
import com.thebrokenrail.energonrelics.energy.core.util.EnergyTickable;
|
2020-07-24 17:25:50 +00:00
|
|
|
import com.thebrokenrail.energonrelics.energy.core.util.EnergyTicker;
|
2020-07-21 02:39:30 +00:00
|
|
|
import com.thebrokenrail.energonrelics.util.BlockPosWithDimension;
|
2020-07-13 20:37:21 +00:00
|
|
|
import net.fabricmc.fabric.api.block.entity.BlockEntityClientSerializable;
|
|
|
|
import net.minecraft.block.BlockState;
|
|
|
|
import net.minecraft.block.Blocks;
|
|
|
|
import net.minecraft.block.entity.BlockEntity;
|
|
|
|
import net.minecraft.block.entity.BlockEntityType;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.CompoundTag;
|
|
|
|
import net.minecraft.server.world.ServerWorld;
|
|
|
|
import net.minecraft.util.Tickable;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
|
|
|
import net.minecraft.util.math.Vec3d;
|
2020-07-21 02:39:30 +00:00
|
|
|
import net.minecraft.util.registry.RegistryKey;
|
2020-07-19 19:12:39 +00:00
|
|
|
import net.minecraft.world.World;
|
2020-07-13 20:37:21 +00:00
|
|
|
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Objects;
|
|
|
|
|
2020-07-26 22:31:05 +00:00
|
|
|
public class EnergyProviderBlockEntity extends BlockEntity implements BlockEntityClientSerializable, Tickable, EnergyTickable {
|
2020-07-13 20:37:21 +00:00
|
|
|
public EnergyProviderBlockEntity(BlockEntityType<?> type) {
|
|
|
|
super(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
public final void addPropagatedAction(Action.PropagatedAction action) {
|
|
|
|
if (isEnergyProvider()) {
|
2020-07-24 18:02:42 +00:00
|
|
|
handlePropagatedAction(action);
|
2020-07-13 20:37:21 +00:00
|
|
|
} else {
|
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public final boolean isNetwork(int network) {
|
|
|
|
return isEnergyProvider() && EnergonRelics.NETWORK_CHIP_ITEM.getID(stack) == network;
|
|
|
|
}
|
|
|
|
|
|
|
|
public final boolean isWithinDistance(Vec3d pos) {
|
2020-07-17 16:14:27 +00:00
|
|
|
return getPos().isWithinDistance(pos, HardcodedConfig.POWER_RANGE);
|
2020-07-13 20:37:21 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 02:39:30 +00:00
|
|
|
public RegistryKey<World> getRegistryKey() {
|
|
|
|
return Objects.requireNonNull(getWorld()).getRegistryKey();
|
|
|
|
}
|
|
|
|
|
2020-07-13 20:37:21 +00:00
|
|
|
private ItemStack stack = ItemStack.EMPTY;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public CompoundTag toTag(CompoundTag tag) {
|
|
|
|
super.toTag(tag);
|
|
|
|
if (isEnergyProvider()) {
|
|
|
|
tag.put("NetworkChip", stack.toTag(new CompoundTag()));
|
|
|
|
}
|
|
|
|
return tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void fromTag(BlockState state, CompoundTag tag) {
|
|
|
|
super.fromTag(state, tag);
|
|
|
|
if (isEnergyProvider()) {
|
|
|
|
stack = ItemStack.fromTag(tag.getCompound("NetworkChip"));
|
|
|
|
} else {
|
|
|
|
stack = ItemStack.EMPTY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isEnergyProvider() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void handlePropagatedAction(Action.PropagatedAction action) {
|
2020-07-22 23:51:42 +00:00
|
|
|
if (!isEnergyProvider()) {
|
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
}
|
2020-07-13 20:37:21 +00:00
|
|
|
}
|
|
|
|
|
2020-07-26 22:31:05 +00:00
|
|
|
@Override
|
2020-07-24 17:25:50 +00:00
|
|
|
public void logicTick() {
|
|
|
|
}
|
|
|
|
|
2020-07-26 22:31:05 +00:00
|
|
|
@Override
|
|
|
|
public List<EnergyTickable> startTick() {
|
2020-07-27 18:06:46 +00:00
|
|
|
if (isEnergyProvider() && stack.getItem() == EnergonRelics.NETWORK_CHIP_ITEM) {
|
2020-07-13 20:37:21 +00:00
|
|
|
NetworkComponent component = NetworkComponent.getInstance((ServerWorld) Objects.requireNonNull(getWorld()));
|
2020-07-27 18:06:46 +00:00
|
|
|
List<BlockPosWithDimension> sources = component.getSourcesPos(EnergonRelics.NETWORK_CHIP_ITEM.getID(stack));
|
2020-07-21 02:39:30 +00:00
|
|
|
if (!sources.contains(new BlockPosWithDimension(getPos(), getWorld().getRegistryKey()))) {
|
|
|
|
takeStack(getWorld());
|
2020-07-13 20:37:21 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-24 17:25:50 +00:00
|
|
|
return Collections.emptyList();
|
2020-07-13 20:37:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public final void tick() {
|
|
|
|
if (hasWorld() && !Objects.requireNonNull(getWorld()).isClient()) {
|
2020-07-24 17:25:50 +00:00
|
|
|
EnergyTicker.schedule(this);
|
2020-07-13 20:37:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-19 19:12:39 +00:00
|
|
|
private static void setEnergyProviderSource(World world, BlockPos pos, ItemStack stack, boolean remove) {
|
|
|
|
ServerWorld serverWorld = (ServerWorld) world;
|
|
|
|
NetworkComponent component = NetworkComponent.getInstance(serverWorld);
|
2020-07-21 02:39:30 +00:00
|
|
|
BlockPosWithDimension newPos = new BlockPosWithDimension(pos, world.getRegistryKey());
|
2020-07-19 19:12:39 +00:00
|
|
|
if (remove) {
|
2020-07-21 02:39:30 +00:00
|
|
|
component.removeSource(EnergonRelics.NETWORK_CHIP_ITEM.getID(stack), newPos);
|
2020-07-19 19:12:39 +00:00
|
|
|
} else {
|
2020-07-21 02:39:30 +00:00
|
|
|
component.addSource(EnergonRelics.NETWORK_CHIP_ITEM.getID(stack), newPos);
|
2020-07-19 19:12:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-21 02:39:30 +00:00
|
|
|
public ItemStack takeStack(World world) {
|
2020-07-13 20:37:21 +00:00
|
|
|
ItemStack newStack = stack.copy();
|
2020-07-21 02:39:30 +00:00
|
|
|
setEnergyProviderSource(world, getPos(), newStack, true);
|
2020-07-13 20:37:21 +00:00
|
|
|
stack = ItemStack.EMPTY;
|
|
|
|
markDirty();
|
|
|
|
return newStack;
|
|
|
|
}
|
|
|
|
|
2020-07-21 02:39:30 +00:00
|
|
|
public void placeStack(ItemStack newStack, World world) {
|
|
|
|
setEnergyProviderSource(world, getPos(), newStack, false);
|
2020-07-13 20:37:21 +00:00
|
|
|
stack = newStack.copy();
|
|
|
|
markDirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasStack() {
|
|
|
|
return !stack.isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void fromClientTag(CompoundTag compoundTag) {
|
|
|
|
fromTag(Blocks.AIR.getDefaultState(), compoundTag);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public CompoundTag toClientTag(CompoundTag compoundTag) {
|
|
|
|
return toTag(compoundTag);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void markDirty() {
|
|
|
|
super.markDirty();
|
2020-07-21 02:39:30 +00:00
|
|
|
if (hasWorld()) {
|
|
|
|
sync();
|
|
|
|
}
|
2020-07-13 20:37:21 +00:00
|
|
|
}
|
2020-07-27 18:06:46 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getID() {
|
|
|
|
return String.valueOf(BlockEntityType.getId(getType()));
|
|
|
|
}
|
2020-07-13 20:37:21 +00:00
|
|
|
}
|