Skip to content

Commit

Permalink
* Reformatted GradientColor
Browse files Browse the repository at this point in the history
* Fixed up DefaultColor
* New methods in AnvilMenu
* New method in StringUtils
* New method in FileManager
  • Loading branch information
Hempfest committed May 1, 2021
1 parent f5fc4cf commit bd5b1a8
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 31 deletions.
8 changes: 0 additions & 8 deletions .idea/artifacts/Labyrinth_jar.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Labyrinth.iml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<orderEntry type="library" name="Maven: org.yaml:snakeyaml:1.26" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: me.clip:placeholderapi:2.10.6" level="project" />
<orderEntry type="library" name="Maven: com.github.MilkBowl:VaultAPI:1.7" level="project" />
<orderEntry type="library" name="Maven: org.bukkit:bukkit:1.13.1-R0.1-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: org.bukkit:bukkit:1.16.1-R0.1-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: com.googlecode.json-simple:json-simple:1.1.1" level="project" />
<orderEntry type="library" name="Maven: junit:junit:4.10" level="project" />
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.1" level="project" />
Expand Down
32 changes: 30 additions & 2 deletions src/main/java/com/github/sanctum/labyrinth/data/FileManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,36 @@ public static void copy(InputStream in, File file) {
OutputStream out = new FileOutputStream(file);
byte[] buf = new byte[1024];
int len;
while((len=in.read(buf))>0){
out.write(buf,0,len);
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.close();
in.close();
} catch (FileNotFoundException e) {
throw new IllegalArgumentException("File is a directory!", e);
} catch (IOException e) {
throw new IllegalStateException("Unable to write to file! See log:", e);
}
}

