Add custom usercontent loading from json and javascript files alongside bugfixes for page list, refactorings and documentation
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
package exopandora.worldhandler.util;
|
||||
|
||||
import net.minecraft.util.text.ITextComponent;
|
||||
import net.minecraft.util.text.StringTextComponent;
|
||||
import net.minecraft.util.text.TextFormatting;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||
|
||||
@OnlyIn(Dist.CLIENT)
|
||||
public class MutableStringTextComponent extends StringTextComponent
|
||||
{
|
||||
private String text;
|
||||
|
||||
public MutableStringTextComponent()
|
||||
{
|
||||
super(null);
|
||||
this.text = "";
|
||||
}
|
||||
|
||||
public MutableStringTextComponent(String text)
|
||||
{
|
||||
super(null);
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
public void setText(String text)
|
||||
{
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
public String getText()
|
||||
{
|
||||
return this.text;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUnformattedComponentText()
|
||||
{
|
||||
return this.text;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFormattedText()
|
||||
{
|
||||
String formatted = super.getFormattedText();
|
||||
|
||||
if(this.isSpecial())
|
||||
{
|
||||
return MutableStringTextComponent.getSpecialFormattedText(formatted);
|
||||
}
|
||||
|
||||
return formatted;
|
||||
}
|
||||
|
||||
public boolean isSpecial()
|
||||
{
|
||||
return this.text != null && !this.text.isEmpty() && MutableStringTextComponent.getSpecialFormattedText(this.text).contains("\u00A7");
|
||||
}
|
||||
|
||||
public static String getSpecialFormattedText(String text)
|
||||
{
|
||||
String result = text.replaceAll("\u0026", "\u00A7").replaceAll("\u00A7\u00A7", "\u0026");
|
||||
|
||||
if(result.contains("\u00A7"))
|
||||
{
|
||||
result += TextFormatting.RESET;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public String formatter(String string, Integer index)
|
||||
{
|
||||
return this.getStyle().getFormattingCode() + string;
|
||||
}
|
||||
|
||||
public String serialize()
|
||||
{
|
||||
MutableStringTextComponent serial = (MutableStringTextComponent) this.deepCopy();
|
||||
serial.setText(MutableStringTextComponent.getSpecialFormattedText(this.getUnformattedComponentText()));
|
||||
return ITextComponent.Serializer.toJson(serial);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MutableStringTextComponent shallowCopy()
|
||||
{
|
||||
return new MutableStringTextComponent(this.text);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object object)
|
||||
{
|
||||
if(this == object)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else if(!(object instanceof MutableStringTextComponent))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
MutableStringTextComponent stringtextcomponent = (MutableStringTextComponent) object;
|
||||
return this.text.equals(stringtextcomponent.getText()) && super.equals(object);
|
||||
}
|
||||
}
|
||||
}
|
||||
79
src/main/java/exopandora/worldhandler/util/SignText.java
Normal file
79
src/main/java/exopandora/worldhandler/util/SignText.java
Normal file
@@ -0,0 +1,79 @@
|
||||
package exopandora.worldhandler.util;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import net.minecraft.util.text.event.ClickEvent;
|
||||
import net.minecraft.util.text.event.ClickEvent.Action;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||
|
||||
@OnlyIn(Dist.CLIENT)
|
||||
public class SignText
|
||||
{
|
||||
private MutableStringTextComponent text = new MutableStringTextComponent();
|
||||
private final int line;
|
||||
|
||||
public SignText(int line)
|
||||
{
|
||||
this.line = line;
|
||||
}
|
||||
|
||||
public int getLine()
|
||||
{
|
||||
return this.line;
|
||||
}
|
||||
|
||||
public MutableStringTextComponent getString()
|
||||
{
|
||||
return this.text;
|
||||
}
|
||||
|
||||
public void setString(MutableStringTextComponent string)
|
||||
{
|
||||
this.text = string;
|
||||
}
|
||||
|
||||
public void setCommand(String command)
|
||||
{
|
||||
if(command != null && !command.isEmpty())
|
||||
{
|
||||
this.text.getStyle().setClickEvent(new ClickEvent(Action.RUN_COMMAND, command));
|
||||
}
|
||||
else
|
||||
{
|
||||
this.text.getStyle().setClickEvent(null);
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public String getCommand()
|
||||
{
|
||||
if(this.hasCommand())
|
||||
{
|
||||
return this.text.getStyle().getClickEvent().getValue();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean hasCommand()
|
||||
{
|
||||
return this.text.getStyle().getClickEvent() != null && this.text.getStyle().getClickEvent().getAction() == Action.RUN_COMMAND && this.text.getStyle().getClickEvent().getValue() != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
if(this.text.getUnformattedComponentText().isEmpty())
|
||||
{
|
||||
return this.text.getUnformattedComponentText();
|
||||
}
|
||||
|
||||
if(this.text.getStyle().isEmpty() && !this.hasCommand())
|
||||
{
|
||||
return this.text.getFormattedText();
|
||||
}
|
||||
|
||||
return this.text.serialize();
|
||||
}
|
||||
}
|
||||
119
src/main/java/exopandora/worldhandler/util/TextFormatting.java
Normal file
119
src/main/java/exopandora/worldhandler/util/TextFormatting.java
Normal file
@@ -0,0 +1,119 @@
|
||||
package exopandora.worldhandler.util;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import net.minecraft.client.gui.FontRenderer;
|
||||
import net.minecraft.client.resources.I18n;
|
||||
import net.minecraft.util.math.MathHelper;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||
|
||||
@OnlyIn(Dist.CLIENT)
|
||||
public class TextFormatting
|
||||
{
|
||||
public static String shortenString(String str, int maxWidth, FontRenderer fontRenderer)
|
||||
{
|
||||
return TextFormatting.shortenString(str, "", maxWidth, fontRenderer);
|
||||
}
|
||||
|
||||
public static String shortenString(String str, String prefix, int maxWidth, FontRenderer fontRenderer)
|
||||
{
|
||||
if(fontRenderer.getStringWidth(prefix + str) > (maxWidth - fontRenderer.getStringWidth(prefix)))
|
||||
{
|
||||
String result = prefix;
|
||||
|
||||
for(char c : str.toCharArray())
|
||||
{
|
||||
if(fontRenderer.getStringWidth(result + c + "...") < maxWidth)
|
||||
{
|
||||
result += c;
|
||||
}
|
||||
else
|
||||
{
|
||||
return result + "...";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return prefix + str;
|
||||
}
|
||||
|
||||
public static String getTotalTimePlayed(long tick)
|
||||
{
|
||||
int days = 0;
|
||||
int hours = 0;
|
||||
int minutes = 0;
|
||||
int seconds = 0;
|
||||
|
||||
seconds = (int) (tick / 20);
|
||||
|
||||
if(seconds > 60)
|
||||
{
|
||||
int min = MathHelper.floor(seconds / 60);
|
||||
seconds = seconds % 60;
|
||||
minutes = min;
|
||||
}
|
||||
|
||||
if(minutes > 60)
|
||||
{
|
||||
int hrs = MathHelper.floor(minutes / 60);
|
||||
minutes = minutes % 60;
|
||||
hours = hrs;
|
||||
}
|
||||
|
||||
if(hours > 24)
|
||||
{
|
||||
int day = MathHelper.floor(hours / 24);
|
||||
hours = hours % 24;
|
||||
days = day;
|
||||
}
|
||||
|
||||
return String.format("%d:%02d:%02d:%02d", days, hours, minutes, seconds);
|
||||
}
|
||||
|
||||
public static int getHour(long tick)
|
||||
{
|
||||
int hour = MathHelper.floor((tick + 6000) / 1000F) % 24;
|
||||
|
||||
return hour;
|
||||
}
|
||||
|
||||
public static int getMinute(long tick)
|
||||
{
|
||||
int hour = MathHelper.floor((tick + 6000F) / 1000F);
|
||||
int minute = MathHelper.floor((tick + 6000F - hour * 1000) * 6 / 100);
|
||||
|
||||
return minute;
|
||||
}
|
||||
|
||||
public static String formatWorldTime(long tick)
|
||||
{
|
||||
int hour = TextFormatting.getHour(tick);
|
||||
int minute = TextFormatting.getMinute(tick);
|
||||
|
||||
return String.format("%02d:%02d", hour, minute);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static String formatNullable(String text, Object... parameters)
|
||||
{
|
||||
if(text == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return I18n.format(text, parameters);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public static String formatNonnull(String text, Object... parameters)
|
||||
{
|
||||
if(text == null)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
return I18n.format(text, parameters);
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,6 @@ import com.mojang.blaze3d.platform.GlStateManager;
|
||||
|
||||
import exopandora.worldhandler.config.Config;
|
||||
import exopandora.worldhandler.helper.ResourceHelper;
|
||||
import exopandora.worldhandler.text.TextFormatting;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.screen.Screen;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
|
||||
Reference in New Issue
Block a user