Skocz do zawartości
  • 👋 Witaj na MPCForum!

    Przeglądasz forum jako gość, co oznacza, że wiele świetnych funkcji jest jeszcze przed Tobą! 😎

    • Pełny dostęp do działów i ukrytych treści
    • Możliwość pisania i odpowiadania w tematach
    • System prywatnych wiadomości
    • Zbieranie reputacji i rozwijanie swojego profilu
    • Członkostwo w jednej z największych społeczności graczy

    👉 Dołączenie zajmie Ci mniej niż minutę – a zyskasz znacznie więcej!

    Zarejestruj się teraz

FinYaml - Biblioteka od konfigów z komentarzami i domyślnymi wartościami!


GotoFinal

Rekomendowane odpowiedzi

Opublikowano

Opis

 

Biblioteka umożliwia tworzenie konfiguracji z komentarzami, domyślnymi wartościami itd itp.

Wspiera bardzo wiele typów, nawet typu Map<int[], int[]> gdzie większość onlinowych parserów się sypie.

Można dodawać własne typy ;) Potem opiszę jak.

 

Potem rozwinę ten opis, a na razię przedstawiam moją małą biblioteczkę od konfiguracji z wsparciem komentarzy, domyślnych wartości, itd.
 
Tutaj jest wiki z jakiś tam opisem w łamanym angielskim, potem się doda drugi po polsku.

Wiki:

https://github.com/GotoFinal/FinYaml/wiki'>https://github.com/GotoFinal/FinYaml/wiki

 
Biblioteka działa na bukicie i Bungee, jednak wymaga javy 8.
Jedyne inne wymagania to SnakeYaml na którym się opiera, jest on wbudowany z bukkita i bungee i w masę innych rzeczy ;)
Sama biblioteka może byc też używana bez bukkita i bungee, o ile dodamy SnakeYaml-a
 

Source code:

https://github.com/GotoFinal/FinYaml

 
I taki przykład na zachętę:
SimpleConfig class

 

package com.gotofinal.finyaml.examples.comdef;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.gotofinal.finyaml.annotations.CfgComment;
import com.gotofinal.finyaml.annotations.CfgFooterComments;
import com.gotofinal.finyaml.annotations.CfgFooterNoNewLine;
import com.gotofinal.finyaml.annotations.defaults.CfgDelegateDefault;
import com.gotofinal.finyaml.annotations.defaults.CfgIntDefault;
import com.gotofinal.finyaml.annotations.defaults.CfgStringDefault;

import org.bukkit.Material;

@SuppressWarnings("ALL")
@CfgComment("Welcome in simple config file!")
@CfgComment("You can change all needed options here!") // you can also use single CfgComments with array of strings.
@CfgFooterComments({"==========", "This is the end of file!"}) // you can use multiple CfgFooterComment or single CfgFooterComments
public class SimpleConfig
{
    @CfgComment("Message when player don't have permissions")
    @CfgStringDefault("&5You don't have permissions for that!")
    private String noPermission;

    @CfgComment("Some other message bla bla bla")
    @CfgComment("two lines, yey")
    @CfgFooterComments("Some other comment...") // you can use footer here too.
    @CfgStringDefault("&4Some other\nmulti line message\n  2 extra spaces here!\n:)")
    private String someOtherMessage;

    @CfgFooterNoNewLine // with this option footer will be printed after value without any new line.
    @CfgFooterComments("How often task should be used.")
    @CfgIntDefault(55)
    private int taskDelay;

    @CfgComment("List of blacklisted materials, players can't use them.")
    @CfgDelegateDefault("com.gotofinal.finyaml.examples.comdef.SimpleConfig#defaultBlacklistedMaterials")
    private List<Material> blacklistedMaterials;

    @CfgComment("You can add custom item names here")
    @CfgDelegateDefault("com.gotofinal.finyaml.examples.comdef.SimpleConfig#defaultItemNames")
    private Map<Material, String> itemNames;

    @CfgDelegateDefault("com.gotofinal.finyaml.examples.comdef.SimpleConfig#defaultItemDesc")
    private Map<Material, String> itemDesc;

    private static List<Material> defaultBlacklistedMaterials()
    {
        return Arrays.asList(Material.BEDROCK, Material.LAVA, Material.WATER, Material.STATIONARY_LAVA, Material.STATIONARY_WATER);
    }

    private static Map<Material, String> defaultItemNames()
    {
        Map<Material, String> names = new LinkedHashMap<>(3);
        names.put(Material.APPLE, "sweet apple");
        names.put(Material.COOKIE, "magic cookie");
        names.put(Material.ARROW, "deadly arrow");
        return names;
    }

    private static Map<Material, String> defaultItemDesc()
    {
        Map<Material, String> descs = new LinkedHashMap<>(3);
        descs.put(Material.APPLE, "this sweet apple will\ngive you 10 cats.");
        descs.put(Material.COOKIE, "don't eat it!\n  It will kill you!\nTrust me, I'm a dolphin.");
        descs.put(Material.ARROW, "Use me to kill your friends!");
        return descs;
    }

    public String getNoPermission()
    {
        return this.noPermission;
    }

    public void setNoPermission(final String noPermission)
    {
        this.noPermission = noPermission;
    }

    public String getSomeOtherMessage()
    {
        return this.someOtherMessage;
    }

    public void setSomeOtherMessage(final String someOtherMessage)
    {
        this.someOtherMessage = someOtherMessage;
    }

    public int getTaskDelay()
    {
        return this.taskDelay;
    }

    public void setTaskDelay(final int taskDelay)
    {
        this.taskDelay = taskDelay;
    }

    public List<Material> getBlacklistedMaterials()
    {
        return this.blacklistedMaterials;
    }

    public void setBlacklistedMaterials(final List<Material> blacklistedMaterials)
    {
        this.blacklistedMaterials = blacklistedMaterials;
    }

    public Map<Material, String> getItemNames()
    {
        return this.itemNames;
    }

    public void setItemNames(final Map<Material, String> itemNames)
    {
        this.itemNames = itemNames;
    }

    public Map<Material, String> getItemDesc()
    {
        return this.itemDesc;
    }

    public void setItemDesc(final Map<Material, String> itemDesc)
    {
        this.itemDesc = itemDesc;
    }

    @Override
    public boolean equals(final Object o)
    {
        if (this == o)
        {
            return true;
        }
        if (! (o instanceof SimpleConfig))
        {
            return false;
        }

        final SimpleConfig that = (SimpleConfig) o;

        if (this.taskDelay != that.taskDelay)
        {
            return false;
        }
        if (this.noPermission != null ? ! this.noPermission.equals(that.noPermission) : that.noPermission != null)
        {
            return false;
        }
        if (this.someOtherMessage != null ? ! this.someOtherMessage.equals(that.someOtherMessage) : that.someOtherMessage != null)
        {
            return false;
        }
        if (this.blacklistedMaterials != null ? ! this.blacklistedMaterials.equals(that.blacklistedMaterials) : that.blacklistedMaterials != null)
        {
            return false;
        }
        if (this.itemNames != null ? ! this.itemNames.equals(that.itemNames) : that.itemNames != null)
        {
            return false;
        }
        return ! (this.itemDesc != null ? ! this.itemDesc.equals(that.itemDesc) : that.itemDesc != null);

    }

    @Override
    public int hashCode()
    {
        int result = this.noPermission != null ? this.noPermission.hashCode() : 0;
        result = 31 * result + (this.someOtherMessage != null ? this.someOtherMessage.hashCode() : 0);
        result = 31 * result + this.taskDelay;
        result = 31 * result + (this.blacklistedMaterials != null ? this.blacklistedMaterials.hashCode() : 0);
        result = 31 * result + (this.itemNames != null ? this.itemNames.hashCode() : 0);
        result = 31 * result + (this.itemDesc != null ? this.itemDesc.hashCode() : 0);
        return result;
    }

    @Override
    public String toString()
    {
        return "SimpleConfig{" + "noPermission='" + noPermission + '\'' + ", someOtherMessage='" + someOtherMessage + '\'' + ", taskDelay=" + taskDelay + ", blacklistedMaterials=" + blacklistedMaterials + ", itemNames=" + itemNames + ", itemDesc=" + itemDesc + '}';
    }
} 

 


Kod testowy:

Template<SimpleConfig> template = FinYaml.getTemplate(SimpleConfig.class);
        SimpleConfig cfg = template.fillDefaults(new SimpleConfig());

        // basic test
        {
            String data = template.dumpAsString(cfg);
            System.out.println(data); // pierwszy print
            SimpleConfig loaded = template.load(data);
            if (! loaded.equals(cfg)) {throw new AsserionError();}
        }

        // default tests
        {
            SimpleConfig loaded = template.load("noPermission: \"&5You don't have permissions for that!\"\n" +
                                  "\n" +
                                  "# Some other message bla bla bla\n" +
                                  "# two lines, yey\n" +
                                  "someOtherMessage: |2-\n" +
                                  "  &4Some other\n" +
                                  "  multi line message\n" +
                                  "    2 extra spaces here!\n" +
                                  "  :)");
            System.out.println(loaded); // print 2
            if (! loaded.equals(cfg)) {throw new AsserionError();}
        }

widok z konosli:

 

# Welcome in simple config file!
# You can change all needed options here!

# Message when player don't have permissions
noPermission: "&5You don't have permissions for that!"

# Some other message bla bla bla
# two lines, yey
someOtherMessage: |2-
  &4Some other
  multi line message
    2 extra spaces here!
  
# Some other comment...

taskDelay: 55 # How often task should be used.

# List of blacklisted materials, players can't use them.
blacklistedMaterials: 
- BEDROCK
- LAVA
- WATER
- STATIONARY_LAVA
- STATIONARY_WATER

# You can add custom item names here
itemNames: 
  APPLE: sweet apple
  COOKIE: magic cookie
  ARROW: deadly arrow

itemDesc: 
  APPLE: |2-
    this sweet apple will
    give you 10 cats.
  COOKIE: |2-
    don't eat it!
      It will kill you!
    Trust me, I'm a dolphin.
  ARROW: Use me to kill your friends!

# ==========
# This is the end of file! 
SimpleConfig{noPermission='&5You don't have permissions for that!', someOtherMessage='&4Some other
multi line message
  2 extra spaces here!
:)', taskDelay=55, blacklistedMaterials=[BEDROCK, LAVA, WATER, STATIONARY_LAVA, STATIONARY_WATER], itemNames={APPLE=sweet apple, COOKIE=magic cookie, ARROW=deadly arrow}, itemDesc={APPLE=this sweet apple will
give you 10 cats., COOKIE=don't eat it!
  It will kill you!
Trust me, I'm a dolphin., ARROW=Use me to kill your friends!}} 

 


Oczywiście można ładować z plików, lub zapisywać do plików, więcej ciekawych metod tutaj:
https://github.com/GotoFinal/FinYaml/blob/master/src/main/java/com/gotofinal/finyaml/FinYaml.java
https://github.com/GotoFinal/FinYaml/blob/master/src/main/java/com/gotofinal/finyaml/Template.java
 

Download:

http://www.spigotmc.org/resources/finyaml.8160/

 
 
Zezwalam na dodawanie załączenie biblioteki do twojego pluginu, ale jeśli plugin jest publiczny, dodaj informacje o tym że używasz FinYaml i podlinkuj githuba lub temat na spigocie (temat powstanie dziś wieczorem)
 

 

Mpc to gówno i nawet na dotacje nie pozwala...

1438614356923701010629.png

 

Opublikowano

Fajna bilbioteka, ale nie użyje jej bo wymaga javy 8 ;c

będzie wersja na jave 7, ale będzie gorsza, bo to będzie tylko proste podmienienie lambd klasami.

Ale to jak wrzuce na spigota, tam dam download wtedy, po 2 wersje.

 

@GotoFinal

 

Ladnie zrobione ! :D

 

Nad tym tyle kombinowales? :>

 

Zrob wersje copy paste do plg :D

nie długo, kilka dni łącznie, bo przerwy były.

1-2 dni robiłem zapis, potem miałem długą przerwę z powodu braku pomysłu jak zrobić wczytywanie za pomocą SnakeYaml, tak by dodać wsparcie do wszystkich funkcji. Ale potem znalazłem i znowu 1-2 dni i zrobione.

+ jakieś poprawki póżniejze

 

pobierz soie soruce i wklej

1438614356923701010629.png

 

Opublikowano

ref

Poprawniłem download, teraz jest już ładnie ze spigota ;) 

+ male poprawki

Fajna bilbioteka, ale nie użyje jej bo wymaga javy 8 ;c

meh, jednak nie dam pod jave 7, java 7 jest zbyt głupia by ogarnac duzą częśc opcji, i nie da rady ich ot-tak dodać nie psując wielu rzeczy.

 

Zacznijcie uzywać aktualnej wersji :P

1438614356923701010629.png

 

Opublikowano

Mamy dostęp do API Preferences i map własności, czy jest sens pchać wszystkie ustawienia domyślne do klasy. Klasa Properties ma konstruktor z domyślnymi wartosciami. Pomysł ciekawy, ale raczej mało trafiony. Ale mimo wszystko życze powodzenia.

Opublikowano

Mamy dostęp do API Preferences i map własności, czy jest sens pchać wszystkie ustawienia domyślne do klasy. Klasa Properties ma konstruktor z domyślnymi wartosciami. Pomysł ciekawy, ale raczej mało trafiony. Ale mimo wszystko życze powodzenia.

głównym plusem są tutaj komentarze, bo wartości domyślnie często można też zrobić na zasadzie

private String data = "domyslne";

 

Po 2 to używa SnakeYaml, czyli jest idealne do tworzenia konfiguracji, które można bardzo łatwo załadować i zapisać i edytować przez użytkownika, bez konieczności żadnego ręcznego ustawiania wartości.

Konfiguracje nie zawsze opiweraja się tylko na zasadzie  klucz w stringu -> wartość, często uzywa się czegoś bardziej wymagającego gdzie kluczem jest jakiś obiekt, czasem nawet lista czy tablica

1438614356923701010629.png

 

Zarchiwizowany

Ten temat przebywa obecnie w archiwum. Dodawanie nowych odpowiedzi zostało zablokowane.

×
×
  • Dodaj nową pozycję...