aboutsummaryrefslogtreecommitdiff
path: root/RoughlyEnoughItems-api/src/main/java
diff options
context:
space:
mode:
authorshedaniel <daniel@shedaniel.me>2020-09-22 12:20:12 +0800
committershedaniel <daniel@shedaniel.me>2020-09-22 12:20:12 +0800
commit72e44a1432f831bdbd42f1c8a574bf22e8b4a481 (patch)
tree42ce881152e2dab8f4378612c45226ea9b0a773a /RoughlyEnoughItems-api/src/main/java
parentb76420e3bd81886593587c295284b33e57b29cdf (diff)
downloadRoughlyEnoughItems-72e44a1432f831bdbd42f1c8a574bf22e8b4a481.tar.gz
RoughlyEnoughItems-72e44a1432f831bdbd42f1c8a574bf22e8b4a481.tar.bz2
RoughlyEnoughItems-72e44a1432f831bdbd42f1c8a574bf22e8b4a481.zip
Fix #409 and more
Signed-off-by: shedaniel <daniel@shedaniel.me>
Diffstat (limited to 'RoughlyEnoughItems-api/src/main/java')
-rw-r--r--RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/ClientHelper.java52
-rw-r--r--RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/DisplayHelper.java23
-rw-r--r--RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/TextRepresentable.java6
-rw-r--r--RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/widgets/Label.java8
-rw-r--r--RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/utils/ImmutableLiteralText.java14
5 files changed, 84 insertions, 19 deletions
diff --git a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/ClientHelper.java b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/ClientHelper.java
index 73f768e8d..797e44ecb 100644
--- a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/ClientHelper.java
+++ b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/ClientHelper.java
@@ -25,12 +25,18 @@ package me.shedaniel.rei.api;
import me.shedaniel.rei.impl.Internals;
import me.shedaniel.rei.utils.CollectionUtils;
+import me.shedaniel.rei.utils.FormattingUtils;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
+import net.minecraft.ChatFormatting;
+import net.minecraft.client.gui.chat.NarratorChatListener;
+import net.minecraft.core.Registry;
import net.minecraft.network.chat.Component;
+import net.minecraft.network.chat.TextComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
+import net.minecraft.world.item.Items;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -135,7 +141,11 @@ public interface ClientHelper {
* @param item the item to find
* @return the mod name
*/
- String getModFromItem(Item item);
+ default String getModFromItem(Item item) {
+ if (item.equals(Items.AIR))
+ return "";
+ return getModFromIdentifier(Registry.ITEM.getKey(item));
+ }
/**
* Tries to delete the player's cursor item
@@ -148,7 +158,12 @@ public interface ClientHelper {
* @param item the item to find
* @return the mod name with blue and italic formatting
*/
- Component getFormattedModFromItem(Item item);
+ default Component getFormattedModFromItem(Item item) {
+ String mod = getModFromItem(item);
+ if (mod.isEmpty())
+ return NarratorChatListener.NO_TITLE;
+ return new TextComponent(mod).withStyle(ChatFormatting.BLUE, ChatFormatting.ITALIC);
+ }
/**
* Gets the formatted mod from an identifier
@@ -156,7 +171,38 @@ public interface ClientHelper {
* @param identifier the identifier to find
* @return the mod name with blue and italic formatting
*/
- Component getFormattedModFromIdentifier(ResourceLocation identifier);
+ default Component getFormattedModFromIdentifier(ResourceLocation identifier) {
+ String mod = getModFromIdentifier(identifier);
+ if (mod.isEmpty())
+ return NarratorChatListener.NO_TITLE;
+ return new TextComponent(mod).withStyle(ChatFormatting.BLUE, ChatFormatting.ITALIC);
+ }
+
+ /**
+ * Gets the mod from a modid
+ *
+ * @param modid the modid of the mod
+ * @return the mod name with blue and italic formatting
+ */
+ default Component getFormattedModFromModId(String modid) {
+ String mod = getModFromModId(modid);
+ if (mod.isEmpty())
+ return NarratorChatListener.NO_TITLE;
+ return new TextComponent(mod).withStyle(ChatFormatting.BLUE, ChatFormatting.ITALIC);
+ }
+
+ default List<Component> appendModIdToTooltips(List<Component> components, String modId) {
+ final String modName = ClientHelper.getInstance().getModFromModId(modId);
+ boolean alreadyHasMod = false;
+ for (Component s : components)
+ if (FormattingUtils.stripFormatting(s.getString()).equalsIgnoreCase(modName)) {
+ alreadyHasMod = true;
+ break;
+ }
+ if (!alreadyHasMod)
+ components.add(ClientHelper.getInstance().getFormattedModFromModId(modId));
+ return components;
+ }
/**
* Gets the mod from an identifier
diff --git a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/DisplayHelper.java b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/DisplayHelper.java
index 517597f8e..cdafd0ba2 100644
--- a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/DisplayHelper.java
+++ b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/DisplayHelper.java
@@ -36,8 +36,6 @@ import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.function.Supplier;
-import static net.minecraft.world.InteractionResult.PASS;
-
@Environment(EnvType.CLIENT)
public interface DisplayHelper {
@@ -167,15 +165,18 @@ public interface DisplayHelper {
* @see BaseBoundsHandler#registerExclusionZones(Class, Supplier) for easier api
*/
default InteractionResult canItemSlotWidgetFit(int left, int top, T screen, Rectangle fullBounds) {
- InteractionResult fit = isInZone(left, top);
- if (fit == InteractionResult.FAIL)
- return InteractionResult.FAIL;
- InteractionResult fit2 = isInZone(left + 18, top + 18);
- if (fit2 == InteractionResult.FAIL)
- return InteractionResult.FAIL;
- if (fit == InteractionResult.SUCCESS && fit2 == InteractionResult.SUCCESS)
- return InteractionResult.SUCCESS;
- return PASS;
+ InteractionResult fit;
+ fit = isInZone(left, top);
+ if (fit != InteractionResult.PASS)
+ return fit;
+ fit = isInZone(left + 18, top);
+ if (fit != InteractionResult.PASS)
+ return fit;
+ fit = isInZone(left, top + 18);
+ if (fit != InteractionResult.PASS)
+ return fit;
+ fit = isInZone(left + 18, top + 18);
+ return fit;
}
@Override
diff --git a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/TextRepresentable.java b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/TextRepresentable.java
index 96ca2c7af..b346ab822 100644
--- a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/TextRepresentable.java
+++ b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/TextRepresentable.java
@@ -26,10 +26,10 @@ package me.shedaniel.rei.api;
import me.shedaniel.math.impl.PointHelper;
import me.shedaniel.rei.api.widgets.Tooltip;
import me.shedaniel.rei.utils.FormattingUtils;
+import me.shedaniel.rei.utils.ImmutableLiteralText;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.network.chat.Component;
-import net.minecraft.network.chat.TextComponent;
import org.jetbrains.annotations.NotNull;
@Environment(EnvType.CLIENT)
@@ -41,11 +41,11 @@ public interface TextRepresentable {
if (tooltip != null && !tooltip.getText().isEmpty())
return tooltip.getText().get(0);
}
- return new TextComponent("");
+ return ImmutableLiteralText.EMPTY;
}
@NotNull
default Component asFormatStrippedText() {
- return new TextComponent(FormattingUtils.stripFormatting(asFormattedText().getString()));
+ return new ImmutableLiteralText(FormattingUtils.stripFormatting(asFormattedText().getString()));
}
}
diff --git a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/widgets/Label.java b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/widgets/Label.java
index c22188634..55761012b 100644
--- a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/widgets/Label.java
+++ b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/widgets/Label.java
@@ -311,6 +311,8 @@ public abstract class Label extends WidgetWithBounds {
public abstract void setMessage(@NotNull FormattedText message);
+ public abstract void setRainbow(boolean rainbow);
+
@NotNull
public final Label text(@NotNull Component text) {
setText(text);
@@ -322,4 +324,10 @@ public abstract class Label extends WidgetWithBounds {
setMessage(message);
return this;
}
+
+ @NotNull
+ public final Label rainbow(boolean rainbow) {
+ setRainbow(rainbow);
+ return this;
+ }
}
diff --git a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/utils/ImmutableLiteralText.java b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/utils/ImmutableLiteralText.java
index a13aa6dd6..0da3d19f8 100644
--- a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/utils/ImmutableLiteralText.java
+++ b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/utils/ImmutableLiteralText.java
@@ -34,7 +34,7 @@ import java.util.Collections;
import java.util.List;
import java.util.Optional;
-public final class ImmutableLiteralText implements Component {
+public final class ImmutableLiteralText implements MutableComponent {
public static final ImmutableLiteralText EMPTY = new ImmutableLiteralText("");
private final String content;
private FormattedCharSequence orderedText;
@@ -60,7 +60,7 @@ public final class ImmutableLiteralText implements Component {
@Override
public MutableComponent plainCopy() {
- return new TextComponent(content);
+ return this;
}
@Override
@@ -85,4 +85,14 @@ public final class ImmutableLiteralText implements Component {
}
return orderedText;
}
+
+ @Override
+ public MutableComponent setStyle(Style style) {
+ return new TextComponent(content).withStyle(style);
+ }
+
+ @Override
+ public MutableComponent append(Component component) {
+ return new TextComponent(content).append(component);
+ }
}