mirror of
https://github.com/GTNewHorizons/CraftTweaker.git
synced 2026-11-21 12:32:29 +00:00
Added tooltip support. Also adds formatted text to make formatted tooltips.
This commit is contained in:
1 parent
25b9589af7
commit
1b2554170a
33 files changed
+1377
-12
No files matched your search
@@ -13,6 +13,7 @@ import minetweaker.annotations.BracketHandler;
|
||||
import minetweaker.annotations.ModOnly;
|
||||
import minetweaker.api.client.IClient;
|
||||
import minetweaker.api.event.IEventManager;
|
||||
import minetweaker.api.formatting.IFormatter;
|
||||
import minetweaker.api.game.IGame;
|
||||
import minetweaker.api.mods.ILoadedMods;
|
||||
import minetweaker.runtime.ITweaker;
|
||||
@@ -88,6 +89,7 @@ public class MineTweakerAPI {
|
||||
registerGlobalSymbol("client", getJavaStaticFieldSymbol(MineTweakerAPI.class, "client"));
|
||||
registerGlobalSymbol("game", getJavaStaticFieldSymbol(MineTweakerAPI.class, "game"));
|
||||
registerGlobalSymbol("loadedMods", getJavaStaticFieldSymbol(MineTweakerAPI.class, "loadedMods"));
|
||||
registerGlobalSymbol("format", getJavaStaticFieldSymbol(MineTweakerAPI.class, "format"));
|
||||
}
|
||||
|
||||
private MineTweakerAPI() {}
|
||||
@@ -96,6 +98,7 @@ public class MineTweakerAPI {
|
||||
* The Tweaker is where you apply undoable actions. Any kind of action that
|
||||
* reloads with the scripts should always be submitted to the tweaker.
|
||||
*/
|
||||
@Deprecated
|
||||
public static final ITweaker tweaker = new MTTweaker();
|
||||
|
||||
/**
|
||||
@@ -148,6 +151,11 @@ public class MineTweakerAPI {
|
||||
*/
|
||||
public static ILoadedMods loadedMods = null;
|
||||
|
||||
/**
|
||||
* Access point to the text formatter.
|
||||
*/
|
||||
public static IFormatter format = null;
|
||||
|
||||
/**
|
||||
* Applies this given action.
|
||||
*
|
||||
|
||||
@@ -19,6 +19,7 @@ import minetweaker.api.event.MTEventManager;
|
||||
import minetweaker.api.event.PlayerInteractEvent;
|
||||
import minetweaker.api.event.PlayerLoggedInEvent;
|
||||
import minetweaker.api.event.PlayerLoggedOutEvent;
|
||||
import minetweaker.api.formatting.IFormatter;
|
||||
import minetweaker.api.game.IGame;
|
||||
import minetweaker.api.item.IItemDefinition;
|
||||
import minetweaker.api.item.IItemStack;
|
||||
@@ -394,18 +395,21 @@ public class MineTweakerImplementationAPI {
|
||||
* @param furnace furnace manager interface
|
||||
* @param game game interface
|
||||
* @param mods mods interface
|
||||
* @param formatter formatter interface
|
||||
*/
|
||||
public static void init(
|
||||
IOreDict oreDict,
|
||||
IRecipeManager recipes,
|
||||
IFurnaceManager furnace,
|
||||
IGame game,
|
||||
ILoadedMods mods) {
|
||||
ILoadedMods mods,
|
||||
IFormatter formatter) {
|
||||
MineTweakerAPI.oreDict = oreDict;
|
||||
MineTweakerAPI.recipes = recipes;
|
||||
MineTweakerAPI.furnace = furnace;
|
||||
MineTweakerAPI.game = game;
|
||||
MineTweakerAPI.loadedMods = mods;
|
||||
MineTweakerAPI.format = formatter;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package minetweaker.api.formatting;
|
||||
|
||||
import stanhebben.zenscript.annotations.OperatorType;
|
||||
import stanhebben.zenscript.annotations.ZenClass;
|
||||
import stanhebben.zenscript.annotations.ZenOperator;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stan
|
||||
*/
|
||||
@ZenClass("minetweaker.formatting.IFormattedText")
|
||||
public interface IFormattedText {
|
||||
@ZenOperator(OperatorType.ADD)
|
||||
public IFormattedText add(IFormattedText other);
|
||||
|
||||
@ZenOperator(OperatorType.CAT)
|
||||
public IFormattedText cat(IFormattedText other);
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package minetweaker.api.formatting;
|
||||
|
||||
import stanhebben.zenscript.annotations.ZenMethod;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stan
|
||||
*/
|
||||
public interface IFormatter {
|
||||
public IFormattedText string(String text);
|
||||
|
||||
@ZenMethod
|
||||
public IFormattedText black(IFormattedText text);
|
||||
|
||||
@ZenMethod
|
||||
public IFormattedText darkBlue(IFormattedText text);
|
||||
|
||||
@ZenMethod
|
||||
public IFormattedText darkGreen(IFormattedText text);
|
||||
|
||||
@ZenMethod
|
||||
public IFormattedText darkAqua(IFormattedText text);
|
||||
|
||||
@ZenMethod
|
||||
public IFormattedText darkRed(IFormattedText text);
|
||||
|
||||
@ZenMethod
|
||||
public IFormattedText darkPurple(IFormattedText text);
|
||||
|
||||
@ZenMethod
|
||||
public IFormattedText gold(IFormattedText text);
|
||||
|
||||
@ZenMethod
|
||||
public IFormattedText gray(IFormattedText text);
|
||||
|
||||
@ZenMethod
|
||||
public IFormattedText darkGray(IFormattedText text);
|
||||
|
||||
@ZenMethod
|
||||
public IFormattedText blue(IFormattedText text);
|
||||
|
||||
@ZenMethod
|
||||
public IFormattedText green(IFormattedText text);
|
||||
|
||||
@ZenMethod
|
||||
public IFormattedText aqua(IFormattedText text);
|
||||
|
||||
@ZenMethod
|
||||
public IFormattedText red(IFormattedText text);
|
||||
|
||||
@ZenMethod
|
||||
public IFormattedText lightPurple(IFormattedText text);
|
||||
|
||||
@ZenMethod
|
||||
public IFormattedText yellow(IFormattedText text);
|
||||
|
||||
@ZenMethod
|
||||
public IFormattedText white(IFormattedText text);
|
||||
|
||||
@ZenMethod
|
||||
public IFormattedText obfuscated(IFormattedText text);
|
||||
|
||||
@ZenMethod
|
||||
public IFormattedText bold(IFormattedText text);
|
||||
|
||||
@ZenMethod
|
||||
public IFormattedText strikethrough(IFormattedText text);
|
||||
|
||||
@ZenMethod
|
||||
public IFormattedText underline(IFormattedText text);
|
||||
|
||||
@ZenMethod
|
||||
public IFormattedText italic(IFormattedText text);
|
||||
}
|
||||
@@ -135,6 +135,13 @@ public class IngredientTransform {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Gives an item back to the player. Also clears the inventory slot at that position.
|
||||
*
|
||||
* @param ingredient
|
||||
* @param givenItem
|
||||
* @return
|
||||
*/
|
||||
@ZenMethod
|
||||
public static IIngredient giveBack(IIngredient ingredient, @Optional final IItemStack givenItem) {
|
||||
return ingredient.transform(new IItemTransformer() {
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package minetweaker.api.tooltip;
|
||||
|
||||
import java.util.List;
|
||||
import minetweaker.IUndoableAction;
|
||||
import minetweaker.MineTweakerAPI;
|
||||
import minetweaker.api.formatting.IFormattedText;
|
||||
import minetweaker.api.item.IIngredient;
|
||||
import minetweaker.api.item.IItemStack;
|
||||
import minetweaker.api.util.IngredientMap;
|
||||
import minetweaker.api.util.IngredientMap.IngredientMapEntry;
|
||||
import stanhebben.zenscript.annotations.ZenExpansion;
|
||||
import stanhebben.zenscript.annotations.ZenMethod;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stan
|
||||
*/
|
||||
@ZenExpansion("minetweaker.item.IIngredient")
|
||||
public class IngredientTooltips {
|
||||
private static final IngredientMap<IFormattedText> TOOLTIPS = new IngredientMap<IFormattedText>();
|
||||
private static final IngredientMap<IFormattedText> SHIFT_TOOLTIPS = new IngredientMap<IFormattedText>();
|
||||
|
||||
@ZenMethod
|
||||
public static void addTooltip(IIngredient ingredient, IFormattedText tooltip) {
|
||||
MineTweakerAPI.apply(new AddTooltipAction(ingredient, tooltip, false));
|
||||
}
|
||||
|
||||
@ZenMethod
|
||||
public static void addShiftTooltip(IIngredient ingredient, IFormattedText tooltip) {
|
||||
MineTweakerAPI.apply(new AddTooltipAction(ingredient, tooltip, true));
|
||||
}
|
||||
|
||||
public static List<IFormattedText> getTooltips(IItemStack item) {
|
||||
return TOOLTIPS.getEntries(item);
|
||||
}
|
||||
|
||||
public static List<IFormattedText> getShiftTooltips(IItemStack item) {
|
||||
return SHIFT_TOOLTIPS.getEntries(item);
|
||||
}
|
||||
|
||||
// ######################
|
||||
// ### Action classes ###
|
||||
// ######################
|
||||
|
||||
private static class AddTooltipAction implements IUndoableAction {
|
||||
private final IIngredient ingredient;
|
||||
private final IFormattedText tooltip;
|
||||
private final boolean shift;
|
||||
|
||||
private IngredientMapEntry<IFormattedText> entry;
|
||||
|
||||
public AddTooltipAction(IIngredient ingredient, IFormattedText tooltip, boolean shift) {
|
||||
this.ingredient = ingredient;
|
||||
this.tooltip = tooltip;
|
||||
this.shift = shift;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply() {
|
||||
entry = (shift ? SHIFT_TOOLTIPS : TOOLTIPS).register(ingredient, tooltip);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canUndo() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void undo() {
|
||||
if (shift) {
|
||||
SHIFT_TOOLTIPS.unregister(entry);
|
||||
} else {
|
||||
TOOLTIPS.unregister(entry);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String describe() {
|
||||
return "Adding tooltip for " + ingredient + ": " + tooltip;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String describeUndo() {
|
||||
return "Removing tooltip for " + ingredient + ": " + tooltip;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getOverrideKey() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package minetweaker.api.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import minetweaker.api.item.IIngredient;
|
||||
import minetweaker.api.item.IItemDefinition;
|
||||
import minetweaker.api.item.IItemStack;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stan
|
||||
*/
|
||||
public class IngredientMap<T> {
|
||||
private final HashMap<IItemDefinition, List<IngredientMapEntry<T>>> entries;
|
||||
|
||||
public IngredientMap() {
|
||||
entries = new HashMap<IItemDefinition, List<IngredientMapEntry<T>>>();
|
||||
}
|
||||
|
||||
public IngredientMapEntry<T> register(IIngredient ingredient, T entry) {
|
||||
Set<IItemDefinition> items = new HashSet<IItemDefinition>();
|
||||
for (IItemStack item : ingredient.getItems()) {
|
||||
items.add(item.getDefinition());
|
||||
}
|
||||
|
||||
IngredientMapEntry<T> actualEntry = new IngredientMapEntry<T>(ingredient, entry);
|
||||
|
||||
for (IItemDefinition item : items) {
|
||||
if (!entries.containsKey(item)) {
|
||||
entries.put(item, new ArrayList<IngredientMapEntry<T>>());
|
||||
}
|
||||
|
||||
entries.get(item).add(actualEntry);
|
||||
}
|
||||
|
||||
return actualEntry;
|
||||
}
|
||||
|
||||
public void unregister(IngredientMapEntry<T> entry) {
|
||||
Set<IItemDefinition> items = new HashSet<IItemDefinition>();
|
||||
for (IItemStack item : entry.ingredient.getItems()) {
|
||||
items.add(item.getDefinition());
|
||||
}
|
||||
|
||||
for (IItemDefinition item : items) {
|
||||
if (entries.containsKey(item)) {
|
||||
entries.get(item).remove(entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public T getFirstEntry(IItemStack item) {
|
||||
for (IngredientMapEntry<T> entry : entries.get(item.getDefinition())) {
|
||||
if (entry.ingredient.matches(item)) {
|
||||
return entry.entry;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<T> getEntries(IItemStack item) {
|
||||
List<IngredientMapEntry<T>> entries = this.entries.get(item.getDefinition());
|
||||
if (entries != null) {
|
||||
ArrayList<T> results = new ArrayList<T>();
|
||||
for (IngredientMapEntry<T> entry : entries) {
|
||||
if (entry.ingredient.matches(item)) {
|
||||
results.add(entry.entry);
|
||||
}
|
||||
}
|
||||
return results;
|
||||
} else {
|
||||
return Collections.EMPTY_LIST;
|
||||
}
|
||||
}
|
||||
|
||||
public static class IngredientMapEntry<T> {
|
||||
private final IIngredient ingredient;
|
||||
private final T entry;
|
||||
|
||||
public IngredientMapEntry(IIngredient ingredient, T entry) {
|
||||
this.ingredient = ingredient;
|
||||
this.entry = entry;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,10 +8,12 @@ package minetweaker.expand;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import minetweaker.MineTweakerAPI;
|
||||
import minetweaker.MineTweakerImplementationAPI;
|
||||
import minetweaker.api.chat.IChatMessage;
|
||||
import minetweaker.api.data.DataString;
|
||||
import minetweaker.api.data.IData;
|
||||
import minetweaker.api.formatting.IFormattedText;
|
||||
import stanhebben.zenscript.annotations.Optional;
|
||||
import stanhebben.zenscript.annotations.ZenCaster;
|
||||
import stanhebben.zenscript.annotations.ZenExpansion;
|
||||
@@ -33,6 +35,11 @@ public class ExpandString {
|
||||
return MineTweakerImplementationAPI.platform.getMessage(value);
|
||||
}
|
||||
|
||||
@ZenCaster
|
||||
public static IFormattedText asFormattedText(String value) {
|
||||
return MineTweakerAPI.format.string(value);
|
||||
}
|
||||
|
||||
@ZenMethod
|
||||
public static List<String> split(String value, String separator, @Optional int maximum) {
|
||||
List<String> result = new ArrayList<String>();
|
||||
|
||||
Reference in new issue
Block a user