Skip to content

Commit

Permalink
Start work on improving the formatting system. May switch to Kashike'…
Browse files Browse the repository at this point in the history
…s as Bukkit is shit
  • Loading branch information
me4502 committed Apr 14, 2019
1 parent b5e1f3d commit 3e4004a
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 36 deletions.
Expand Up @@ -26,6 +26,7 @@
import com.sk89q.worldedit.internal.cui.CUIEvent;
import com.sk89q.worldedit.session.SessionKey;
import com.sk89q.worldedit.util.auth.AuthorizationException;
import com.sk89q.worldedit.util.formatting.Fragment;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

Expand Down Expand Up @@ -91,6 +92,12 @@ public void printError(String msg) {
}
}

@Override
public void print(Fragment fragment) {
// TODO Bukkit is bad and the API is somewhat lacking
printRaw(fragment.toString());
}

@Override
public boolean canDestroyBedrock() {
return true;
Expand Down
Expand Up @@ -31,6 +31,7 @@
import com.sk89q.worldedit.math.Vector3;
import com.sk89q.worldedit.session.SessionKey;
import com.sk89q.worldedit.util.HandSide;
import com.sk89q.worldedit.util.formatting.Fragment;
import com.sk89q.worldedit.world.World;
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.block.BlockStateHolder;
Expand Down Expand Up @@ -125,6 +126,12 @@ public void printError(String msg) {
}
}

@Override
public void print(Fragment fragment) {
// TODO Bukkit is bad and the API is somewhat lacking
printRaw(fragment.toString());
}

