Skip to content

Commit

Permalink
Fix saveEach;
Browse files Browse the repository at this point in the history
Migrate TimeDate / Duration Format Utils
  • Loading branch information
sakura-ryoko committed Dec 27, 2024
1 parent 8181827 commit da50055
Show file tree
Hide file tree
Showing 31 changed files with 1,322 additions and 30 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# Mod Properties
mod_id=corelib
mod_name=CoreLib
mod_version=0.2.1
mod_version=0.2.2
maven_group=com.sakuraryoko
archives_base_name=corelib

Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/sakuraryoko/corelib/api/CoreLibAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

import javax.annotation.Nonnull;

import net.minecraft.network.chat.Component;

import com.sakuraryoko.corelib.api.commands.IServerCommand;
import com.sakuraryoko.corelib.api.config.IConfigDispatch;
import com.sakuraryoko.corelib.api.events.IClientEventsDispatch;
Expand All @@ -35,6 +37,7 @@
import com.sakuraryoko.corelib.impl.events.players.PlayerEventsManager;
import com.sakuraryoko.corelib.impl.events.server.ServerEventsManager;
import com.sakuraryoko.corelib.impl.modinit.ModInitManager;
import com.sakuraryoko.corelib.impl.time.TimeTestExample;

public interface CoreLibAPI
{
Expand Down Expand Up @@ -227,4 +230,14 @@ static boolean registerPlayerEventsDispatcher(IPlayerEventsDispatch dispatch)

return false;
}

static Component getTimeDurationTest(boolean usePlaceholder)
{
return TimeTestExample.runDurationTest(usePlaceholder);
}

