2020-08-04 17:06:11 +00:00
|
|
|
package com.thebrokenrail.energonrelics.api.block.energy;
|
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.block.SimpleBlockWithEntity;
|
|
|
|
import com.thebrokenrail.energonrelics.api.block.entity.core.EnergyProviderBlockEntity;
|
2020-07-17 03:55:31 +00:00
|
|
|
import com.thebrokenrail.energonrelics.client.block.entity.render.HighlightBlockEntityRenderer;
|
2020-07-13 20:37:21 +00:00
|
|
|
import net.fabricmc.api.EnvType;
|
|
|
|
import net.fabricmc.api.Environment;
|
|
|
|
import net.fabricmc.fabric.api.client.rendereregistry.v1.BlockEntityRendererRegistry;
|
|
|
|
import net.minecraft.block.BlockState;
|
|
|
|
import net.minecraft.block.entity.BlockEntity;
|
|
|
|
import net.minecraft.client.render.block.entity.BlockEntityRenderDispatcher;
|
|
|
|
import net.minecraft.client.render.block.entity.BlockEntityRenderer;
|
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
|
|
|
import net.minecraft.item.ItemStack;
|
2020-07-22 18:07:29 +00:00
|
|
|
import net.minecraft.loot.context.LootContext;
|
|
|
|
import net.minecraft.loot.context.LootContextParameters;
|
2020-07-13 20:37:21 +00:00
|
|
|
import net.minecraft.util.ActionResult;
|
|
|
|
import net.minecraft.util.Hand;
|
2020-08-04 17:06:11 +00:00
|
|
|
import net.minecraft.util.Identifier;
|
2020-07-13 20:37:21 +00:00
|
|
|
import net.minecraft.util.hit.BlockHitResult;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.function.Function;
|
|
|
|
|
2020-08-04 17:06:11 +00:00
|
|
|
/**
|
|
|
|
* Block That Interacts With Energy
|
|
|
|
*/
|
2020-07-26 23:05:59 +00:00
|
|
|
public abstract class EnergyBlock extends SimpleBlockWithEntity {
|
|
|
|
private static final List<EnergyBlock> blocks = new ArrayList<>();
|
2020-07-13 20:37:21 +00:00
|
|
|
|
2020-07-26 23:05:59 +00:00
|
|
|
public EnergyBlock(Settings settings) {
|
2020-07-13 20:37:21 +00:00
|
|
|
super(settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-08-04 17:06:11 +00:00
|
|
|
public void register(Identifier id) {
|
|
|
|
super.register(id);
|
2020-07-13 20:37:21 +00:00
|
|
|
blocks.add(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Environment(EnvType.CLIENT)
|
|
|
|
public static void initRenderer() {
|
2020-07-26 23:05:59 +00:00
|
|
|
for (EnergyBlock block : blocks) {
|
2020-07-13 20:37:21 +00:00
|
|
|
BlockEntityRendererRegistry.INSTANCE.register(block.type, block.getRenderer());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Environment(EnvType.CLIENT)
|
|
|
|
protected Function<BlockEntityRenderDispatcher, BlockEntityRenderer<BlockEntity>> getRenderer() {
|
|
|
|
return HighlightBlockEntityRenderer::new;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@SuppressWarnings("deprecation")
|
|
|
|
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
|
|
|
|
BlockEntity entity = world.getBlockEntity(pos);
|
|
|
|
if (entity instanceof EnergyProviderBlockEntity && ((EnergyProviderBlockEntity) entity).isEnergyProvider()) {
|
|
|
|
ItemStack stack = player.getStackInHand(hand);
|
2020-08-20 23:03:47 +00:00
|
|
|
if (stack.getItem() == EnergonRelics.Items.NETWORK_CHIP_ITEM) {
|
2020-07-13 20:37:21 +00:00
|
|
|
if (!((EnergyProviderBlockEntity) entity).hasStack()) {
|
|
|
|
if (!world.isClient()) {
|
2020-07-21 02:39:30 +00:00
|
|
|
((EnergyProviderBlockEntity) entity).placeStack(stack, world);
|
2020-07-13 20:37:21 +00:00
|
|
|
stack.setCount(0);
|
|
|
|
}
|
2020-08-20 23:03:47 +00:00
|
|
|
EnergonRelics.Extras.playBeep(world, pos);
|
2020-07-13 20:37:21 +00:00
|
|
|
return ActionResult.SUCCESS;
|
|
|
|
} else {
|
|
|
|
return ActionResult.FAIL;
|
|
|
|
}
|
|
|
|
} else if (stack.isEmpty() && hand == Hand.MAIN_HAND) {
|
2020-07-17 20:20:05 +00:00
|
|
|
if (((EnergyProviderBlockEntity) entity).hasStack()) {
|
|
|
|
if (!world.isClient()) {
|
2020-07-21 02:39:30 +00:00
|
|
|
ItemStack newStack = ((EnergyProviderBlockEntity) entity).takeStack(world);
|
2020-07-17 20:20:05 +00:00
|
|
|
player.setStackInHand(hand, newStack);
|
|
|
|
}
|
2020-08-20 23:03:47 +00:00
|
|
|
EnergonRelics.Extras.playBeep(world, pos);
|
2020-07-17 20:20:05 +00:00
|
|
|
return ActionResult.SUCCESS;
|
|
|
|
} else {
|
|
|
|
return ActionResult.FAIL;
|
2020-07-13 20:37:21 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return ActionResult.PASS;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return ActionResult.PASS;
|
|
|
|
}
|
|
|
|
}
|
2020-07-22 18:07:29 +00:00
|
|
|
|
|
|
|
@SuppressWarnings("deprecation")
|
|
|
|
@Override
|
|
|
|
public List<ItemStack> getDroppedStacks(BlockState state, LootContext.Builder builder) {
|
|
|
|
List<ItemStack> stacks = new ArrayList<>(super.getDroppedStacks(state, builder));
|
|
|
|
if (!builder.getWorld().isClient()) {
|
|
|
|
BlockEntity entity = builder.getNullable(LootContextParameters.BLOCK_ENTITY);
|
|
|
|
if (entity instanceof EnergyProviderBlockEntity && ((EnergyProviderBlockEntity) entity).isEnergyProvider()) {
|
|
|
|
stacks.add(((EnergyProviderBlockEntity) entity).takeStack(builder.getWorld()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return stacks;
|
|
|
|
}
|
2020-07-13 20:37:21 +00:00
|
|
|
}
|