Project update.
This commit is contained in:
@ -0,0 +1,170 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jmobius.gameserver.model.announce;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import com.l2jmobius.commons.database.pool.impl.ConnectionFactory;
|
||||
|
||||
/**
|
||||
* @author UnAfraid
|
||||
*/
|
||||
public class Announcement implements IAnnouncement
|
||||
{
|
||||
protected static final Logger _log = Logger.getLogger(Announcement.class.getName());
|
||||
|
||||
private static final String INSERT_QUERY = "INSERT INTO announcements (type, content, author) VALUES (?, ?, ?)";
|
||||
private static final String UPDATE_QUERY = "UPDATE announcements SET type = ?, content = ?, author = ? WHERE id = ?";
|
||||
private static final String DELETE_QUERY = "DELETE FROM announcements WHERE id = ?";
|
||||
|
||||
protected int _id;
|
||||
private AnnouncementType _type;
|
||||
private String _content;
|
||||
private String _author;
|
||||
|
||||
public Announcement(AnnouncementType type, String content, String author)
|
||||
{
|
||||
_type = type;
|
||||
_content = content;
|
||||
_author = author;
|
||||
}
|
||||
|
||||
public Announcement(ResultSet rset) throws SQLException
|
||||
{
|
||||
_id = rset.getInt("id");
|
||||
_type = AnnouncementType.findById(rset.getInt("type"));
|
||||
_content = rset.getString("content");
|
||||
_author = rset.getString("author");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId()
|
||||
{
|
||||
return _id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AnnouncementType getType()
|
||||
{
|
||||
return _type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setType(AnnouncementType type)
|
||||
{
|
||||
_type = type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getContent()
|
||||
{
|
||||
return _content;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setContent(String content)
|
||||
{
|
||||
_content = content;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAuthor()
|
||||
{
|
||||
return _author;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAuthor(String author)
|
||||
{
|
||||
_author = author;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean storeMe()
|
||||
{
|
||||
try (Connection con = ConnectionFactory.getInstance().getConnection();
|
||||
PreparedStatement ps = con.prepareStatement(INSERT_QUERY, Statement.RETURN_GENERATED_KEYS))
|
||||
{
|
||||
ps.setInt(1, _type.ordinal());
|
||||
ps.setString(2, _content);
|
||||
ps.setString(3, _author);
|
||||
ps.execute();
|
||||
try (ResultSet rset = ps.getGeneratedKeys())
|
||||
{
|
||||
if (rset.next())
|
||||
{
|
||||
_id = rset.getInt(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_log.log(Level.WARNING, getClass().getSimpleName() + ": Couldn't store announcement: ", e);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateMe()
|
||||
{
|
||||
try (Connection con = ConnectionFactory.getInstance().getConnection();
|
||||
PreparedStatement ps = con.prepareStatement(UPDATE_QUERY))
|
||||
{
|
||||
ps.setInt(1, _type.ordinal());
|
||||
ps.setString(2, _content);
|
||||
ps.setString(3, _author);
|
||||
ps.setInt(4, _id);
|
||||
ps.execute();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_log.log(Level.WARNING, getClass().getSimpleName() + ": Couldn't store announcement: ", e);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean deleteMe()
|
||||
{
|
||||
try (Connection con = ConnectionFactory.getInstance().getConnection();
|
||||
PreparedStatement ps = con.prepareStatement(DELETE_QUERY))
|
||||
{
|
||||
ps.setInt(1, _id);
|
||||
ps.execute();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_log.log(Level.WARNING, getClass().getSimpleName() + ": Couldn't remove announcement: ", e);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jmobius.gameserver.model.announce;
|
||||
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* @author UnAfraid
|
||||
*/
|
||||
public enum AnnouncementType
|
||||
{
|
||||
NORMAL,
|
||||
CRITICAL,
|
||||
EVENT,
|
||||
AUTO_NORMAL,
|
||||
AUTO_CRITICAL;
|
||||
private static final Logger _log = Logger.getLogger(AnnouncementType.class.getName());
|
||||
|
||||
public static AnnouncementType findById(int id)
|
||||
{
|
||||
for (AnnouncementType type : values())
|
||||
{
|
||||
if (type.ordinal() == id)
|
||||
{
|
||||
return type;
|
||||
}
|
||||
}
|
||||
_log.log(Level.WARNING, AnnouncementType.class.getSimpleName() + ": Unexistent id specified: " + id + "!", new IllegalStateException());
|
||||
return NORMAL;
|
||||
}
|
||||
|
||||
public static AnnouncementType findByName(String name)
|
||||
{
|
||||
for (AnnouncementType type : values())
|
||||
{
|
||||
if (type.name().equalsIgnoreCase(name))
|
||||
{
|
||||
return type;
|
||||
}
|
||||
}
|
||||
_log.log(Level.WARNING, AnnouncementType.class.getSimpleName() + ": Unexistent name specified: " + name + "!", new IllegalStateException());
|
||||
return NORMAL;
|
||||
}
|
||||
}
|
@ -0,0 +1,184 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jmobius.gameserver.model.announce;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import com.l2jmobius.Config;
|
||||
import com.l2jmobius.commons.database.pool.impl.ConnectionFactory;
|
||||
import com.l2jmobius.gameserver.ThreadPoolManager;
|
||||
import com.l2jmobius.gameserver.util.Broadcast;
|
||||
|
||||
/**
|
||||
* @author UnAfraid
|
||||
*/
|
||||
public final class AutoAnnouncement extends Announcement implements Runnable
|
||||
{
|
||||
private static final String INSERT_QUERY = "INSERT INTO announcements (`type`, `content`, `author`, `initial`, `delay`, `repeat`) VALUES (?, ?, ?, ?, ?, ?)";
|
||||
private static final String UPDATE_QUERY = "UPDATE announcements SET `type` = ?, `content` = ?, `author` = ?, `initial` = ?, `delay` = ?, `repeat` = ? WHERE id = ?";
|
||||
|
||||
private long _initial;
|
||||
private long _delay;
|
||||
private int _repeat = -1;
|
||||
private int _currentState;
|
||||
private ScheduledFuture<?> _task;
|
||||
|
||||
public AutoAnnouncement(AnnouncementType type, String content, String author, long initial, long delay, int repeat)
|
||||
{
|
||||
super(type, content, author);
|
||||
_initial = initial;
|
||||
_delay = delay;
|
||||
_repeat = repeat;
|
||||
restartMe();
|
||||
}
|
||||
|
||||
public AutoAnnouncement(ResultSet rset) throws SQLException
|
||||
{
|
||||
super(rset);
|
||||
_initial = rset.getLong("initial");
|
||||
_delay = rset.getLong("delay");
|
||||
_repeat = rset.getInt("repeat");
|
||||
restartMe();
|
||||
}
|
||||
|
||||
public long getInitial()
|
||||
{
|
||||
return _initial;
|
||||
}
|
||||
|
||||
public void setInitial(long initial)
|
||||
{
|
||||
_initial = initial;
|
||||
}
|
||||
|
||||
public long getDelay()
|
||||
{
|
||||
return _delay;
|
||||
}
|
||||
|
||||
public void setDelay(long delay)
|
||||
{
|
||||
_delay = delay;
|
||||
}
|
||||
|
||||
public int getRepeat()
|
||||
{
|
||||
return _repeat;
|
||||
}
|
||||
|
||||
public void setRepeat(int repeat)
|
||||
{
|
||||
_repeat = repeat;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean storeMe()
|
||||
{
|
||||
try (Connection con = ConnectionFactory.getInstance().getConnection();
|
||||
PreparedStatement ps = con.prepareStatement(INSERT_QUERY, Statement.RETURN_GENERATED_KEYS))
|
||||
{
|
||||
ps.setInt(1, getType().ordinal());
|
||||
ps.setString(2, getContent());
|
||||
ps.setString(3, getAuthor());
|
||||
ps.setLong(4, getInitial());
|
||||
ps.setLong(5, getDelay());
|
||||
ps.setInt(6, getRepeat());
|
||||
ps.execute();
|
||||
try (ResultSet rset = ps.getGeneratedKeys())
|
||||
{
|
||||
if (rset.next())
|
||||
{
|
||||
_id = rset.getInt(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_log.log(Level.WARNING, getClass().getSimpleName() + ": Couldn't store announcement: ", e);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateMe()
|
||||
{
|
||||
try (Connection con = ConnectionFactory.getInstance().getConnection();
|
||||
PreparedStatement ps = con.prepareStatement(UPDATE_QUERY))
|
||||
{
|
||||
ps.setInt(1, getType().ordinal());
|
||||
ps.setString(2, getContent());
|
||||
ps.setString(3, getAuthor());
|
||||
ps.setLong(4, getInitial());
|
||||
ps.setLong(5, getDelay());
|
||||
ps.setLong(6, getRepeat());
|
||||
ps.setLong(7, getId());
|
||||
ps.execute();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_log.log(Level.WARNING, getClass().getSimpleName() + ": Couldn't update announcement: ", e);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean deleteMe()
|
||||
{
|
||||
if ((_task != null) && !_task.isCancelled())
|
||||
{
|
||||
_task.cancel(false);
|
||||
}
|
||||
return super.deleteMe();
|
||||
}
|
||||
|
||||
public void restartMe()
|
||||
{
|
||||
if ((_task != null) && !_task.isCancelled())
|
||||
{
|
||||
_task.cancel(false);
|
||||
}
|
||||
_currentState = _repeat;
|
||||
_task = ThreadPoolManager.getInstance().scheduleGeneral(this, _initial);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
if ((_currentState == -1) || (_currentState > 0))
|
||||
{
|
||||
for (String content : getContent().split(Config.EOL))
|
||||
{
|
||||
Broadcast.toAllOnlinePlayers(content, (getType() == AnnouncementType.AUTO_CRITICAL));
|
||||
}
|
||||
|
||||
if (_currentState != -1)
|
||||
{
|
||||
_currentState--;
|
||||
}
|
||||
|
||||
_task = ThreadPoolManager.getInstance().scheduleGeneral(this, _delay);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,106 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jmobius.gameserver.model.announce;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import com.l2jmobius.gameserver.idfactory.IdFactory;
|
||||
import com.l2jmobius.gameserver.script.DateRange;
|
||||
|
||||
/**
|
||||
* @author UnAfraid
|
||||
*/
|
||||
public class EventAnnouncement implements IAnnouncement
|
||||
{
|
||||
private final int _id;
|
||||
private final DateRange _range;
|
||||
private String _content;
|
||||
|
||||
public EventAnnouncement(DateRange range, String content)
|
||||
{
|
||||
_id = IdFactory.getInstance().getNextId();
|
||||
_range = range;
|
||||
_content = content;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId()
|
||||
{
|
||||
return _id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AnnouncementType getType()
|
||||
{
|
||||
return AnnouncementType.EVENT;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setType(AnnouncementType type)
|
||||
{
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid()
|
||||
{
|
||||
return _range.isWithinRange(new Date());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getContent()
|
||||
{
|
||||
return _content;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setContent(String content)
|
||||
{
|
||||
_content = content;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAuthor()
|
||||
{
|
||||
return "N/A";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAuthor(String author)
|
||||
{
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean deleteMe()
|
||||
{
|
||||
IdFactory.getInstance().releaseId(_id);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean storeMe()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateMe()
|
||||
{
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
/*
|
||||
* This file is part of the L2J Mobius project.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jmobius.gameserver.model.announce;
|
||||
|
||||
import com.l2jmobius.gameserver.model.interfaces.IDeletable;
|
||||
import com.l2jmobius.gameserver.model.interfaces.IStorable;
|
||||
import com.l2jmobius.gameserver.model.interfaces.IUpdatable;
|
||||
|
||||
/**
|
||||
* @author UnAfraid
|
||||
*/
|
||||
public interface IAnnouncement extends IStorable, IUpdatable, IDeletable
|
||||
{
|
||||
public int getId();
|
||||
|
||||
public AnnouncementType getType();
|
||||
|
||||
public void setType(AnnouncementType type);
|
||||
|
||||
public boolean isValid();
|
||||
|
||||
public String getContent();
|
||||
|
||||
public void setContent(String content);
|
||||
|
||||
public String getAuthor();
|
||||
|
||||
public void setAuthor(String author);
|
||||
}
|
Reference in New Issue
Block a user