Skip to content

Commit

Permalink
Cover API rewrite (#2071)
Browse files Browse the repository at this point in the history
Co-authored-by: brachy84 <[email protected]>
Co-authored-by: Ghzdude <[email protected]>
Co-authored-by: serenibyss <[email protected]>
  • Loading branch information
4 people committed Nov 23, 2023
1 parent 8630295 commit 87e9810
Show file tree
Hide file tree
Showing 99 changed files with 2,406 additions and 2,183 deletions.
8 changes: 4 additions & 4 deletions src/main/java/gregtech/api/block/machines/BlockMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import gregtech.api.block.BlockCustomParticle;
import gregtech.api.block.UnlistedIntegerProperty;
import gregtech.api.block.UnlistedStringProperty;
import gregtech.api.cover.CoverBehavior;
import gregtech.api.cover.Cover;
import gregtech.api.cover.IFacadeCover;
import gregtech.api.items.toolitem.ToolClasses;
import gregtech.api.items.toolitem.ToolHelper;
Expand Down Expand Up @@ -445,9 +445,9 @@ public IBlockState getFacade(@Nonnull IBlockAccess world, @Nonnull BlockPos pos,
public IBlockState getFacade(@Nonnull IBlockAccess world, @Nonnull BlockPos pos, EnumFacing side) {
MetaTileEntity metaTileEntity = getMetaTileEntity(world, pos);
if (metaTileEntity != null && side != null) {
CoverBehavior coverBehavior = metaTileEntity.getCoverAtSide(side);
if (coverBehavior instanceof IFacadeCover) {
return ((IFacadeCover) coverBehavior).getVisualState();
Cover cover = metaTileEntity.getCoverAtSide(side);
if (cover instanceof IFacadeCover facadeCover) {
return facadeCover.getVisualState();
}
}
return world.getBlockState(pos);
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/gregtech/api/capability/GregtechDataCodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public static int assignId() {
// Adjustable Transformer, Adjustable Energy Hatch, Diode
public static final int AMP_INDEX = assignId();

// Facade Cover
public static final int UPDATE_FACADE_STACK = 17;

// Pipe implementation update codes
public static final int UPDATE_INSULATION_COLOR = assignId();
public static final int UPDATE_CONNECTIONS = assignId();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package gregtech.api.capability;

import gregtech.api.capability.impl.AbstractRecipeLogic;
import gregtech.api.cover.ICoverable;
import gregtech.api.cover.CoverHolder;
import gregtech.api.metatileentity.multiblock.IMaintenance;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;
Expand All @@ -11,8 +11,8 @@ public class GregtechTileCapabilities {
@CapabilityInject(IWorkable.class)
public static Capability<IWorkable> CAPABILITY_WORKABLE = null;

@CapabilityInject(ICoverable.class)
public static Capability<ICoverable> CAPABILITY_COVERABLE = null;
@CapabilityInject(CoverHolder.class)
public static Capability<CoverHolder> CAPABILITY_COVER_HOLDER = null;

@CapabilityInject(IControllable.class)
public static Capability<IControllable> CAPABILITY_CONTROLLABLE = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package gregtech.api.capability;

import gregtech.api.capability.impl.AbstractRecipeLogic;
import gregtech.api.cover.ICoverable;
import gregtech.api.cover.CoverHolder;
import gregtech.api.metatileentity.multiblock.IMaintenance;
import gregtech.api.terminal.hardware.HardwareProvider;
import gregtech.api.worldgen.generator.GTWorldGenCapability;
Expand Down Expand Up @@ -37,7 +37,7 @@ public static void init() {
registerCapabilityWithNoDefault(IEnergyContainer.class);
registerCapabilityWithNoDefault(IElectricItem.class);
registerCapabilityWithNoDefault(IWorkable.class);
registerCapabilityWithNoDefault(ICoverable.class);
registerCapabilityWithNoDefault(CoverHolder.class);
registerCapabilityWithNoDefault(IControllable.class);
registerCapabilityWithNoDefault(IActiveOutputSide.class);
registerCapabilityWithNoDefault(IMultiblockController.class);
Expand Down
256 changes: 256 additions & 0 deletions src/main/java/gregtech/api/cover/Cover.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
package gregtech.api.cover;

import codechicken.lib.raytracer.CuboidRayTraceResult;
import codechicken.lib.render.CCRenderState;
import codechicken.lib.render.pipeline.IVertexOperation;
import codechicken.lib.vec.Cuboid6;
import codechicken.lib.vec.Matrix4;
import gregtech.client.utils.BloomEffectUtil;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.*;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.UnknownNullability;
import org.jetbrains.annotations.Unmodifiable;

import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;

public interface Cover {

/**
* @return the CoverableView containing this cover
*/
@NotNull CoverableView getCoverableView();

@NotNull CoverDefinition getDefinition();

/**
* @return the World containing this cover
*/
default @UnknownNullability World getWorld() {
return getCoverableView().getWorld();
}

/**
* @return the pos of this cover
*/
default @UnknownNullability BlockPos getPos() {
return getCoverableView().getPos();
}

/**
* Mark the CoverableView as needing to be saved to the chunk
*/
default void markDirty() {
getCoverableView().markDirty();
}

/**
* Notify block updates for the CoverableView
*/
default void notifyBlockUpdate() {
getCoverableView().notifyBlockUpdate();
}

/**
* Schedule the CoverableView to update rendering
*/
default void scheduleRenderUpdate() {
getCoverableView().scheduleRenderUpdate();
}

/**
* @return tick timer value with a random offset of [0,20]
*/
default long getOffsetTimer() {
return getCoverableView().getOffsetTimer();
}

default void update() {}

default boolean isTickable() {
//noinspection InstanceofThis
return this instanceof ITickable;
}

/**
* @return the side the cover is attached to
*/
@NotNull EnumFacing getAttachedSide();

/**
* @param coverable the CoverableView to attach to
* @param side the side to test
* @return if the cover can attach to the side
*/
boolean canAttach(@NotNull CoverableView coverable, @NotNull EnumFacing side);


/**
* Called when the cover is first attached on the Server Side.
* Do NOT sync custom data to client here. It will overwrite the attach cover packet!
*
* @param coverableView the CoverableView this cover is attached to
* @param side the side this cover is attached to
* @param player the player attaching the cover
* @param itemStack the item used to place the cover
*/
default void onAttachment(@NotNull CoverableView coverableView, @NotNull EnumFacing side, @Nullable EntityPlayer player, @NotNull ItemStack itemStack) {}

/**
* Called when the cover is removed
*/
default void onRemoval() {}

/**
* @return if the cover interacts with an Output Side of a CoverableView
*/
default boolean canInteractWithOutputSide() {
return false;
}

/**
* @return if the pipe this cover is placed on should render a connection to the cover
*/
default boolean shouldAutoConnectToPipes() {
return true;
}

/**
* @return if the pipe this cover is placed on and a pipe on the other side should be able to connect
*/
default boolean canPipePassThrough() {
return false;
}

/**
* @param player the player clicking the cover
* @param hitResult the HitResult of the click
* @return the action's result
*/
default boolean onLeftClick(@NotNull EntityPlayer player, @NotNull CuboidRayTraceResult hitResult) {
return false;
}

/**
* @param player the player clicking the cover
* @param hand the active hand the player is using
* @param hitResult the HitResult of the click
* @return the action's result
*/
default @NotNull EnumActionResult onRightClick(@NotNull EntityPlayer player, @NotNull EnumHand hand, @NotNull CuboidRayTraceResult hitResult) {
return EnumActionResult.PASS;
}

/**
* @param player the player clicking the cover
* @param hand the active hand the player is using
* @param hitResult the HitResult of the click
* @return the action's result
*/
default @NotNull EnumActionResult onScrewdriverClick(@NotNull EntityPlayer player, @NotNull EnumHand hand, @NotNull CuboidRayTraceResult hitResult) {
return EnumActionResult.PASS;
}

/**
* @param player the player clicking the cover
* @param hand the active hand the player is using
* @param hitResult the HitResult of the click
* @return the action's result
*/
default @NotNull EnumActionResult onSoftMalletClick(@NotNull EntityPlayer player, @NotNull EnumHand hand, @NotNull CuboidRayTraceResult hitResult) {
return EnumActionResult.PASS;
}

/**
* @return a list of ItemStacks to drop when removed
*/
default @NotNull @Unmodifiable List<@NotNull ItemStack> getDrops() {
return Collections.singletonList(getPickItem());
}

/**
* @return the ItemStack form of the Cover
*/
default @NotNull ItemStack getPickItem() {
return getDefinition().getDropItemStack();
}

/**
* @return if the Cover can connect to redstone
*/
default boolean canConnectRedstone() {
return false;
}

/**
* Called when the redstone input signal changes.
*
* @param redstone the new signal value
*/
default void onRedstoneInputSignalChange(int redstone) {}

/**
* @return the redstone signal being output from the cover
*/
default int getRedstoneSignalOutput() {
return 0;
}

/**
* Called on client side to render this cover on the machine's face
* It will be automatically translated to prevent Z-fighting with machine faces
*/
@SideOnly(Side.CLIENT)
void renderCover(@NotNull CCRenderState renderState, @NotNull Matrix4 translation, @NotNull IVertexOperation[] pipeline,
@NotNull Cuboid6 plateBox, @NotNull BlockRenderLayer layer);

@SideOnly(Side.CLIENT)
default boolean canRenderInLayer(@NotNull BlockRenderLayer renderLayer) {
return renderLayer == BlockRenderLayer.CUTOUT_MIPPED || renderLayer == BloomEffectUtil.getRealBloomLayer();
}

@SideOnly(Side.CLIENT)
void renderCoverPlate(@NotNull CCRenderState renderState, @NotNull Matrix4 translation, @NotNull IVertexOperation[] pipeline,
@NotNull Cuboid6 plateBox, @NotNull BlockRenderLayer layer);

default boolean canRenderBackside() {
return true;
}

/**
* Will be called for each capability request to the CoverableView
* Cover can override CoverableView capabilities, modify their values, or deny accessing them
*
* @param capability the requested Capability
* @param defaultValue value of the capability from CoverableView itself
* @return the resulting capability the caller will receive
*/
default <T> @Nullable T getCapability(@NotNull Capability<T> capability, @Nullable T defaultValue) {
return defaultValue;
}

default void writeToNBT(@NotNull NBTTagCompound nbt) {}

default void readFromNBT(@NotNull NBTTagCompound nbt) {}

default void writeInitialSyncData(@NotNull PacketBuffer packetBuffer) {}

default void readInitialSyncData(@NotNull PacketBuffer packetBuffer) {}

default void writeCustomData(int discriminator, @NotNull Consumer<@NotNull PacketBuffer> buf) {
getCoverableView().writeCoverData(this, discriminator, buf);
}

default void readCustomData(int discriminator, @NotNull PacketBuffer buf) {}
}
Loading

0 comments on commit 87e9810

Please sign in to comment.