001/* 002 * Copyright 2021 Siroshun09 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.github.siroshun09.mcmessage.loader; 018 019import com.github.siroshun09.configapi.common.FileConfiguration; 020import com.github.siroshun09.mcmessage.MessageHoldable; 021import com.github.siroshun09.mcmessage.message.KeyedMessage; 022import net.kyori.adventure.translation.TranslationRegistry; 023import org.jetbrains.annotations.NotNull; 024import org.jetbrains.annotations.Nullable; 025import org.jetbrains.annotations.Unmodifiable; 026 027import java.io.IOException; 028import java.nio.file.Path; 029import java.text.MessageFormat; 030import java.util.Locale; 031import java.util.Map; 032import java.util.Objects; 033import java.util.Set; 034 035public interface MessageLoader extends MessageHoldable { 036 037 static @NotNull MessageLoader fromProperties(@NotNull Path path) { 038 return new PropertiesLoader(path); 039 } 040 041 static @NotNull MessageLoader fromFileConfiguration(@NotNull FileConfiguration config) { 042 return new FileConfigurationLoader(config); 043 } 044 045 boolean existsFile(); 046 047 @NotNull @Unmodifiable Set<DuplicateKeyMessage> load() throws IOException; 048 049 void save(@NotNull Iterable<? extends KeyedMessage> messages) throws IOException; 050 051 @Nullable Locale getLocale(); 052 053 @NotNull Map<String, MessageFormat> toMessageFormatMap(); 054 055 default boolean registerToRegistry(@NotNull TranslationRegistry registry) { 056 var locale = getLocale(); 057 058 if (locale == null) { 059 return false; 060 } 061 062 registry.registerAll(locale, toMessageFormatMap()); 063 return true; 064 } 065 066 final class DuplicateKeyMessage { 067 068 private final String key; 069 private final String message; 070 071 protected DuplicateKeyMessage(@NotNull String key, @NotNull String message) { 072 this.key = Objects.requireNonNull(key); 073 this.message = Objects.requireNonNull(message); 074 } 075 076 public @NotNull String getKey() { 077 return key; 078 } 079 080 public @NotNull String getMessage() { 081 return message; 082 } 083 084 @Override 085 public boolean equals(Object o) { 086 if (this == o) return true; 087 if (o == null || getClass() != o.getClass()) return false; 088 DuplicateKeyMessage that = (DuplicateKeyMessage) o; 089 return key.equals(that.key) && message.equals(that.message); 090 } 091 092 @Override 093 public int hashCode() { 094 return Objects.hash(key, message); 095 } 096 097 @Override 098 public String toString() { 099 return "DuplicateKeyMessage{" + 100 "key='" + key + '\'' + 101 ", message='" + message + '\'' + 102 '}'; 103 } 104 } 105}