This repository has been archived on 2023-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
EnergonRelics/src/main/java/com/thebrokenrail/energonrelics/api/block/entity/core/EnergyProviderBlockEntity.java

202 lines
6.0 KiB
Java
Raw Normal View History

2020-08-04 17:06:11 +00:00
package com.thebrokenrail.energonrelics.api.block.entity.core;
2020-07-13 20:37:21 +00:00
import com.thebrokenrail.energonrelics.EnergonRelics;
2020-08-04 17:06:11 +00:00
import com.thebrokenrail.energonrelics.api.energy.Action;
2020-07-13 20:37:21 +00:00
import com.thebrokenrail.energonrelics.component.NetworkComponent;
2020-08-04 17:06:11 +00:00
import com.thebrokenrail.energonrelics.config.HardcodedConfig;
import com.thebrokenrail.energonrelics.api.energy.tick.EnergyTickable;
import com.thebrokenrail.energonrelics.api.energy.tick.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-08-04 17:06:11 +00:00
/**
* Block Entity That Provides Energy
*/
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);
}
2020-08-04 17:06:11 +00:00
/**
* Give Propagated Action To This Block
* @param action Action
*/
2020-07-13 20:37:21 +00:00
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();
}
}
2020-08-04 17:06:11 +00:00
/**
* Is In Energy Network
* @param network Network ID
* @return If Is In Network
*/
2020-07-13 20:37:21 +00:00
public final boolean isNetwork(int network) {
return isEnergyProvider() && EnergonRelics.NETWORK_CHIP_ITEM.getID(stack) == network;
}
2020-08-04 17:06:11 +00:00
/**
* Is Within Valid Distance Of Position
* @param pos Position
* @return Is Within Distance
*/
2020-07-13 20:37:21 +00:00
public final boolean isWithinDistance(Vec3d pos) {
return getPos().isWithinDistance(pos, HardcodedConfig.POWER_RANGE);
2020-07-13 20:37:21 +00:00
}
2020-08-04 17:06:11 +00:00
/**
* Get Current Dimension
* @return Current Dimension
*/
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;
}
}
2020-08-04 17:06:11 +00:00
/**
* Should Behave As Energy Provider
* @return Is Energy Provider
*/
2020-07-13 20:37:21 +00:00
public boolean isEnergyProvider() {
return false;
}
2020-08-04 17:06:11 +00:00
/**
* Override To Handle Propagated Action
* @param action Propagated Action
*/
2020-07-13 20:37:21 +00:00
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-08-04 17:06:11 +00:00
/**
* Take Network Chip Item Stack
* @param world World
* @return Item Stack
*/
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-08-04 17:06:11 +00:00
/**
* Place Network Chip Item Stack
* @param newStack Item Stack
* @param world World
*/
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();
}
2020-08-04 17:06:11 +00:00
/**
* Has Item Stack
* @return Has Item Stack
*/
2020-07-13 20:37:21 +00:00
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
}