001/* 002 * PlotSquared, a land and world management plugin for Minecraft. 003 * Copyright (C) IntellectualSites <https://intellectualsites.com> 004 * Copyright (C) IntellectualSites team and contributors 005 * 006 * This program is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * This program is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with this program. If not, see <https://www.gnu.org/licenses/>. 018 */ 019package com.plotsquared.core.plot.flag.implementations; 020 021import com.plotsquared.core.configuration.caption.TranslatableCaption; 022import com.plotsquared.core.plot.flag.FlagParseException; 023import com.plotsquared.core.plot.flag.PlotFlag; 024import com.plotsquared.core.util.MathMan; 025import com.plotsquared.core.util.TimeUtil; 026import org.checkerframework.checker.nullness.qual.NonNull; 027 028public class KeepFlag extends PlotFlag<Object, KeepFlag> { 029 030 public static final KeepFlag KEEP_FLAG_FALSE = new KeepFlag(false); 031 032 /** 033 * Construct a new flag instance. 034 * 035 * @param value Flag value 036 */ 037 protected KeepFlag(@NonNull Object value) { 038 super(value, TranslatableCaption.of("flags.flag_category_mixed"), TranslatableCaption.of("flags.flag_description_keep")); 039 } 040 041 @Override 042 public KeepFlag parse(@NonNull String input) throws FlagParseException { 043 if (MathMan.isInteger(input)) { 044 final long value = Long.parseLong(input); 045 if (value < 0) { 046 throw new FlagParseException(this, input, TranslatableCaption.of("flags.flag_error_keep")); 047 } else { 048 return flagOf(value); 049 } 050 } 051 return switch (input.toLowerCase()) { 052 case "true" -> flagOf(true); 053 case "false" -> flagOf(false); 054 default -> flagOf(TimeUtil.timeToSec(input) * 1000 + System.currentTimeMillis()); 055 }; 056 } 057 058 @Override 059 public KeepFlag merge(@NonNull Object newValue) { 060 if (newValue.equals(true)) { 061 return flagOf(true); 062 } else if (newValue.equals(false)) { 063 if (getValue().equals(true) || getValue().equals(false)) { 064 return this; 065 } else { 066 return flagOf(newValue); 067 } 068 } else { 069 if (getValue().equals(true)) { 070 return this; 071 } else if (getValue().equals(false)) { 072 return flagOf(newValue); 073 } else { 074 long currentValue = (long) getValue(); 075 return flagOf((long) newValue + currentValue); 076 } 077 } 078 } 079 080 @Override 081 public String toString() { 082 return getValue().toString(); 083 } 084 085 @Override 086 public String getExample() { 087 return "3w 4d 2h"; 088 } 089 090 @Override 091 protected KeepFlag flagOf(@NonNull Object value) { 092 return new KeepFlag(value); 093 } 094 095}