@Override
public void setPosition(Vector3 pos, float pitch, float yaw) {
player.teleport(new Location(player.getWorld(), pos.getX(), pos.getY(),
Expand Down
Expand Up @@ -23,6 +23,7 @@
import com.sk89q.worldedit.session.SessionOwner;
import com.sk89q.worldedit.util.Identifiable;
import com.sk89q.worldedit.util.auth.Subject;
import com.sk89q.worldedit.util.formatting.Fragment;

import java.io.File;

Expand Down Expand Up @@ -75,6 +76,13 @@ default String getDisplayName() {
*/
void printError(String msg);

/**
* Print a {@link Fragment}.
*
* @param fragment The fragment to print
*/
void print(Fragment fragment);

/**
* Returns true if the actor can destroy bedrock.
*
Expand Down
Expand Up @@ -31,6 +31,7 @@
import com.sk89q.worldedit.session.SessionKey;
import com.sk89q.worldedit.util.HandSide;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.util.formatting.Fragment;
import com.sk89q.worldedit.world.World;
import com.sk89q.worldedit.world.block.BlockStateHolder;
import com.sk89q.worldedit.world.gamemode.GameMode;
Expand Down Expand Up @@ -132,6 +133,11 @@ public void printError(String msg) {
basePlayer.printError(msg);
}

@Override
public void print(Fragment fragment) {
basePlayer.print(fragment);
}

@Override
public String[] getGroups() {
return permActor.getGroups();
Expand Down
Expand Up @@ -90,6 +90,9 @@ public static String getFormattingCode(StyleSet style) {
if (style.isStrikethrough()) {
builder.append(Style.STRIKETHROUGH);
}
if (style.isObfuscated()) {
builder.append(Style.OBFUSCATED);
}
return builder.toString();
}

Expand Down
Expand Up @@ -19,16 +19,45 @@

package com.sk89q.worldedit.util.formatting;

import java.util.ArrayList;
import java.util.List;

/**
* A fragment of text.
*/
public class Fragment {

private final StringBuilder builder = new StringBuilder();

private final List<Fragment> children = new ArrayList<>();
private Fragment lastText;

Fragment() {
}

public List<Fragment> getChildren() {
return children;
}

protected Fragment lastText() {
Fragment text;
if (!children.isEmpty()) {
text = children.get(children.size() - 1);
if (text == lastText) {
return text;
}
}

text = new Fragment();
this.lastText = text;
children.add(text);
return text;
}

public Fragment append(Fragment fragment) {
children.add(fragment);
return this;
}

public Fragment append(String str) {
builder.append(Style.stripColor(str));
return this;
Expand Down
Expand Up @@ -29,8 +29,6 @@

/**
* All supported color values for chat.
*
* <p>From Bukkit.</p>
*/
public enum Style {
/**
Expand Down Expand Up @@ -100,7 +98,7 @@ public enum Style {
/**
* Represents magical characters that change around randomly
*/
RANDOMIZE('k', 0x10, true),
OBFUSCATED('k', 0x10, true),
/**
* Makes the text bold.
*/
Expand Down
Expand Up @@ -19,6 +19,8 @@

package com.sk89q.worldedit.util.formatting;

import java.util.Objects;

/**
* Represents set of styles, such as color, bold, etc.
*/
Expand All @@ -28,6 +30,8 @@ public class StyleSet {
private Boolean italic;
private Boolean underline;
private Boolean strikethrough;
private Boolean obfuscated;
private String insertion;
private Style color;

/**
Expand Down Expand Up @@ -55,6 +59,8 @@ public StyleSet(Style... styles) {
underline = true;
} else if (style == Style.STRIKETHROUGH) {
strikethrough = true;
} else if (style == Style.OBFUSCATED) {
obfuscated = true;
}
}
}
Expand Down Expand Up @@ -167,6 +173,60 @@ public void setStrikethrough(Boolean strikethrough) {
this.strikethrough = strikethrough;
}

/**
* Get whether this style set is obfuscated.
*
* @return true, false, or null if unset
*/
public Boolean getObfuscated() {
return obfuscated;
}

/**
* Get whether this style set is obfuscated.
*
* @return true if there is obfuscation applied
*/
public boolean isObfuscated() {
return getObfuscated() != null && getObfuscated();
}

/**
* Set whether the text is obfuscated.
*
* @param obfuscated false, or null to unset
*/
public void setObfuscated(Boolean obfuscated) {
this.obfuscated = obfuscated;
}

/**
* Get this style set's insertion, if present.
*
* @return the insertion, or null if unset
*/
public String getInsertion() {
return insertion;
}

/**
* Get whether this style set has an insertion.
*
* @return true if there is an insertion
*/
public boolean hasInsertion() {
return insertion != null;
}

/**
* Set the style set's insertion.
*
* @param insertion the insertion, or null to unset
*/
public void setInsertion(String insertion) {
this.insertion = insertion;
}

/**
* Get the color of the text.
*
Expand All @@ -192,7 +252,8 @@ public void setColor(Style color) {
*/
public boolean hasFormatting() {
return getBold() != null || getItalic() != null
|| getUnderline() != null || getStrikethrough() != null;
|| getUnderline() != null || getStrikethrough() != null
|| getObfuscated() != null || getInsertion() != null;
}

/**
Expand All @@ -205,7 +266,9 @@ public boolean hasFormatting() {
public boolean hasEqualFormatting(StyleSet other) {
return getBold() == other.getBold() && getItalic() == other.getItalic()
&& getUnderline() == other.getUnderline() &&
getStrikethrough() == other.getStrikethrough();
getStrikethrough() == other.getStrikethrough() &&
getObfuscated() == other.getObfuscated() &&
Objects.equals(getInsertion(), other.getInsertion());
}

/**
Expand All @@ -229,6 +292,12 @@ public StyleSet extend(StyleSet style) {
if (style.getStrikethrough() != null) {
newStyle.setStrikethrough(style.getStrikethrough());
}
if (style.getObfuscated() != null) {
newStyle.setObfuscated(style.getObfuscated());
}
if (style.getInsertion() != null) {
newStyle.setInsertion(style.getInsertion());
}
if (style.getColor() != null) {
newStyle.setColor(style.getColor());
}
Expand All @@ -242,6 +311,8 @@ public StyleSet clone() {
style.setItalic(getItalic());
style.setUnderline(getUnderline());
style.setStrikethrough(getStrikethrough());
style.setObfuscated(getObfuscated());
style.setInsertion(getInsertion());
style.setColor(getColor());
return style;
}
Expand Down
Expand Up @@ -19,18 +19,13 @@

package com.sk89q.worldedit.util.formatting;

import java.util.ArrayList;
import java.util.List;

/**
* A fragment of text that can be styled.
*/
public class StyledFragment extends Fragment {

private final List<Fragment> children = new ArrayList<>();
private StyleSet style;
private Fragment lastText;


public StyledFragment() {
style = new StyleSet();
}
Expand All @@ -51,36 +46,12 @@ public void setStyles(StyleSet style) {
this.style = style;
}

public List<Fragment> getChildren() {
return children;
}

protected Fragment lastText() {
Fragment text;
if (!children.isEmpty()) {
text = children.get(children.size() - 1);
if (text == lastText) {
return text;
}
}

text = new Fragment();
this.lastText = text;
children.add(text);
return text;
}

public StyledFragment createFragment(Style... styles) {
StyledFragment fragment = new StyledFragment(styles);
append(fragment);
return fragment;
}

public StyledFragment append(StyledFragment fragment) {
children.add(fragment);
return this;
}

@Override
public StyledFragment append(String str) {
lastText().append(str);
Expand Down
@@ -0,0 +1,37 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.sk89q.worldedit.util.formatting.component;

import com.sk89q.worldedit.util.formatting.Style;
import com.sk89q.worldedit.util.formatting.StyledFragment;

/**
* Represents a fragment representing an error.
*/
public class Error extends StyledFragment {

/**
* Create a new instance.
*/
public Error() {
super(Style.RED);
}

}

0 comments on commit 3e4004a

Please sign in to comment.