Skip to content

Commit

Permalink
Refactor/covers part 2 (#3891)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Robertz <[email protected]>
  • Loading branch information
YannickMG and Dream-Master authored Feb 5, 2025
1 parent e39e2a4 commit 1631409
Show file tree
Hide file tree
Showing 46 changed files with 319 additions and 462 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import bartworks.system.material.WerkstoffLoader;
import bartworks.system.material.werkstoff_loaders.IWerkstoffRunnable;
import bartworks.util.BWColorUtil;
import gregtech.api.GregTechAPI;
import gregtech.api.covers.CoverRegistry;
import gregtech.api.enums.GTValues;
import gregtech.api.enums.Materials;
import gregtech.api.enums.Textures;
Expand Down Expand Up @@ -201,7 +201,7 @@ public void run(Werkstoff werkstoff) {
final ITexture texture = TextureFactory.of(
Textures.BlockIcons.MACHINE_CASINGS[2][0],
TextureFactory.of(Textures.BlockIcons.OVERLAY_LENS, werkstoff.getRGBA(), false));
GregTechAPI.registerCover(
CoverRegistry.registerCover(
werkstoff.get(lens),
texture,
new CoverLens(BWColorUtil.getDyeFromColor(werkstoff.getRGBA()).mIndex, texture));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import bartworks.system.material.Werkstoff;
import bartworks.system.material.werkstoff_loaders.IWerkstoffRunnable;
import gregtech.api.GregTechAPI;
import gregtech.api.covers.CoverRegistry;
import gregtech.api.recipe.RecipeMaps;
import gregtech.api.render.TextureFactory;
import gregtech.api.util.GTRecipe;
Expand All @@ -47,7 +47,7 @@ public void run(Werkstoff werkstoff) {
1L),
60,
0));
GregTechAPI.registerCover(
CoverRegistry.registerCover(
werkstoff.get(plateDouble),
TextureFactory.of(werkstoff.getTexSet().mTextures[72], werkstoff.getRGBA(), false),
null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import bartworks.system.material.Werkstoff;
import bartworks.system.material.WerkstoffLoader;
import bartworks.system.material.werkstoff_loaders.IWerkstoffRunnable;
import gregtech.api.GregTechAPI;
import gregtech.api.covers.CoverRegistry;
import gregtech.api.enums.GTValues;
import gregtech.api.enums.ItemList;
import gregtech.api.enums.TextureSet;
Expand Down Expand Up @@ -87,7 +87,7 @@ public void run(Werkstoff werkstoff) {
texSet.mTextures[PrefixTextureLinker.blockTexMap.getOrDefault(texSet, block.mTextureIndex)],
werkstoff.getRGBA(),
false) : TextureFactory.of(texSet.mTextures[block.mTextureIndex], werkstoff.getRGBA(), false);
GregTechAPI.registerCover(werkstoff.get(plate), texture, null);
CoverRegistry.registerCover(werkstoff.get(plate), texture, null);

return;
}
Expand Down Expand Up @@ -136,7 +136,7 @@ public void run(Werkstoff werkstoff) {
.eut(16)
.addTo(hammerRecipes);

GregTechAPI.registerCover(
CoverRegistry.registerCover(
werkstoff.get(plate),
TextureFactory.of(werkstoff.getTexSet().mTextures[71], werkstoff.getRGBA(), false),
null);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/gregtech/GTMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import cpw.mods.fml.common.registry.GameRegistry;
import galacticgreg.SpaceDimRegisterer;
import gregtech.api.GregTechAPI;
import gregtech.api.covers.CoverRegistry;
import gregtech.api.enchants.EnchantmentEnderDamage;
import gregtech.api.enchants.EnchantmentHazmat;
import gregtech.api.enchants.EnchantmentRadioactivity;
Expand Down Expand Up @@ -489,7 +490,7 @@ public void onPostLoad(FMLPostInitializationEvent aEvent) {
.transform(i -> new ItemStack(i, 1, GTValues.W))
.orNull();
if (facade != null) {
GregTechAPI.registerCover(facade, null, new CoverFacadeAE());
CoverRegistry.registerCover(facade, null, new CoverFacadeAE());
}

Arrays
Expand Down
81 changes: 1 addition & 80 deletions src/main/java/gregtech/api/GregTechAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,17 @@
import gregtech.api.enums.GTValues;
import gregtech.api.enums.Materials;
import gregtech.api.enums.SoundResource;
import gregtech.api.enums.Textures;
import gregtech.api.interfaces.IDamagableItem;
import gregtech.api.interfaces.ITexture;
import gregtech.api.interfaces.internal.IGTRecipeAdder;
import gregtech.api.interfaces.internal.IThaumcraftCompat;
import gregtech.api.interfaces.metatileentity.IMetaTileEntity;
import gregtech.api.interfaces.tileentity.IMachineBlockUpdateable;
import gregtech.api.metatileentity.BaseMetaTileEntity;
import gregtech.api.objects.GTCoverDefault;
import gregtech.api.objects.GTCoverNone;
import gregtech.api.objects.GTHashSet;
import gregtech.api.objects.GTItemStack;
import gregtech.api.threads.RunnableCableUpdate;
import gregtech.api.threads.RunnableMachineUpdate;
import gregtech.api.util.CircuitryBehavior;
import gregtech.api.util.CoverBehavior;
import gregtech.api.util.CoverBehaviorBase;
import gregtech.api.util.GTCreativeTab;
import gregtech.api.util.GTLog;
import gregtech.api.util.GTModHandler;
Expand Down Expand Up @@ -102,14 +96,7 @@ public class GregTechAPI {
TAB_GREGTECH_ORES = new GTCreativeTab("Ores", "Ores");

public static final IMetaTileEntity[] METATILEENTITIES = new IMetaTileEntity[MAXIMUM_METATILE_IDS];
/**
* The Icon List for Covers
*/
public static final Map<GTItemStack, ITexture> sCovers = new ConcurrentHashMap<>();
/**
* The List of Cover Behaviors for the Covers
*/
public static final Map<GTItemStack, CoverBehaviorBase<?>> sCoverBehaviors = new ConcurrentHashMap<>();

/**
* The List of Circuit Behaviors for the Redstone Circuit Block
*/
Expand Down Expand Up @@ -174,10 +161,6 @@ public class GregTechAPI {
* A List containing all the Materials, which are somehow in use by GT and therefor receive a specific Set of Items.
*/
public static final Materials[] sGeneratedMaterials = new Materials[1000];
/**
* This is the generic Cover behavior. Used for the default Covers, which have no Behavior.
*/
public static final CoverBehavior sDefaultBehavior = new GTCoverDefault(), sNoBehavior = new GTCoverNone();
/**
* For the API Version check
*/
Expand Down Expand Up @@ -266,8 +249,6 @@ public class GregTechAPI {
private static final Set<Class<?>> dummyWorlds = new HashSet<>();

static {
sItemStackMappings.add(sCovers);
sItemStackMappings.add(sCoverBehaviors);
dummyWorlds.add(GTDummyWorld.class);
}

Expand Down Expand Up @@ -443,66 +424,6 @@ public static Comparator<ItemStack> getConfigurationCircuitsComparator() {
.thenComparing(ItemStack::getItemDamage);
}

public static void registerCover(ItemStack aStack, ITexture aCover, CoverBehavior aBehavior) {
registerCover(aStack, aCover, (CoverBehaviorBase<?>) aBehavior);
}

public static void registerCover(ItemStack aStack, ITexture aCover, CoverBehaviorBase<?> aBehavior) {
if (!sCovers.containsKey(new GTItemStack(aStack))) sCovers.put(
new GTItemStack(aStack),
aCover == null || !aCover.isValidTexture() ? Textures.BlockIcons.ERROR_RENDERING[0] : aCover);
if (aBehavior != null) sCoverBehaviors.put(new GTItemStack(aStack), aBehavior);
}

public static void registerCoverBehavior(ItemStack aStack, CoverBehavior aBehavior) {
registerCoverBehavior(aStack, (CoverBehaviorBase<?>) aBehavior);
}

public static void registerCoverBehavior(ItemStack aStack, CoverBehaviorBase<?> aBehavior) {
sCoverBehaviors.put(new GTItemStack(aStack), aBehavior == null ? sDefaultBehavior : aBehavior);
}

/**
* Registers multiple Cover Items. I use that for the OreDict Functionality.
*
* @param aBehavior can be null
*/
public static void registerCover(Collection<ItemStack> aStackList, ITexture aCover, CoverBehavior aBehavior) {
registerCover(aStackList, aCover, (CoverBehaviorBase<?>) aBehavior);
}

/**
* Registers multiple Cover Items. I use that for the OreDict Functionality.
*
* @param aBehavior can be null
*/
public static void registerCover(Collection<ItemStack> aStackList, ITexture aCover,
CoverBehaviorBase<?> aBehavior) {
if (aCover.isValidTexture()) aStackList.forEach(tStack -> GregTechAPI.registerCover(tStack, aCover, aBehavior));
}

/**
* returns a Cover behavior, guaranteed to not return null after preload
*
* @return The Cover behavior
*/
public static CoverBehaviorBase<?> getCoverBehaviorNew(ItemStack aStack) {
if (aStack == null || aStack.getItem() == null) return sNoBehavior;
CoverBehaviorBase<?> rCover = sCoverBehaviors.get(new GTItemStack(aStack));
if (rCover != null) return rCover;
rCover = sCoverBehaviors.get(new GTItemStack(aStack, true));
if (rCover != null) return rCover;
return sDefaultBehavior;
}

/**
* returns a Cover behavior, guaranteed to not return null
*/
public static CoverBehaviorBase<?> getCoverBehaviorNew(int aStack) {
if (aStack == 0) return sNoBehavior;
return getCoverBehaviorNew(GTUtility.intToStack(aStack));
}

/**
* Register a Wrench to be usable on GregTech Machines. The Wrench MUST have some kind of Durability unlike certain
* Buildcraft Wrenches.
Expand Down
78 changes: 78 additions & 0 deletions src/main/java/gregtech/api/covers/CoverRegistry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package gregtech.api.covers;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import net.minecraft.item.ItemStack;

import org.jetbrains.annotations.NotNull;

import gregtech.api.GregTechAPI;
import gregtech.api.enums.Textures;
import gregtech.api.interfaces.ITexture;
import gregtech.api.objects.GTCoverDefault;
import gregtech.api.objects.GTCoverNone;
import gregtech.api.objects.GTItemStack;
import gregtech.api.util.CoverBehaviorBase;
import gregtech.api.util.GTUtility;

public class CoverRegistry {

/**
* The Icon List for Covers
*/
private static final Map<GTItemStack, ITexture> coverTextures = new ConcurrentHashMap<>();
/**
* The List of Cover Behaviors for the Covers
*/
private static final Map<GTItemStack, CoverBehaviorBase<?>> coverBehaviors = new ConcurrentHashMap<>();
/**
* This is the generic Cover behavior. Used for the default Covers, which have no Behavior.
*/
private static final CoverBehaviorBase<?> defaultBehavior = new GTCoverDefault(), noBehavior = new GTCoverNone();

static {
GregTechAPI.sItemStackMappings.add(coverTextures);
GregTechAPI.sItemStackMappings.add(coverBehaviors);
}

public static @NotNull CoverBehaviorBase<?> getEmptyCover() {
return noBehavior;
}

public static void registerCover(ItemStack stack, ITexture cover, CoverBehaviorBase<?> behavior) {
if (!coverTextures.containsKey(new GTItemStack(stack))) coverTextures.put(
new GTItemStack(stack),
cover == null || !cover.isValidTexture() ? Textures.BlockIcons.ERROR_RENDERING[0] : cover);
if (behavior != null) coverBehaviors.put(new GTItemStack(stack), behavior);
}

@NotNull
public static CoverBehaviorBase<?> getCoverBehaviorNew(ItemStack stack) {
if (stack == null || stack.getItem() == null) return noBehavior;
CoverBehaviorBase<?> behavior = coverBehaviors.get(new GTItemStack(stack));
if (behavior != null) return behavior;
behavior = coverBehaviors.get(new GTItemStack(stack, true));
if (behavior != null) return behavior;
return defaultBehavior;
}

@NotNull
public static CoverBehaviorBase<?> getCoverBehaviorNew(int coverId) {
if (coverId == 0) return noBehavior;
return getCoverBehaviorNew(GTUtility.intToStack(coverId));
}

public static boolean isCover(@NotNull ItemStack stack) {
return coverTextures.containsKey(new GTItemStack(stack));
}

public static ITexture getCoverTexture(int coverId) {
return coverTextures.get(new GTItemStack(coverId));
}

public static void reloadCoverColorOverrides() {
coverBehaviors.values()
.forEach(CoverBehaviorBase::reloadColorOverride);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gregtech.api.interfaces.tileentity;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.util.ForgeDirection;
Expand All @@ -25,7 +26,14 @@ default void setCoverDataAtSide(ForgeDirection side, ISerializableObject aData)

boolean setCoverIDAtSideNoUpdate(ForgeDirection side, int aID);

void setCoverItemAtSide(ForgeDirection side, ItemStack aCover);
/**
* Called when the cover is initially attached to a machine.
*
* @param player The attaching player
* @param aCover An {@link ItemStack} containing the cover
* @param side Which side the cover is attached to
*/
void attachCover(EntityPlayer player, ItemStack aCover, ForgeDirection side);

default CoverInfo getCoverInfoAtSide(ForgeDirection side) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import net.minecraftforge.fluids.IFluidHandler;

import gregtech.api.GregTechAPI;
import gregtech.api.covers.CoverRegistry;
import gregtech.api.enums.GTValues;
import gregtech.api.enums.SoundResource;
import gregtech.api.enums.Textures;
Expand Down Expand Up @@ -963,13 +964,12 @@ public boolean onRightclick(EntityPlayer aPlayer, ForgeDirection side, float aX,
final CoverInfo coverInfo = getCoverInfoAtSide(coverSide);

if (coverInfo.getCoverID() == 0) {
if (GTUtility.isStackInList(tCurrentItem, GregTechAPI.sCovers.keySet())) {
final CoverBehaviorBase<?> coverBehavior = GregTechAPI.getCoverBehaviorNew(tCurrentItem);
if (CoverRegistry.isCover(tCurrentItem)) {
final CoverBehaviorBase<?> coverBehavior = CoverRegistry.getCoverBehaviorNew(tCurrentItem);
if (coverBehavior.isCoverPlaceable(coverSide, tCurrentItem, this)
&& mMetaTileEntity.allowCoverOnSide(coverSide, new GTItemStack(tCurrentItem))) {

setCoverItemAtSide(coverSide, tCurrentItem);
coverBehavior.onPlayerAttach(aPlayer, tCurrentItem, this, coverSide);
attachCover(aPlayer, tCurrentItem, coverSide);

mMetaTileEntity.markDirty();
if (!aPlayer.capabilities.isCreativeMode) tCurrentItem.stackSize--;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import cpw.mods.fml.relauncher.ReflectionHelper;
import gregtech.GTMod;
import gregtech.api.GregTechAPI;
import gregtech.api.covers.CoverRegistry;
import gregtech.api.enums.GTValues;
import gregtech.api.enums.ItemList;
import gregtech.api.enums.SoundResource;
Expand Down Expand Up @@ -1641,13 +1642,12 @@ public boolean onRightclick(EntityPlayer aPlayer, ForgeDirection side, float aX,
if (getCoverIDAtSide(side) == 0) coverSide = GTUtility.determineWrenchingSide(side, aX, aY, aZ);

if (getCoverIDAtSide(coverSide) == 0) {
if (GTUtility.isStackInList(tCurrentItem, GregTechAPI.sCovers.keySet())) {
final CoverBehaviorBase<?> coverBehavior = GregTechAPI.getCoverBehaviorNew(tCurrentItem);
if (CoverRegistry.isCover(tCurrentItem)) {
final CoverBehaviorBase<?> coverBehavior = CoverRegistry.getCoverBehaviorNew(tCurrentItem);
if (coverBehavior.isCoverPlaceable(coverSide, tCurrentItem, this)
&& mMetaTileEntity.allowCoverOnSide(coverSide, new GTItemStack(tCurrentItem))) {

setCoverItemAtSide(coverSide, tCurrentItem);
coverBehavior.onPlayerAttach(aPlayer, tCurrentItem, this, coverSide);
attachCover(aPlayer, tCurrentItem, coverSide);

if (!aPlayer.capabilities.isCreativeMode) tCurrentItem.stackSize--;
GTUtility.sendSoundToPlayers(
Expand Down Expand Up @@ -1682,7 +1682,7 @@ public boolean onRightclick(EntityPlayer aPlayer, ForgeDirection side, float aX,
// Configuration of delicate electronics calls for a tool with precision and subtlety.
if (GTModHandler.damageOrDechargeItem(tCurrentItem, 1, 1000, aPlayer)) {
final CoverInfo info = getCoverInfoAtSide(coverSide);
if (info != CoverInfo.EMPTY_INFO) {
if (info.isValid()) {
if (info.allowsTickRateAddition()) {
info.onCoverJackhammer(aPlayer);
GTUtility.sendSoundToPlayers(
Expand Down
Loading

0 comments on commit 1631409

Please sign in to comment.