-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cfd19c5
commit b75a933
Showing
44 changed files
with
526 additions
and
125 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
26 changes: 26 additions & 0 deletions
26
src/main/java/de/eldoria/jacksonbukkit/deserializer/BukkitColorDeserializer.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,26 @@ | ||
/* | ||
* SPDX-License-Identifier: MIT | ||
* | ||
* Copyright (C) EldoriaRPG Team and Contributor | ||
*/ | ||
package de.eldoria.jacksonbukkit.deserializer; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import de.eldoria.jacksonbukkit.entities.BukkitColorWrapper; | ||
import de.eldoria.jacksonbukkit.entities.PaperColorWrapper; | ||
import org.bukkit.Color; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Class for deserialization of {@link Color} as object. | ||
*/ | ||
public class BukkitColorDeserializer extends JsonDeserializer<Color> { | ||
|
||
@Override | ||
public Color deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { | ||
return ctxt.readValue(p, BukkitColorWrapper.class).toBukkitColor(); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/de/eldoria/jacksonbukkit/deserializer/HexBukkitColorDeserializer.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,26 @@ | ||
/* | ||
* SPDX-License-Identifier: MIT | ||
* | ||
* Copyright (C) EldoriaRPG Team and Contributor | ||
*/ | ||
package de.eldoria.jacksonbukkit.deserializer; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import de.eldoria.jacksonbukkit.entities.BukkitColorWrapper; | ||
import de.eldoria.jacksonbukkit.entities.PaperColorWrapper; | ||
import org.bukkit.Color; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Class for deserialization of {@link Color} as hexadecimal string. | ||
*/ | ||
public class HexBukkitColorDeserializer extends JsonDeserializer<Color> { | ||
|
||
@Override | ||
public Color deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { | ||
return BukkitColorWrapper.of(ctxt.readValue(p, String.class)).toBukkitColor(); | ||
} | ||
} |
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
57 changes: 57 additions & 0 deletions
57
src/main/java/de/eldoria/jacksonbukkit/entities/BukkitColorWrapper.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,57 @@ | ||
/* | ||
* SPDX-License-Identifier: MIT | ||
* | ||
* Copyright (C) EldoriaRPG Team and Contributor | ||
*/ | ||
package de.eldoria.jacksonbukkit.entities; | ||
|
||
import org.bukkit.Color; | ||
|
||
/** | ||
* Class for wrapping a {@link Color}. | ||
* <p> | ||
* It also allows transformation to hex code and parsing of hex code. | ||
*/ | ||
public record BukkitColorWrapper(int red, int green, int blue) { | ||
|
||
/** | ||
* Create a new {@link BukkitColorWrapper} based on a {@link Color}. | ||
* | ||
* @param color color instance | ||
* @return new {@link BukkitColorWrapper} instance | ||
*/ | ||
public static BukkitColorWrapper of(Color color) { | ||
return new BukkitColorWrapper(color.getRed(), color.getGreen(), color.getBlue()); | ||
} | ||
|
||
/** | ||
* Create a new {@link BukkitColorWrapper} based on a hex string with RGBA or RGB. | ||
* | ||
* @param hex color as hex string | ||
* @return new {@link BukkitColorWrapper} instance | ||
*/ | ||
public static BukkitColorWrapper of(String hex) { | ||
int red = Integer.parseInt(hex.substring(0, 2), 16); | ||
int green = Integer.parseInt(hex.substring(2, 4), 16); | ||
int blue = Integer.parseInt(hex.substring(4, 6), 16); | ||
return new BukkitColorWrapper(red, green, blue); | ||
} | ||
|
||
/** | ||
* Get the string as hex string with RGBA. | ||
* | ||
* @return hex color as RGBA | ||
*/ | ||
public String asHex() { | ||
return "%02X%02X%02X".formatted(red, green, blue); | ||
} | ||
|
||
/** | ||
* Constructs a new color instance based on wrapper values. | ||
* | ||
* @return new color instance | ||
*/ | ||
public Color toBukkitColor() { | ||
return Color.fromRGB(red, green, blue); | ||
} | ||
} |
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
25 changes: 25 additions & 0 deletions
25
src/main/java/de/eldoria/jacksonbukkit/serializer/BukkitColorSerializer.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,25 @@ | ||
/* | ||
* SPDX-License-Identifier: MIT | ||
* | ||
* Copyright (C) EldoriaRPG Team and Contributor | ||
*/ | ||
package de.eldoria.jacksonbukkit.serializer; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.JsonSerializer; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import de.eldoria.jacksonbukkit.entities.BukkitColorWrapper; | ||
import de.eldoria.jacksonbukkit.entities.PaperColorWrapper; | ||
import org.bukkit.Color; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Class for serialization of {@link Color}. | ||
*/ | ||
public class BukkitColorSerializer extends JsonSerializer<Color> { | ||
@Override | ||
public void serialize(Color value, JsonGenerator gen, SerializerProvider serializers) throws IOException { | ||
gen.writeObject(BukkitColorWrapper.of(value)); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/de/eldoria/jacksonbukkit/serializer/HexBukkitColorSerializer.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,25 @@ | ||
/* | ||
* SPDX-License-Identifier: MIT | ||
* | ||
* Copyright (C) EldoriaRPG Team and Contributor | ||
*/ | ||
package de.eldoria.jacksonbukkit.serializer; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.JsonSerializer; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import de.eldoria.jacksonbukkit.entities.BukkitColorWrapper; | ||
import de.eldoria.jacksonbukkit.entities.PaperColorWrapper; | ||
import org.bukkit.Color; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Class for serialization of {@link Color}. | ||
*/ | ||
public class HexBukkitColorSerializer extends JsonSerializer<Color> { | ||
@Override | ||
public void serialize(Color value, JsonGenerator gen, SerializerProvider serializers) throws IOException { | ||
gen.writeString(BukkitColorWrapper.of(value).asHex()); | ||
} | ||
} |
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
Oops, something went wrong.