- @Plugin(id = MultiExp.PLUGIN_ID,name = MultiExp.PLUGIN_NAME,authors = {"GiNYAi"})
- public class MultiExp {
- public final static String PLUGIN_ID = "multiexp";
- public final static String PLUGIN_NAME = "MultiExp";
- @Inject
- private Logger logger;
- @Inject
- @ConfigDir(sharedRoot = true)
- private Path defaultConfigDir;
- private Path configPath;
- private ConfigurationLoader<CommentedConfigurationNode> configurationLoader;
- private CommentedConfigurationNode rootNode;
- private LinkedHashMap<String,Double> scaleMap;
- private HashMap<String,String> messageMap;
- @Listener
- public void onPreLoad(GamePreInitializationEvent event){
- MinecraftForge.EVENT_BUS.register(this);
- configPath = defaultConfigDir.resolve(PLUGIN_NAME+".conf");
- try {
- if(!configPath.toFile().exists()){
- Sponge.getAssetManager().getAsset(this,"default_config.conf").get()
- .copyToFile(configPath);
- }
- reload();
- } catch (IOException e) {
- logger.error(PLUGIN_NAME+" failed to load config",e);
- }
- }
- @Listener
- public void onGameStart(GameStartingServerEvent event){
- CommandSpec reload = CommandSpec.builder()
- .permission("MultiExp.Commands.Reload")
- .executor(((src, args) -> {
- try {
- reload();
- }catch (Exception e){
- logger.error(PLUGIN_NAME+" failed to load config",e);
- throw new CommandException(Text.of("Failed to reload."),e);
- }
- src.sendMessage(Text.of("Reload complete."));
- return CommandResult.success();
- })).build();
- CommandSpec root = CommandSpec.builder()
- .permission("MultiExp.Commands.Command")
- .child(reload,"reload")
- .build();
- Sponge.getCommandManager().register(this,root,PLUGIN_NAME);
- }
- @Listener
- public void onReload(GameReloadEvent event) {
- try {
- reload();
- }catch (Exception e){
- logger.error(PLUGIN_NAME+" failed to load config",e);
- }
- }
- private void reload() throws IOException {
- configurationLoader = HoconConfigurationLoader.builder().setPath(configPath).build();
- rootNode = configurationLoader.load();
- scaleMap = new LinkedHashMap<>();
- messageMap = new HashMap<>();
- for(CommentedConfigurationNode node:rootNode.getNode("Multiples").getChildrenMap().values()){
- String permission = node.getNode("Permission").getString("MultiExp.Multiples."+node.getKey().toString());
- double scale = node.getNode("Scale").getDouble();
- String message = node.getNode("Message").getString(null);
- if(scaleMap.containsKey(permission)){
- logger.warn("Multiple "+node.getKey().toString()+" use the permission:"+permission+" has been used by other multiple");
- }else {
- scaleMap.put(permission,scale);
- messageMap.put(permission,message);
- }
- }
- }
- @SubscribeEvent
- public void onGainExp(PlayerPickupXpEvent event){
- Player player = (Player) event.getEntityPlayer();
- for(String permission: scaleMap.keySet()){
- if(player.hasPermission(permission)){
- int originExp = event.getOrb().getXpValue();
- double expExp = originExp * scaleMap.get(permission);
- int finalExp = (int) Math.round(expExp);
- event.getOrb().xpValue = finalExp;
- int gainExp = finalExp-originExp;
- String message = messageMap.get(permission);
- if(message!=null){
- message = message.replaceAll("%player%",player.getName());
- message = message.replaceAll("%origin_exp%",String.valueOf(originExp));
- message = message.replaceAll("%final_exp%",String.valueOf(finalExp));
- message = message.replaceAll("%gain_exp%",String.valueOf(gainExp));
- Text text;
- try {
- text = TextSerializers.JSON.deserialize(message);
- }catch (Exception e){
- text = TextSerializers.FORMATTING_CODE.deserializeUnchecked(message);
- }
- player.sendMessage(text);
- }
- break;
- }
- }
- }
- }
复制代码 |