Fix case when the second section goes further when combining

And added a test for it
This commit is contained in:
Norbi Peti 2020-04-07 23:08:48 +02:00
parent 43525bd93c
commit e4b47efd3f
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
3 changed files with 13 additions and 8 deletions

View file

@ -213,7 +213,7 @@
<dependency>
<groupId>com.palmergames.bukkit.towny</groupId>
<artifactId>Towny</artifactId>
<version>0.95.2.0</version>
<version>0.96.1.0</version>
<scope>provided</scope>
</dependency>
<!-- <dependency> <groupId>au.com.mineauz</groupId> <artifactId>Minigames</artifactId>

View file

@ -120,22 +120,25 @@ public final class ChatFormatter {
sortSections(sections);
continue;
} else if (firstSection.End >= lastSection.Start && firstSection.Start <= lastSection.End) {
int firstSectEnd = firstSection.End;
firstSection.End = lastSection.Start - 1;
int lastSectEnd = lastSection.End;
FormattedSection section = new FormattedSection(firstSection.Settings, lastSection.Start, lastSectEnd,
int middleStart = lastSection.Start;
// |----|-- |------|
// --|----| -|----|-
int middleEnd = Math.min(lastSection.End, firstSection.End);
int lastSectEnd = Math.max(lastSection.End, firstSection.End);
FormattedSection section = new FormattedSection(firstSection.Settings, middleStart, middleEnd,
firstSection.Matches);
section.Settings.copyFrom(lastSection.Settings);
section.Matches.addAll(lastSection.Matches);
sections.add(i, section);
if (firstSectEnd > lastSection.End) { //Copy first section info to last as the lastSection initially cuts the firstSection in half
if (firstSection.End > lastSection.End) { //Copy first section info to last as the lastSection initially cuts the firstSection in half
lastSection.Settings = FormatSettings.builder().build();
lastSection.Settings.copyFrom(firstSection.Settings);
}
lastSection.Start = lastSectEnd + 1;
lastSection.End = firstSectEnd;
firstSection.End = middleStart - 1;
lastSection.Start = middleEnd + 1;
lastSection.End = lastSectEnd;
Predicate<FormattedSection> removeIfNeeded = s -> {
if (s.Start < 0 || s.End < 0 || s.Start > s.End) {

View file

@ -85,6 +85,8 @@ public class ChatFormatIT {
space, new TellrawPart("rainbow").setColor(Color.Gold), space, new TellrawPart("text").setColor(Color.Yellow),
space, new TellrawPart("for").setColor(Color.Green), space, new TellrawPart("testing.").setColor(Color.Blue),
space, new TellrawPart("O").setColor(Color.DarkPurple)).setRainbowMode());
list.add(new ChatFormatIT(sender, "***test*** test", new TellrawPart("test").setColor(Color.White)
.setItalic(true).setBold(true), new TellrawPart(" test").setColor(Color.White)));
return list;
}