Skip to content

Instantly share code, notes, and snippets.

@Stapleton
Last active August 17, 2020 10:07
Show Gist options
  • Save Stapleton/b4bde6780b40477015774e79f280bfdd to your computer and use it in GitHub Desktop.
Save Stapleton/b4bde6780b40477015774e79f280bfdd to your computer and use it in GitHub Desktop.
package pw.stapleton.ColouredCategories.crt;
import crafttweaker.IAction;
import crafttweaker.api.item.IIngredient;
import crafttweaker.api.minecraft.CraftTweakerMC;
import net.minecraft.item.ItemStack;
import pw.stapleton.ColouredCategories.ColouredCategories;
import java.util.HashMap;
import java.util.Map;
public class ActionColour implements IAction {
private ItemStack ingredient;
private int bg;
private int bs;
private int be;
public ActionColour(IIngredient ingredients, String background, String borderStart, String borderEnd) {
this.ingredient = CraftTweakerMC.getItemStack(ingredients);
if (background.length() == 6) this.bg = Integer.getInteger("0xf0" + background);
else if (background.length() == 8) this.bg = Integer.getInteger("0x" + background);
else throw new IllegalArgumentException("background colour value must be either 6 characters long (RGB) or 8 characters long (ARGB)");
if (borderStart.length() == 6) this.bs = Integer.getInteger("0xf0" + borderStart);
else if (borderStart.length() == 8) this.bs = Integer.getInteger("0x" + borderStart);
else throw new IllegalArgumentException("borderStart colour value must be either 6 characters long (RGB) or 8 characters long (ARGB)");
if (borderEnd.length() == 6) this.be = Integer.getInteger("0xf0" + borderEnd);
else if (borderEnd.length() == 8) this.be = Integer.getInteger("0x" + borderEnd);
else throw new IllegalArgumentException("borderEnd colour value must be either 6 characters long (RGB) or 8 characters long (ARGB)");
}
public ActionColour(IIngredient[] ingredients, String background, String borderStart, String borderEnd) {
for (IIngredient ingredient : ingredients) {
new ActionColour(ingredient, background, borderStart, borderEnd);
}
}
@Override
public void apply() {
Map<String, Integer> values = new HashMap<>();
values.put("background", this.bg);
values.put("borderStart", this.bs);
values.put("borderEnd", this.be);
ColouredCategories.INGREDIENT_MAP.put(this.ingredient, values);
}
@Override
public String describe() {
return this.ingredient + " has been added and coloured.";
}
}
package pw.stapleton.ColouredCategories.crt;
import crafttweaker.IAction;
import crafttweaker.api.item.IIngredient;
import crafttweaker.api.minecraft.CraftTweakerMC;
import net.minecraft.item.ItemStack;
import net.minecraft.util.NonNullList;
import pw.stapleton.ColouredCategories.ColouredCategories;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public class ActionRandom implements IAction {
private final NonNullList<ItemStack> randItemStacks = NonNullList.create();
private final String[] hexChars = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" };
private String transparency;
public ActionRandom(IIngredient ingredient, String transparency) {
randItemStacks.add(CraftTweakerMC.getItemStack(ingredient));
this.transparency = transparency;
}
public ActionRandom(IIngredient[] ingredients, String transparency) {
for (IIngredient ingredient : ingredients) {
new ActionRandom(ingredient, transparency);
}
}
public Integer random(String transparency) {
StringBuilder hexBuild = new StringBuilder("0x");
Random ran = new Random();
int loop = 8;
if (!transparency.isEmpty()) {
hexBuild.append(transparency);
loop = 6;
}
for (int i = 0; i < loop; i++) {
int ranI = ran.nextInt(hexChars.length);
ColouredCategories.Logger.info("Var: ActionRandom#random().ranI, From: ActionRandom#random() = " + ranI);
ColouredCategories.Logger.info("Var: ActionRandom#random().hexChars[ranI], From: ActionRandom#random(). = " + hexChars[ranI]);
hexBuild.append(hexChars[ranI]);
}
ColouredCategories.Logger.info("Var: ActionRandom#random().hexBuild.toString(), From: ActionRandom#random() = " + hexBuild.toString());
return Integer.getInteger(hexBuild.toString());
}
@Override
public void apply() {
for (ItemStack item : randItemStacks) {
Map<String, Integer> values = new HashMap<>();
ColouredCategories.Logger.info("Call: ActionRandom#random(), From: ActionRandom#apply(). = " + random(this.transparency));
values.put("background", random(this.transparency));
values.put("borderStart", random(this.transparency));
values.put("borderEnd", random(this.transparency));
ColouredCategories.INGREDIENT_MAP.put(item, values);
}
}
@Override
public String describe() {
return "Randomized tooltip colours on " + randItemStacks.size() + " items.";
}
}
package pw.stapleton.ColouredCategories;
import net.minecraft.item.ItemStack;
import net.minecraftforge.client.event.RenderTooltipEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.EventPriority;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import pw.stapleton.ColouredCategories.util.Config;
import pw.stapleton.ColouredCategories.util.Reference;
import java.util.HashMap;
import java.util.Map;
@Mod(modid = Reference.MODID, version = Reference.VERSION, clientSideOnly = true, name = Reference.MOD_NAME, dependencies = Reference.DEPENDENCIES)
public class ColouredCategories {
public static final Map<ItemStack, Map<String, Integer>> INGREDIENT_MAP = new HashMap<>();
public static Logger Logger = LogManager.getLogger(Reference.MOD_NAME);
@Mod.Instance(Reference.MOD_NAME)
public static ColouredCategories instance;
@Mod.EventHandler
public void onPreInit(FMLPreInitializationEvent event) {
new Config(event.getSuggestedConfigurationFile());
MinecraftForge.EVENT_BUS.register(this);
}
@SubscribeEvent(priority = EventPriority.LOWEST)
public void onTooltipColour(RenderTooltipEvent.Color event) {
//Logger.info(INGREDIENT_MAP);
if (event.getStack().isEmpty()) return;
if (!INGREDIENT_MAP.containsKey(event.getStack())) return;
Map<String, Integer> values = INGREDIENT_MAP.get(event.getStack());
event.setBackground(values.get("background"));
event.setBorderStart(values.get("borderStart"));
event.setBorderEnd(values.get("borderEnd"));
}
}
package pw.stapleton.ColouredCategories.crt;
import crafttweaker.CraftTweakerAPI;
import crafttweaker.annotations.ZenRegister;
import crafttweaker.api.item.IIngredient;
import stanhebben.zenscript.annotations.ZenClass;
import stanhebben.zenscript.annotations.ZenMethod;
@ZenRegister
@ZenClass("mods.ColouredCategories")
public class CraftTweaker {
@ZenMethod
public static void colour(IIngredient ingredients, String background, String borderStart, String borderEnd) {
CraftTweakerAPI.apply(new ActionColour(ingredients, background, borderStart, borderEnd));
}
@ZenMethod
public static void colour(IIngredient[] ingredients, String background, String borderStart, String borderEnd) {
CraftTweakerAPI.apply(new ActionColour(ingredients, background, borderStart, borderEnd));
}
@ZenMethod
public static void random(IIngredient ingredient, String transparency) {
CraftTweakerAPI.apply(new ActionRandom(ingredient, transparency));
}
@ZenMethod
public static void random(IIngredient[] ingredients, String transparency) {
CraftTweakerAPI.apply(new ActionRandom(ingredients, transparency));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment