summaryrefslogtreecommitdiffstats
path: root/EssentialsUpdate/src/com/earth2me/essentials/update/states/Changelog.java
blob: 10a4f33c54a03905e06d96ed0b34ee9824c69ed8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package com.earth2me.essentials.update.states;

import com.earth2me.essentials.update.UpdateCheck;
import com.earth2me.essentials.update.VersionInfo;
import java.util.List;
import org.bukkit.entity.Player;


public class Changelog extends AbstractState
{
	private static final int CHANGES_PER_PAGE = 5;
	private transient int page = 0;
	private transient boolean confirmed = false;
	private transient final List<String> changes;
	private transient final int pages;

	public Changelog(final StateMap stateMap)
	{
		super(stateMap);
		changes = getChanges();
		pages = changes.size() / CHANGES_PER_PAGE + (changes.size() % CHANGES_PER_PAGE > 0 ? 1 : 0);
	}

	@Override
	public AbstractState getNextState()
	{
		return confirmed ? getState(EssentialsChat.class) : this;
	}

	@Override
	public boolean guessAnswer()
	{
		if (pages == 0)
		{
			confirmed = true;
		}
		return confirmed;
	}

	private List<String> getChanges()
	{
		final UpdateCheck updateCheck = getState(UpdateOrInstallation.class).getUpdateCheck();
		final VersionInfo versionInfo = updateCheck.getNewVersionInfo();
		return versionInfo.getChangelog();
	}

	@Override
	public void askQuestion(final Player sender)
	{
		if (pages > 1)
		{
			sender.sendMessage("Changelog, page " + page + " of " + pages + ":");
		}
		else
		{
			sender.sendMessage("Changelog:");
		}
		for (int i = page * CHANGES_PER_PAGE; i < Math.min(page * CHANGES_PER_PAGE + CHANGES_PER_PAGE, changes.size()); i++)
		{
			sender.sendMessage(changes.get(i));
		}
		if (pages > 1)
		{
			sender.sendMessage("Select a page by typing the numbers 1 to " + pages + " to view all changes and then type confirm or abort.");
		}
		else
		{
			sender.sendMessage("Type confirm to update Essentials or abort to cancel the update.");
		}
	}

	@Override
	public boolean reactOnAnswer(final String answer)
	{
		if (answer.equalsIgnoreCase("confirm"))
		{
			confirmed = true;
			return true;
		}
		if (answer.matches("[0-9]+"))
		{
			final int page = Integer.parseInt(answer);
			if (page <= pages && page > 0)
			{
				this.page = page - 1;
				return true;
			}
		}
		return false;
	}
}