-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from Wetterquarz/fix-desync-charactorvisitors
Fix desync charactervisitors and Caxton compatibility
- Loading branch information
Showing
19 changed files
with
476 additions
and
445 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/com/kevinsundqvistnorlen/rubi/IRubyStyle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.kevinsundqvistnorlen.rubi; | ||
|
||
import net.minecraft.text.Style; | ||
|
||
import java.util.Optional; | ||
|
||
public interface IRubyStyle { | ||
static Optional<RubyText> getRuby(Style style) { | ||
return Optional.ofNullable(((IRubyStyle) style).rubi$getRuby()); | ||
} | ||
|
||
Style rubi$withRuby(String word, String ruby); | ||
RubyText rubi$getRuby(); | ||
} |
65 changes: 65 additions & 0 deletions
65
src/main/java/com/kevinsundqvistnorlen/rubi/OrderedTextStringVisitable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.kevinsundqvistnorlen.rubi; | ||
|
||
import net.minecraft.text.*; | ||
import org.apache.commons.lang3.mutable.Mutable; | ||
import org.apache.commons.lang3.mutable.MutableObject; | ||
|
||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
/** | ||
* A helper class that breaks down an {@link OrderedText} into sections with the same style. | ||
*/ | ||
public final class OrderedTextStringVisitable implements StringVisitable { | ||
private final OrderedText text; | ||
|
||
public OrderedTextStringVisitable(OrderedText text) { | ||
this.text = text; | ||
} | ||
|
||
@Override | ||
public <T> Optional<T> visit(Visitor<T> visitor) { | ||
return this.visit((style, string) -> visitor.accept(string), Style.EMPTY); | ||
} | ||
|
||
@Override | ||
public <T> Optional<T> visit(StyledVisitor<T> styledVisitor, Style parent) { | ||
if (Objects.equals(this.text, OrderedText.EMPTY)) { | ||
return Optional.empty(); | ||
} | ||
|
||
Mutable<Optional<T>> result = new MutableObject<>(Optional.empty()); | ||
StringBuilder pendingString = new StringBuilder(); | ||
Mutable<Style> pendingStyle = new MutableObject<>(); | ||
|
||
this.text.accept((index, style, codePoint) -> { | ||
Style parentedStyle = style.withParent(parent); | ||
if (!pendingString.isEmpty() && !pendingStyle.getValue().equals(parentedStyle)) { | ||
var optional = styledVisitor.accept(pendingStyle.getValue(), pendingString.toString()); | ||
if (optional.isPresent()) { | ||
result.setValue(optional); | ||
return false; | ||
} | ||
|
||
pendingString.setLength(0); | ||
} | ||
|
||
pendingString.appendCodePoint(codePoint); | ||
pendingStyle.setValue(parentedStyle); | ||
return true; | ||
}); | ||
|
||
if (result.getValue().isPresent()) { | ||
return result.getValue(); | ||
} | ||
|
||
if (!pendingString.isEmpty()) { | ||
var optional = styledVisitor.accept(pendingStyle.getValue(), pendingString.toString()); | ||
if (optional.isPresent()) { | ||
result.setValue(optional); | ||
} | ||
} | ||
|
||
return result.getValue(); | ||
} | ||
} |
Oops, something went wrong.