/**
* Copy an InputStream directly to a given File.
* <p>
* Useful for placing resources retrieved from a JavaPlugin
* implementation at custom locations.
*
* @param in an InputStream, likely a plugin resource
* @param manager the manager to locate to.
* @throws IllegalArgumentException if the file describes a directory
* @throws IllegalStateException if write is unsuccessful
*/
public static void copy(InputStream in, FileManager manager) {
try {
OutputStream out = new FileOutputStream(manager.getFile());
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.close();
in.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ public enum DefaultColor implements CustomColor {

protected final CharSequence start;
protected final CharSequence end;
protected String context;

DefaultColor(CharSequence start, CharSequence end) {
this.start = start;
this.end = end;
}

public GradientColor gradient(String context) {
return new GradientColor(context, name(), this.start, this.end);
public DefaultColor wrap(String context) {
this.context = context;
return this;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ public class GradientColor implements CustomColor {

private final CharSequence end;

private final String context;
private String context;

private String name;

public GradientColor(CharSequence start, CharSequence end) {
this.start = start;
this.end = end;
}

public GradientColor(String context, CharSequence start, CharSequence end) {
this.context = context;
this.start = start;
Expand All @@ -36,40 +41,59 @@ public TextColor[] colors() {
}

/**
* @param name
* @return
* Provide a name for this color.
*
* @param name The name of the color.
* @return The same gradient color object.
*/
public GradientColor name(String name) {
this.name = name;
return this;
}

/**
* @return
* Provide context to wrap.
*
* @param context The context to wrap.
* @return The same gradient color object.
*/
public GradientColor context(String context) {
this.context = context;
return this;
}

/**
* @return The name of the color
*/
@Override
public String name() {
return name;
}

/**
* @return
* Gets the un-translated formatted context.
*
* @return The gradient formatted context un-translated.
*/
@Override
public String join() {
return "<" + start + ">" + this.context + "</" + end + ">";
}

/**
* @return
* Translate the colorized context.
*
* @return The translated gradient formatted context as a base component array.
*/
@Override
public BaseComponent[] build() {
return TextComponent.fromLegacyText(translate());
}

/**
* @return
* Translate the colorized context.
*
* @return The translated gradient formatted context.
*/
@Override
public String translate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class AnvilMenu {
protected ItemBuilder RIGHT_ITEM;
protected AnvilCloseEvent event;
private boolean visible;
private boolean empty;


public AnvilMenu(Player holder, String title, ItemBuilder left, ItemBuilder right) {
Expand Down Expand Up @@ -62,6 +63,22 @@ public AnvilMenu setViewer(Player player) {
return this;
}

/**
* Apply normal running logic for this anvil (Behaves like a normal anvil).
*
* @return The same AnvilMenu.
*/
public AnvilMenu isEmpty() {
this.empty = true;
return this;
}

/**
* Apply running logic to the closing event of this menu.
*
* @param event The expression to run
* @return The same AnvilMenu.
*/
public AnvilMenu applyClosingLogic(AnvilCloseEvent event) {
this.event = event;
return this;
Expand Down Expand Up @@ -186,7 +203,9 @@ private class AnvilListener implements Listener {
@EventHandler
public void onInventoryClick(InventoryClickEvent e) {
if (e.getInventory().equals(inventory)) {
e.setCancelled(true);
if (!empty) {
e.setCancelled(true);
}
final Player clicker = (Player) e.getWhoClicked();
if (e.getRawSlot() == Slot.OUTPUT.get() || e.getRawSlot() == Slot.OUTPUT.get()) {
final ItemStack clicked = inventory.getItem(e.getRawSlot());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@
*/
public abstract class SharedMenu implements Listener {

private final Plugin plugin;
private final String id;
private final HUID huid;
private static final Map<String, SharedMenu> MENU_MAP = new HashMap<>();
private static final Map<HUID, Listener> LISTENER_MAP = new HashMap<>();
private static final Map<HUID, Inventory> INVENTORY_MAP = new HashMap<>();
private static final Map<UUID, Inventory> PLAYER_MAP = new HashMap<>();
private final LinkedList<Option> MENU_OPTIONS = new LinkedList<>();
private final Map<ItemStack, SharedProcess> PROCESS_MAP = new HashMap<>();
protected final Plugin plugin;
protected final String id;
protected final HUID huid;
protected static final Map<String, SharedMenu> MENU_MAP = new HashMap<>();
protected static final Map<HUID, Listener> LISTENER_MAP = new HashMap<>();
protected static final Map<HUID, Inventory> INVENTORY_MAP = new HashMap<>();
protected static final Map<UUID, Inventory> PLAYER_MAP = new HashMap<>();
protected final LinkedList<Option> MENU_OPTIONS = new LinkedList<>();
protected final Map<ItemStack, SharedProcess> PROCESS_MAP = new HashMap<>();

protected SharedMenu(Plugin plugin, String id) {
this.plugin = plugin;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.sanctum.labyrinth.library;

import com.github.sanctum.labyrinth.formatting.string.ColoredString;
import com.github.sanctum.labyrinth.formatting.string.CustomColor;
import com.github.sanctum.labyrinth.formatting.string.GradientColor;
import com.github.sanctum.labyrinth.formatting.string.RandomID;
import java.util.ArrayList;
Expand Down Expand Up @@ -61,7 +62,19 @@ public String generateID(int size) {
* @param to The ending Hex code.
* @return A custom color gradient using the provided context.
*/
public GradientColor gradient(CharSequence from, CharSequence to) {
public CustomColor gradient(CharSequence from, CharSequence to) {
return new GradientColor(this.context, from, to);
}

/**
* Form a custom gradient to wrap the provided context with.
* Then decide whether or not to translate it or get the raw joined string back.
*
* @param from The starting Hex code.
* @param to The ending Hex code.
* @return A custom color gradient using the provided context.
*/
public GradientColor modifyableGradient(CharSequence from, CharSequence to) {
return new GradientColor(this.context, from, to);
}

Expand Down

0 comments on commit bd5b1a8

Please sign in to comment.