-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for an item or economy cost
Add config option for default permission level Update fallback head cache Bump version
- Loading branch information
1 parent
0d91f93
commit f2dd287
Showing
22 changed files
with
221 additions
and
40 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
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
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
91 changes: 91 additions & 0 deletions
91
src/main/java/us/potatoboy/headindex/config/HeadIndexConfig.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,91 @@ | ||
package us.potatoboy.headindex.config; | ||
|
||
import com.google.gson.*; | ||
import eu.pb4.common.economy.api.CommonEconomy; | ||
import eu.pb4.common.economy.api.EconomyCurrency; | ||
import net.minecraft.item.Item; | ||
import net.minecraft.registry.Registries; | ||
import net.minecraft.server.MinecraftServer; | ||
import net.minecraft.text.Text; | ||
import net.minecraft.util.Identifier; | ||
|
||
import java.io.*; | ||
import java.lang.reflect.Type; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
public class HeadIndexConfig { | ||
private static final Gson GSON = new GsonBuilder().registerTypeAdapter(Identifier.class, new IdentifierSerializer()).setPrettyPrinting().create(); | ||
|
||
public enum EconomyType { | ||
ITEM, | ||
ECONOMY, | ||
FREE | ||
} | ||
|
||
public int permissionLevel = 2; | ||
|
||
public EconomyType economyType = EconomyType.FREE; | ||
public Identifier costType = new Identifier("minecraft", "diamond"); | ||
public int costAmount = 1; | ||
|
||
public Text getCost(MinecraftServer server) { | ||
return switch (economyType) { | ||
case ITEM -> Text.empty().append(getCostItem().getName()).append(Text.of(" × " + costAmount)); | ||
case ECONOMY -> getCostCurrency(server).formatValueText(costAmount, false); | ||
case FREE -> Text.empty(); | ||
}; | ||
} | ||
|
||
public Item getCostItem() { | ||
return Registries.ITEM.get(costType); | ||
} | ||
|
||
public EconomyCurrency getCostCurrency(MinecraftServer server) { | ||
return CommonEconomy.getCurrency(server, costType); | ||
} | ||
|
||
public static HeadIndexConfig loadConfig(File file) { | ||
HeadIndexConfig config; | ||
|
||
if (file.exists() && file.isFile()) { | ||
try ( | ||
FileInputStream fileInputStream = new FileInputStream(file); | ||
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8); | ||
BufferedReader bufferedReader = new BufferedReader(inputStreamReader) | ||
) { | ||
config = GSON.fromJson(bufferedReader, HeadIndexConfig.class); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Failed to load config", e); | ||
} | ||
} else { | ||
config = new HeadIndexConfig(); | ||
} | ||
|
||
config.saveConfig(file); | ||
|
||
return config; | ||
} | ||
|
||
public void saveConfig(File config) { | ||
try ( | ||
FileOutputStream stream = new FileOutputStream(config); | ||
Writer writer = new OutputStreamWriter(stream, StandardCharsets.UTF_8) | ||
) { | ||
GSON.toJson(this, writer); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Failed to load config", e); | ||
} | ||
} | ||
|
||
public static class IdentifierSerializer implements JsonSerializer<Identifier>, JsonDeserializer<Identifier> { | ||
@Override | ||
public JsonElement serialize(Identifier src, Type typeOfSrc, JsonSerializationContext context) { | ||
return new JsonPrimitive(src.toString()); | ||
} | ||
|
||
@Override | ||
public Identifier deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { | ||
return Identifier.tryParse(json.getAsString()); | ||
} | ||
} | ||
} |
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.