Skip to content

Commit

Permalink
Infinite Spraycan Additions (GTNewHorizons#3226)
Browse files Browse the repository at this point in the history
Co-authored-by: Caedis <Caedis@users.noreply.github.com>
  • Loading branch information
querns and Caedis authored Sep 19, 2024
1 parent c24b780 commit 7ba0fc9
Show file tree
Hide file tree
Showing 27 changed files with 1,018 additions and 175 deletions.
17 changes: 17 additions & 0 deletions src/main/java/gregtech/api/enums/Dyes.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;

import org.jetbrains.annotations.Contract;

import gregtech.api.interfaces.IColorModulationContainer;
import gregtech.api.objects.GTArrayList;
import gregtech.api.util.GTUtility;
Expand Down Expand Up @@ -123,4 +125,19 @@ public short[] getRGBA() {
public static Dyes getDyeFromIndex(short index) {
return index != -1 ? Dyes.get(index) : Dyes.MACHINE_METAL;
}

/**
* Transforms a dye index between the GT index for this color and the vanilla index for this color.
*
* @param color an integer between 0 and 15
* @return the transformed color
*/
@Contract(pure = true)
public static int transformDyeIndex(final int color) {
if (color < 0 || color > 15) {
throw new IllegalArgumentException("Color passed to transformColor must be between 0 and 15");
}

return (~(byte) color) & 0xF;
}
}
4 changes: 3 additions & 1 deletion src/main/java/gregtech/api/enums/SoundResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ public enum SoundResource {
GT_MACHINES_MULTI_LATHE_LOOP(241, GregTech.ID, "machines.MultiLatheLoop"),
GT_MACHINES_MULTI_AUTOCLAVE_LOOP(242, GregTech.ID, "machines.MultiAutoclaveLoop"),

GT_SPRAYCAN_SHAKE(243, GregTech.ID, "items.spraycan"),
GT_SPRAYCAN_SHAKE(243, GregTech.ID, "items.spraycan_shake"),
GT_SPRAYCAN_LOCK(244, GregTech.ID, "items.spraycan_lock"),
GT_SPRAYCAN_UNLOCK(245, GregTech.ID, "items.spraycan_unlock"),

GUI_BUTTON_DOWN(-1, GregTech.ID, "gui.buttonDown"),
GUI_BUTTON_UP(-1, GregTech.ID, "gui.buttonUp"),
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/gregtech/api/interfaces/IItemBehaviour.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
import net.minecraft.entity.projectile.EntityArrow;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;

import com.google.common.collect.ImmutableList;

import gregtech.api.enums.SubTag;
import gregtech.api.items.MetaBaseItem;

Expand All @@ -21,6 +24,33 @@ default boolean onLeftClick(E aItem, ItemStack aStack, EntityPlayer aPlayer) {
return false;
}

default boolean onMiddleClick(E aItem, ItemStack aStack, EntityPlayer aPlayer) {
return false;
}

/**
* Suppresses standard block activation for a {@link gregtech.common.blocks.BlockMachines GT machine block}. Put
* your item's right click activation in
* {@link #onItemUse(Item, ItemStack, EntityPlayer, World, int, int, int, int, float, float, float) onItemUse}
* for best results.
* <p>
* Typically used when the item needs support for the Ring of Loki (from Botania.) If you don't care about that,
* using
* {@link #onItemUseFirst(Item, ItemStack, EntityPlayer, World, int, int, int, ForgeDirection, float, float, float)
* onItemUseFirst}
* instead of {@link #onItemUse(Item, ItemStack, EntityPlayer, World, int, int, int, int, float, float, float)
* onItemUse}
* will act before block activation with a little less overhead.
*
* @param player the player making the request
* @param tileEntity the tile entity that is attempting to be activated
* @param side the side of the tile entity that the player clicked on
* @return true if standard block activation should be suppressed
*/
default boolean shouldInterruptBlockActivation(EntityPlayer player, TileEntity tileEntity, ForgeDirection side) {
return false;
}

boolean onLeftClickEntity(E aItem, ItemStack aStack, EntityPlayer aPlayer, Entity aEntity);

boolean onItemUse(E aItem, ItemStack aStack, EntityPlayer aPlayer, World aWorld, int aX, int aY, int aZ,
Expand All @@ -33,6 +63,10 @@ boolean onItemUseFirst(E aItem, ItemStack aStack, EntityPlayer aPlayer, World aW

List<String> getAdditionalToolTips(E aItem, List<String> aList, ItemStack aStack);

default List<String> getAdditionalToolTipsWhileSneaking(E aItem, List<String> aList, ItemStack aStack) {
return ImmutableList.of();
}

void onUpdate(E aItem, ItemStack aStack, World aWorld, Entity aPlayer, int aTimer, boolean aIsInHand);

boolean isItemStackUsable(E aItem, ItemStack aStack);
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/gregtech/api/items/GTGenericItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,11 @@ public static class GT_Item_Dispense extends BehaviorProjectileDispense {

@Override
public ItemStack dispenseStack(IBlockSource aSource, ItemStack aStack) {
return ((GTGenericItem) aStack.getItem()).onDispense(aSource, aStack);
final GTGenericItem item = (GTGenericItem) aStack.getItem();
if (item != null) {
return item.onDispense(aSource, aStack);
}
return aStack;
}

@Override
Expand Down
16 changes: 13 additions & 3 deletions src/main/java/gregtech/api/items/MetaBaseItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.IFluidContainerItem;

import com.gtnewhorizons.modularui.api.KeyboardUtil;

import gregtech.api.enums.SubTag;
import gregtech.api.interfaces.IItemBehaviour;
import gregtech.api.util.GTLanguageManager;
Expand Down Expand Up @@ -123,6 +125,10 @@ public boolean onLeftClick(ItemStack aStack, EntityPlayer aPlayer) {
return forEachBehavior(aStack, behavior -> behavior.onLeftClick(this, aStack, aPlayer));
}

public boolean onMiddleClick(ItemStack aStack, EntityPlayer aPlayer) {
return forEachBehavior(aStack, behavior -> behavior.onMiddleClick(this, aStack, aPlayer));
}

@Override
public boolean onLeftClickEntity(ItemStack aStack, EntityPlayer aPlayer, Entity aEntity) {
use(aStack, 0, aPlayer);
Expand Down Expand Up @@ -260,9 +266,13 @@ public final void addInformation(ItemStack aStack, EntityPlayer aPlayer, List<St
"" + formatNumbers(tStats[0])) + EnumChatFormatting.GRAY);
}

ArrayList<IItemBehaviour<MetaBaseItem>> tList = mItemBehaviors.get((short) getDamage(aStack));
if (tList != null) for (IItemBehaviour<MetaBaseItem> tBehavior : tList)
aList = tBehavior.getAdditionalToolTips(this, aList, aStack);
ArrayList<IItemBehaviour<MetaBaseItem>> behaviours = mItemBehaviors.get((short) getDamage(aStack));
if (behaviours != null) {
for (IItemBehaviour<MetaBaseItem> behavior : behaviours) {
aList = !KeyboardUtil.isShiftKeyDown() ? behavior.getAdditionalToolTips(this, aList, aStack)
: behavior.getAdditionalToolTipsWhileSneaking(this, aList, aStack);
}
}

addAdditionalToolTips(aList, aStack, aPlayer);
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/gregtech/api/items/MetaGeneratedItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,12 @@ public final MetaGeneratedItem setElectricStats(int aMetaValue, long aMaxCharge,
return this;
}

@SuppressWarnings("UnusedReturnValue")
public final MetaGeneratedItem setSubIcons(int metaValue, int length) {
mIconList[metaValue] = Arrays.copyOf(mIconList[metaValue], length + 1);
return this;
}

/**
*
* @param aMetaValue the Meta Value of the Item you want to set it to. [0 - 32765]
Expand Down
121 changes: 105 additions & 16 deletions src/main/java/gregtech/api/net/GTPacketInfiniteSpraycan.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package gregtech.api.net;

import java.nio.charset.StandardCharsets;

import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.network.INetHandler;
Expand All @@ -16,16 +18,24 @@

public class GTPacketInfiniteSpraycan extends GTPacketNew {

private boolean wasSneaking;
private Action action;
private int newColor;
private EntityPlayerMP player;

public GTPacketInfiniteSpraycan() {
super(true);
}

public GTPacketInfiniteSpraycan(boolean wasSneaking) {
public GTPacketInfiniteSpraycan(Action action) {
super(false);
this.action = action;
this.newColor = -1;
}

public GTPacketInfiniteSpraycan(Action action, int newColor) {
super(false);
this.wasSneaking = wasSneaking;
this.action = action;
this.newColor = newColor;
}

@Override
Expand All @@ -35,12 +45,21 @@ public byte getPacketID() {

@Override
public void encode(final ByteBuf aOut) {
aOut.writeBoolean(wasSneaking);
final byte[] name = action.name()
.getBytes(StandardCharsets.UTF_8);
aOut.writeInt(newColor);
aOut.writeInt(name.length);
aOut.writeBytes(name);
}

@Override
public GTPacketNew decode(final ByteArrayDataInput aData) {
return new GTPacketInfiniteSpraycan(aData.readBoolean());
final int newColor = aData.readInt();
final int length = aData.readInt();
final byte[] name = new byte[length];
aData.readFully(name, 0, length);

return new GTPacketInfiniteSpraycan(Action.valueOf(new String(name, StandardCharsets.UTF_8)), newColor);
}

@Override
Expand All @@ -53,24 +72,94 @@ public void process(final IBlockAccess aWorld) {
ItemStack currentItemStack = player.inventory.getCurrentItem();
if (currentItemStack != null && currentItemStack.getItem() instanceof MetaBaseItem item) {
item.forEachBehavior(currentItemStack, behavior -> {
if (behavior instanceof BehaviourSprayColorInfinite spraycanBehavior) {
spraycanBehavior.setNewColor(currentItemStack, wasSneaking);
if (behavior instanceof BehaviourSprayColorInfinite spraycanBehavior
&& action.execute(spraycanBehavior, currentItemStack, player, newColor)) {
player.sendSlotContents(player.inventoryContainer, player.inventory.currentItem, currentItemStack);
return true;
}

return false;
});
}
}

public enum Action {

GTUtility.sendSoundToPlayers(
player.worldObj,
SoundResource.GT_SPRAYCAN_SHAKE,
1.0F,
1,
(int) player.posX,
(int) player.posY,
(int) player.posZ);
INCREMENT_COLOR {

@Override
boolean execute(final BehaviourSprayColorInfinite behaviour, final ItemStack itemStack,
final EntityPlayerMP player, final int newColor) {
if (!behaviour.isLocked(itemStack)) {
behaviour.incrementColor(itemStack, player.isSneaking());
playShakeSound(player);

return true;
}
return false;
}
},
LOCK_CAN {

@Override
boolean execute(final BehaviourSprayColorInfinite behavior, final ItemStack itemStack,
final EntityPlayerMP player, final int newColor) {
if (behavior.toggleLock(itemStack)) {
Action.playLockSound(player);
} else {
Action.playUnlockSound(player);
}
return true;
}
},
SET_COLOR {

@Override
boolean execute(final BehaviourSprayColorInfinite behavior, final ItemStack itemStack,
final EntityPlayerMP player, final int newColor) {
if (newColor != -1) {
behavior.setColor(itemStack, (byte) newColor);
Action.playShakeSound(player);
return true;
}
return false;
});
}
};

private static void playShakeSound(final EntityPlayerMP player) {
GTUtility.sendSoundToPlayers(
player.worldObj,
SoundResource.GT_SPRAYCAN_SHAKE,
1.0F,
1,
(int) player.posX,
(int) player.posY,
(int) player.posZ);
}

private static void playLockSound(final EntityPlayerMP player) {
GTUtility.sendSoundToPlayers(
player.worldObj,
SoundResource.GT_SPRAYCAN_LOCK,
1.0F,
1,
(int) player.posX,
(int) player.posY,
(int) player.posZ);
}

private static void playUnlockSound(final EntityPlayerMP player) {
GTUtility.sendSoundToPlayers(
player.worldObj,
SoundResource.GT_SPRAYCAN_UNLOCK,
1.0F,
1,
(int) player.posX,
(int) player.posY,
(int) player.posZ);
}

abstract boolean execute(final BehaviourSprayColorInfinite behavior, ItemStack itemStack, EntityPlayerMP player,
final int newColor);
}
}
Loading

0 comments on commit 7ba0fc9

Please sign in to comment.