static Component getTimeDateTest(boolean usePlaceholder)
{
return TimeTestExample.runTimeDateTest(usePlaceholder);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public ModInitData(String modID)
this.integratedServer = false;
this.dedicatedServer = false;
this.openToLan = false;
this.iTextUtils = new BuiltinTextHandler();
this.iTextUtils = BuiltinTextHandler.getInstance();

if (this.instance.getModContainer(this.MOD_ID).isPresent())
{
Expand Down
107 changes: 107 additions & 0 deletions src/main/java/com/sakuraryoko/corelib/api/time/DurationFormat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* This file is part of the CoreLib project, licensed under the
* GNU Lesser General Public License v3.0
*
* Copyright (C) 2024 Sakura Ryoko and contributors
*
* CoreLib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CoreLib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with CoreLib. If not, see <https://www.gnu.org/licenses/>.
*/

package com.sakuraryoko.corelib.api.time;

import javax.annotation.Nullable;
import com.google.common.collect.ImmutableList;

import com.sakuraryoko.corelib.impl.time.DurationFmtType;
import com.sakuraryoko.corelib.impl.time.formatter.DurationFmt;

public enum DurationFormat
{
REGULAR ("regular", DurationFmtType.REGULAR),
PRETTY ("pretty", DurationFmtType.PRETTY),
ISO_EXTENDED ("iso_extended", DurationFmtType.ISO_EXTENDED),
FORMATTED ("formatted", DurationFmtType.FORMATTED);

public static final ImmutableList<DurationFormat> VALUES = ImmutableList.copyOf(values());

final String name;
final DurationFmtType<?> type;

DurationFormat(String name, DurationFmtType<?> type)
{
this.name = name;
this.type = type;
}

public String getName() { return this.name; }

public DurationFmtType<?> getType()
{
return this.type;
}

public @Nullable DurationFmt init()
{
return this.type.init(this);
}

public String getFormat(long duration, @Nullable String fmt)
{
DurationFmt formatter = this.init();

if (formatter != null)
{
if (duration < 1)
{
return "Invalid Duration ["+duration+"]";
}

return formatter.format(duration, fmt);
}

return "";
}

public String getFormatString()
{
DurationFmt formatter = this.init();

if (formatter != null)
{
return formatter.getFormatString();
}

return "";
}

@Nullable
public static DurationFormat fromName(String name)
{
for (DurationFormat val : VALUES)
{
if (name.compareToIgnoreCase(val.getName()) == 0)
{
return val;
}
}

return null;
}

@Override
public String toString()
{
return this.getName();
}
}
52 changes: 52 additions & 0 deletions src/main/java/com/sakuraryoko/corelib/api/time/DurationOption.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* This file is part of the CoreLib project, licensed under the
* GNU Lesser General Public License v3.0
*
* Copyright (C) 2024 Sakura Ryoko and contributors
*
* CoreLib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CoreLib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with CoreLib. If not, see <https://www.gnu.org/licenses/>.
*/

package com.sakuraryoko.corelib.api.time;

import com.sakuraryoko.corelib.api.config.IConfigOption;

public class DurationOption implements IConfigOption
{
public DurationFormat option;
public String customFormat;

public DurationOption()
{
this.defaults();
}

@Override
public void defaults()
{
this.option = DurationFormat.REGULAR;
this.customFormat = "";
}

@Override
public DurationOption copy(IConfigOption opt)
{
DurationOption opts = (DurationOption) opt;

this.option = opts.option;
this.customFormat = opts.customFormat;

return this;
}
}
52 changes: 52 additions & 0 deletions src/main/java/com/sakuraryoko/corelib/api/time/TimeDateOption.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* This file is part of the CoreLib project, licensed under the
* GNU Lesser General Public License v3.0
*
* Copyright (C) 2024 Sakura Ryoko and contributors
*
* CoreLib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CoreLib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with CoreLib. If not, see <https://www.gnu.org/licenses/>.
*/

package com.sakuraryoko.corelib.api.time;

import com.sakuraryoko.corelib.api.config.IConfigOption;

public class TimeDateOption implements IConfigOption
{
public TimeFormat option;
public String customFormat;

public TimeDateOption()
{
this.defaults();
}

@Override
public void defaults()
{
this.option = TimeFormat.REGULAR;
this.customFormat = "";
}

@Override
public TimeDateOption copy(IConfigOption opt)
{
TimeDateOption opts = (TimeDateOption) opt;

this.option = opts.option;
this.customFormat = opts.customFormat;

return this;
}
}
128 changes: 128 additions & 0 deletions src/main/java/com/sakuraryoko/corelib/api/time/TimeFormat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* This file is part of the CoreLib project, licensed under the
* GNU Lesser General Public License v3.0
*
* Copyright (C) 2024 Sakura Ryoko and contributors
*
* CoreLib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CoreLib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with CoreLib. If not, see <https://www.gnu.org/licenses/>.
*/

package com.sakuraryoko.corelib.api.time;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import com.google.common.collect.ImmutableList;

import com.sakuraryoko.corelib.impl.time.TimeFmtType;
import com.sakuraryoko.corelib.impl.time.formatter.TimeFmt;

public enum TimeFormat
{
REGULAR ("regular", TimeFmtType.REGULAR),
ISO_LOCAL ("iso_local", TimeFmtType.ISO_LOCAL),
ISO_OFFSET ("iso_offset", TimeFmtType.ISO_OFFSET),
FORMATTED ("formatted", TimeFmtType.FORMATTED),
RFC1123 ("rfc1123", TimeFmtType.RFC1123);

public static final ImmutableList<TimeFormat> VALUES = ImmutableList.copyOf(values());

final String name;
final TimeFmtType<?> type;

TimeFormat(String name, TimeFmtType<?> type)
{
this.name = name;
this.type = type;
}

public String getName() { return this.name; }

public TimeFmtType<?> getType()
{
return this.type;
}

public @Nullable TimeFmt init()
{
return this.type.init(this);
}

public String formatTo(long time, @Nullable String fmt)
{
TimeFmt formatter = this.init();

if (formatter != null)
{
return formatter.formatTo(time, fmt);
}

return "";
}

public long formatFrom(@Nonnull String formattedTime, @Nullable String fmt)
{
TimeFmt formatter = this.init();

if (formatter != null)
{
return formatter.formatFrom(formattedTime, fmt);
}

return 0L;
}

public String formatNow(@Nullable String fmt)
{
TimeFmt formatter = this.init();

if (formatter != null)
{
return formatter.formatNow(fmt);
}

return "";
}

public String getFormatString()
{
TimeFmt formatter = this.init();

if (formatter != null)
{
return formatter.getFormatString();
}

return "";
}

@Nullable
public static TimeFormat fromName(String name)
{
for (TimeFormat val : VALUES)
{
if (name.compareToIgnoreCase(val.getName()) == 0)
{
return val;
}
}

return null;
}

@Override
public String toString()
{
return this.getName();
}
}
Loading

0 comments on commit da50055

Please sign in to comment.