Skip to content

Commit

Permalink
Dropper covers that work like conveyors in GT5u when there is no bloc…
Browse files Browse the repository at this point in the history
…k in front of them (#108)

* yippe

* Adjustable drop rate

* Better UI and TOP integration

* Recipes

* Textures (thx mina!!!!)

* whoopies
  • Loading branch information
Zorbatron authored Dec 18, 2024
1 parent f4c5a48 commit 8915ff6
Show file tree
Hide file tree
Showing 38 changed files with 505 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/main/java/com/zorbatron/zbgt/ZBGTCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.SidedProxy;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;

Expand Down Expand Up @@ -83,6 +84,11 @@ public void preInit(FMLPreInitializationEvent event) {
proxy.preInit();
}

@EventHandler
public void init(FMLInitializationEvent event) {
proxy.init();
}

@EventHandler
public void postInit(FMLPostInitializationEvent event) throws IOException {
proxy.postInit();
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/zorbatron/zbgt/common/CommonProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.zorbatron.zbgt.common.block.ZBGTMetaBlocks;
import com.zorbatron.zbgt.common.covers.ZBGTCovers;
import com.zorbatron.zbgt.common.items.ZBGTMetaItems;
import com.zorbatron.zbgt.integration.theoneprobe.ZBGTTOPModule;
import com.zorbatron.zbgt.recipe.ZBGTRecipes;

import gregtech.api.GTValues;
Expand All @@ -41,6 +42,10 @@ public void preInit() {
ZBGTMetaItems.init();
}

public void init() {
ZBGTTOPModule.init();
}

public void postInit() throws IOException {
if (ZBGTConfig.worldGenerationSettings.enableOreGeneration) {
CustomOreVeins.init();
Expand Down
203 changes: 203 additions & 0 deletions src/main/java/com/zorbatron/zbgt/common/covers/CoverDropper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
package com.zorbatron.zbgt.common.covers;

import net.minecraft.client.resources.I18n;
import net.minecraft.dispenser.BehaviorDefaultDispenseItem;
import net.minecraft.dispenser.IPosition;
import net.minecraft.dispenser.PositionImpl;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
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.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

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.api.capability.GregtechDataCodes;
import gregtech.api.capability.GregtechTileCapabilities;
import gregtech.api.capability.IControllable;
import gregtech.api.cover.CoverBase;
import gregtech.api.cover.CoverDefinition;
import gregtech.api.cover.CoverWithUI;
import gregtech.api.cover.CoverableView;
import gregtech.api.gui.GuiTextures;
import gregtech.api.gui.ModularUI;
import gregtech.api.gui.widgets.DynamicLabelWidget;
import gregtech.api.gui.widgets.SliderWidget;
import gregtech.client.renderer.texture.Textures;

public class CoverDropper extends CoverBase implements ITickable, IControllable, CoverWithUI {

private boolean isWorkingEnabled = true;
private int itemsLeftToTransferLastSecond;
private int updateRate;
private final int maxTransfer;

public CoverDropper(@NotNull CoverDefinition definition, @NotNull CoverableView coverableView,
@NotNull EnumFacing attachedSide, int maxTransfer) {
super(definition, coverableView, attachedSide);
this.maxTransfer = maxTransfer;
this.updateRate = 20;
}

@Override
public boolean isWorkingEnabled() {
return isWorkingEnabled;
}

@Override
public void setWorkingEnabled(boolean isWorkingAllowed) {
isWorkingEnabled = isWorkingAllowed;

if (!getWorld().isRemote) {
writeCustomData(GregtechDataCodes.WORKING_ENABLED, buf -> buf.writeBoolean(isWorkingAllowed));
}
}

@Override
public boolean canAttach(@NotNull CoverableView coverable, @NotNull EnumFacing side) {
return coverable.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side);
}

@Override
public void renderCover(@NotNull CCRenderState renderState, @NotNull Matrix4 translation,
@NotNull IVertexOperation[] pipeline, @NotNull Cuboid6 plateBox,
@NotNull BlockRenderLayer layer) {
Textures.CONVEYOR_OVERLAY.renderSided(getAttachedSide(), plateBox, renderState, pipeline, translation);
}

@Override
public void update() {
CoverableView coverable = getCoverableView();
long timer = coverable.getOffsetTimer();

if (timer % updateRate == 0 && itemsLeftToTransferLastSecond > 0) {
IItemHandler extractFrom = coverable.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY,
getAttachedSide());

if (extractFrom != null) {
int totalTransferred = dropItems(extractFrom, itemsLeftToTransferLastSecond);
this.itemsLeftToTransferLastSecond -= totalTransferred;
}
}

if (timer % 20 == 0) {
this.itemsLeftToTransferLastSecond = maxTransfer;
}
}

private int dropItems(IItemHandler extractFrom, int maxTransferAmount) {
int itemsLeftToTransfer = maxTransferAmount;

for (int slot = 0; slot < extractFrom.getSlots(); slot++) {
ItemStack stack = extractFrom.extractItem(slot, itemsLeftToTransfer, true);
if (stack.isEmpty()) continue;

stack = extractFrom.extractItem(slot, itemsLeftToTransfer, false);
itemsLeftToTransfer -= stack.getCount();

actuallyDropItems(stack);
}

return itemsLeftToTransfer;
}

private void actuallyDropItems(ItemStack stack) {
EnumFacing facing = getAttachedSide();

BehaviorDefaultDispenseItem.doDispense(getWorld(), stack, 6, facing,
getDropPosition(getPos(), facing));
}

private IPosition getDropPosition(BlockPos blockPos, EnumFacing facing) {
double d0 = blockPos.getX() + 0.7D * (double) facing.getXOffset() + 0.5D;
double d1 = blockPos.getY() + 0.7D * (double) facing.getYOffset() + 0.5D;
double d2 = blockPos.getZ() + 0.7D * (double) facing.getZOffset() + 0.5D;

return new PositionImpl(d0, d1, d2);
}

@Override
public ModularUI createUI(EntityPlayer player) {
return ModularUI.builder(GuiTextures.BACKGROUND, 110, 30)
.widget(new SliderWidget("", 5, 5, 100, 10, 1, 100, updateRate, val -> {
updateRate = (int) val;
writeCustomData(GregtechDataCodes.UPDATE_COVER_MODE, buf -> buf.writeInt(updateRate));
}))
.widget(new DynamicLabelWidget(5, 17, () -> updateRate == 1 ?
I18n.format("cover.cover_dropper.update_rate_label.2") :
I18n.format("cover.cover_dropper.update_rate_label.1", updateRate)))
.build(this, player);
}

@Override
public @NotNull EnumActionResult onScrewdriverClick(@NotNull EntityPlayer playerIn, @NotNull EnumHand hand,
@NotNull CuboidRayTraceResult hitResult) {
if (!getCoverableView().getWorld().isRemote) {
openUI((EntityPlayerMP) playerIn);
}
return EnumActionResult.SUCCESS;
}

@Override
public void readCustomData(int discriminator, @NotNull PacketBuffer buf) {
super.readCustomData(discriminator, buf);

if (discriminator == GregtechDataCodes.WORKING_ENABLED) {
isWorkingEnabled = buf.readBoolean();
} else if (discriminator == GregtechDataCodes.UPDATE_COVER_MODE) {
updateRate = buf.readInt();
}
}

@Override
public void writeInitialSyncData(@NotNull PacketBuffer packetBuffer) {
super.writeInitialSyncData(packetBuffer);
packetBuffer.writeBoolean(isWorkingEnabled);
packetBuffer.writeInt(updateRate);
}

@Override
public void readInitialSyncData(@NotNull PacketBuffer packetBuffer) {
super.readInitialSyncData(packetBuffer);
isWorkingEnabled = packetBuffer.readBoolean();
updateRate = packetBuffer.readInt();
}

@Override
public <T> @Nullable T getCapability(@NotNull Capability<T> capability, @Nullable T defaultValue) {
if (capability == GregtechTileCapabilities.CAPABILITY_CONTROLLABLE) {
return GregtechTileCapabilities.CAPABILITY_CONTROLLABLE.cast(this);
}

return super.getCapability(capability, defaultValue);
}

@Override
public void writeToNBT(@NotNull NBTTagCompound nbt) {
super.writeToNBT(nbt);
nbt.setBoolean("IsWorkingEnabled", isWorkingEnabled);
nbt.setInteger("UpdateRate", updateRate);
}

@Override
public void readFromNBT(@NotNull NBTTagCompound nbt) {
super.readFromNBT(nbt);
isWorkingEnabled = nbt.getBoolean("IsWorkingEnabled");
updateRate = nbt.getInteger("UpdateRate");
}

public int getUpdateRate() {
return updateRate;
}
}
27 changes: 27 additions & 0 deletions src/main/java/com/zorbatron/zbgt/common/covers/ZBGTCovers.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,32 @@ public static void init() {
CoverBehaviors.registerBehavior(zbgtId("precise_dual_cover.uv"), ZBGTMetaItems.PRECISE_DUAL_COVER_UV,
(def, tile, side) -> new CoverPreciseDualCover(def, tile, side, GTValues.UV, 16 * 64,
1280 * 64 * 64 * 4));

CoverBehaviors.registerBehavior(zbgtId("cover_dropper.lv"), ZBGTMetaItems.DROPPER_COVER_LV,
(def, tile, side) -> new CoverDropper(def, tile, side, 8));
CoverBehaviors.registerBehavior(zbgtId("cover_dropper.mv"), ZBGTMetaItems.DROPPER_COVER_MV,
(def, tile, side) -> new CoverDropper(def, tile, side, 8 * 2));
CoverBehaviors.registerBehavior(zbgtId("cover_dropper.hv"), ZBGTMetaItems.DROPPER_COVER_HV,
(def, tile, side) -> new CoverDropper(def, tile, side, 8 * 4));
CoverBehaviors.registerBehavior(zbgtId("cover_dropper.ev"), ZBGTMetaItems.DROPPER_COVER_EV,
(def, tile, side) -> new CoverDropper(def, tile, side, 8 * 8));
CoverBehaviors.registerBehavior(zbgtId("cover_dropper.iv"), ZBGTMetaItems.DROPPER_COVER_IV,
(def, tile, side) -> new CoverDropper(def, tile, side, 8 * 16));
CoverBehaviors.registerBehavior(zbgtId("cover_dropper.luv"), ZBGTMetaItems.DROPPER_COVER_LuV,
(def, tile, side) -> new CoverDropper(def, tile, side, 8 * 32));
CoverBehaviors.registerBehavior(zbgtId("cover_dropper.zpm"), ZBGTMetaItems.DROPPER_COVER_ZPM,
(def, tile, side) -> new CoverDropper(def, tile, side, 8 * 64));
CoverBehaviors.registerBehavior(zbgtId("cover_dropper.uv"), ZBGTMetaItems.DROPPER_COVER_UV,
(def, tile, side) -> new CoverDropper(def, tile, side, 8 * 128));
CoverBehaviors.registerBehavior(zbgtId("cover_dropper.uhv"), ZBGTMetaItems.DROPPER_COVER_UHV,
(def, tile, side) -> new CoverDropper(def, tile, side, 8 * 256));
CoverBehaviors.registerBehavior(zbgtId("cover_dropper.uev"), ZBGTMetaItems.DROPPER_COVER_UEV,
(def, tile, side) -> new CoverDropper(def, tile, side, 8 * 512));
CoverBehaviors.registerBehavior(zbgtId("cover_dropper.uiv"), ZBGTMetaItems.DROPPER_COVER_UIV,
(def, tile, side) -> new CoverDropper(def, tile, side, 8 * 1024));
CoverBehaviors.registerBehavior(zbgtId("cover_dropper.uxv"), ZBGTMetaItems.DROPPER_COVER_UXV,
(def, tile, side) -> new CoverDropper(def, tile, side, 8 * 2048));
CoverBehaviors.registerBehavior(zbgtId("cover_dropper.opv"), ZBGTMetaItems.DROPPER_COVER_OpV,
(def, tile, side) -> new CoverDropper(def, tile, side, 8 * 4096));
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/zorbatron/zbgt/common/items/ZBGTMetaItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,5 +289,25 @@ public void registerSubItems() {
WRAPPED_CRYSTAL_SOC = addItem(115, "wrapped.misc.crystal_soc");

ZBGT_ITEM = addItem(116, "zbgt").setInvisible();

// Dropper Covers 117-129
DROPPER_COVER_LV = addItem(117, "cover.cover_dropper.lv");
DROPPER_COVER_MV = addItem(118, "cover.cover_dropper.mv");
DROPPER_COVER_HV = addItem(119, "cover.cover_dropper.hv");
DROPPER_COVER_EV = addItem(120, "cover.cover_dropper.ev");
DROPPER_COVER_IV = addItem(121, "cover.cover_dropper.iv");
DROPPER_COVER_LuV = addItem(122, "cover.cover_dropper.luv");
DROPPER_COVER_ZPM = addItem(123, "cover.cover_dropper.zpm");
DROPPER_COVER_UV = addItem(124, "cover.cover_dropper.uv");
DROPPER_COVER_UHV = addItem(125, "cover.cover_dropper.uhv")
.setInvisibleIf(!GregTechAPI.isHighTier());
DROPPER_COVER_UEV = addItem(126, "cover.cover_dropper.uev")
.setInvisibleIf(!GregTechAPI.isHighTier());
DROPPER_COVER_UIV = addItem(127, "cover.cover_dropper.uiv")
.setInvisibleIf(!GregTechAPI.isHighTier());
DROPPER_COVER_UXV = addItem(128, "cover.cover_dropper.uxv")
.setInvisibleIf(!GregTechAPI.isHighTier());
DROPPER_COVER_OpV = addItem(129, "cover.cover_dropper.opv")
.setInvisibleIf(!GregTechAPI.isHighTier());
}
}
14 changes: 14 additions & 0 deletions src/main/java/com/zorbatron/zbgt/common/items/ZBGTMetaItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,20 @@ public class ZBGTMetaItems {
public static MetaItem<?>.MetaValueItem WRAPPED_CRYSTAL_CPU;
public static MetaItem<?>.MetaValueItem WRAPPED_CRYSTAL_SOC;

public static MetaItem<?>.MetaValueItem DROPPER_COVER_LV;
public static MetaItem<?>.MetaValueItem DROPPER_COVER_MV;
public static MetaItem<?>.MetaValueItem DROPPER_COVER_HV;
public static MetaItem<?>.MetaValueItem DROPPER_COVER_EV;
public static MetaItem<?>.MetaValueItem DROPPER_COVER_IV;
public static MetaItem<?>.MetaValueItem DROPPER_COVER_LuV;
public static MetaItem<?>.MetaValueItem DROPPER_COVER_ZPM;
public static MetaItem<?>.MetaValueItem DROPPER_COVER_UV;
public static MetaItem<?>.MetaValueItem DROPPER_COVER_UHV;
public static MetaItem<?>.MetaValueItem DROPPER_COVER_UEV;
public static MetaItem<?>.MetaValueItem DROPPER_COVER_UIV;
public static MetaItem<?>.MetaValueItem DROPPER_COVER_UXV;
public static MetaItem<?>.MetaValueItem DROPPER_COVER_OpV;

public static void init() {
ZBGTMetaItem item = new ZBGTMetaItem();
item.setRegistryName("zbgt_meta_item");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.zorbatron.zbgt.integration.theoneprobe;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.capabilities.Capability;

import org.jetbrains.annotations.NotNull;

import com.zorbatron.zbgt.ZBGTCore;
import com.zorbatron.zbgt.common.covers.CoverDropper;

import gregtech.api.capability.GregtechTileCapabilities;
import gregtech.api.cover.Cover;
import gregtech.api.cover.CoverHolder;
import gregtech.integration.theoneprobe.provider.CapabilityInfoProvider;
import mcjty.theoneprobe.api.IProbeHitData;
import mcjty.theoneprobe.api.IProbeInfo;

public class DropperCoverProvider extends CapabilityInfoProvider<CoverHolder> {

@Override
protected @NotNull Capability<CoverHolder> getCapability() {
return GregtechTileCapabilities.CAPABILITY_COVER_HOLDER;
}

@Override
protected void addProbeInfo(CoverHolder capability, IProbeInfo probeInfo, EntityPlayer player,
TileEntity tileEntity, IProbeHitData data) {
Cover cover = capability.getCoverAtSide(data.getSideHit());
if (cover instanceof CoverDropper coverDropper) {
int updateRate = coverDropper.getUpdateRate();
probeInfo.text(updateRate == 1 ? "{*cover.cover_dropper.update_rate.3*}" :
"{*cover.cover_dropper.update_rate.1*}" + updateRate + "{*cover.cover_dropper.update_rate.2*}");
}
}

@Override
public String getID() {
return ZBGTCore.MODID + ":dropper_cover_provider";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.zorbatron.zbgt.integration.theoneprobe;

import com.zorbatron.zbgt.api.util.ZBGTLog;

import mcjty.theoneprobe.TheOneProbe;
import mcjty.theoneprobe.api.ITheOneProbe;

public class ZBGTTOPModule {

public static void init() {
ZBGTLog.logger.info("Registering TOP providers");

ITheOneProbe oneProbe = TheOneProbe.theOneProbeImp;

oneProbe.registerProvider(new DropperCoverProvider());
}
}
Loading

0 comments on commit 8915ff6

Please sign in to comment.