Skip to content

Commit 82f4c90

Browse files
committed
ref game modifier
1 parent 4c0f66e commit 82f4c90

File tree

10 files changed

+309
-33
lines changed

10 files changed

+309
-33
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.github.elic0de.hungergames.animation;
2+
3+
import lombok.Getter;
4+
import lombok.NonNull;
5+
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
@Getter
10+
public abstract class Animation {
11+
12+
private final @NonNull String name;
13+
private final List<String> aliases;
14+
private final int speed;
15+
private final int pause;
16+
17+
public Animation(@NonNull String name, int speed, int pause) {
18+
this(name, speed, pause, new String[0]);
19+
}
20+
21+
public Animation(@NonNull String name, int speed, int pause, String... aliases) {
22+
this.name = name;
23+
this.speed = speed;
24+
this.pause = pause;
25+
this.aliases = Arrays.asList(aliases);
26+
}
27+
28+
protected int getCurrentStep(long step, int maxSteps) {
29+
if (maxSteps <= 0) return 0;
30+
long actualStep = step / speed;
31+
// Adapt the pause to speed.
32+
int actualPause = pause <= 0 ? 0 : pause / speed;
33+
int currentStep = (int) (actualStep % (maxSteps + actualPause));
34+
return Math.min(currentStep, maxSteps);
35+
}
36+
37+
public boolean isIdentifier(@NonNull String string) {
38+
return name.equalsIgnoreCase(string) || aliases.contains(string.toLowerCase());
39+
}
40+
41+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.github.elic0de.hungergames.animation;
2+
3+
import com.github.elic0de.hungergames.HungerGames;
4+
import com.github.elic0de.hungergames.modifier.modifiers.GameModifier;
5+
import org.bukkit.Bukkit;
6+
import org.bukkit.ChatColor;
7+
import org.bukkit.entity.Player;
8+
import org.bukkit.scheduler.BukkitRunnable;
9+
10+
import java.util.Collection;
11+
import java.util.List;
12+
13+
public class Test {
14+
15+
public static void animation(Collection<GameModifier> gameModifiers) {
16+
final List<String> frames = gameModifiers.stream().map(modifier -> modifier.getColor() + "" + modifier.getSymbol() + "," + modifier.getColor() + modifier.getName()).toList();
17+
18+
Bukkit.getOnlinePlayers().forEach(player -> {
19+
new BukkitRunnable() {
20+
int frameIndex = 0;
21+
int cycles = gameModifiers.size(); // Random cycle count between 3 and 5
22+
23+
@Override
24+
public void run() {
25+
final String frame[] = frames.get(frameIndex).split(",");
26+
final String title = frame[0];
27+
final String subtitle = frame[1];
28+
player.sendTitle(title, subtitle, 0, 20, 0);
29+
30+
frameIndex++;
31+
if (frameIndex >= frames.size()) {
32+
frameIndex = 0;
33+
cycles--;
34+
35+
if (cycles <= 0) {
36+
cancel();
37+
sendAnimatedTitle(player, new String[]{
38+
"&c[ Game Modifier ]",
39+
"&6[ Game Modifier ]",
40+
"&c[ Game Modifier ]",
41+
"&6[ Game Modifier ]",
42+
"&c[ Game Modifier ]",
43+
"&6[ Game Modifier ]",
44+
"&c[ Game Modifier ]",
45+
"&6[ Game Modifier ]",
46+
"&c[ Game Modifier ]",
47+
"&6[ Game Modifier ]",
48+
"&c[ Game Modifier ]",
49+
"&6[ Game Modifier ]",
50+
"&c[ Game Modifier ]",
51+
"&c[ Game Modifier ]",
52+
"&6[ Game Modifier ]",
53+
"&c[ Game Modifier ]",
54+
"&6[ Game Modifier ]",
55+
"&c[ Game Modifier ]",
56+
"&6[ Game Modifier ]",
57+
"&c[ Game Modifier ]",
58+
"&6[ Game Modifier ]",
59+
"&c[ Game Modifier ]",
60+
"&6[ Game Modifier ]",
61+
"&c[ Game Modifier ]",
62+
"&6[ Game Modifier ]",
63+
"&c[ Game Modifier ]",
64+
"&6[ Game Modifier ]",
65+
"&c[ Game Modifier ]",
66+
"&6[Game Modifier]",
67+
}, 3);
68+
}
69+
}
70+
}
71+
}.runTaskTimer(HungerGames.getInstance(), 0L, 5L);
72+
});
73+
}
74+
75+
private static void sendAnimatedTitle(Player player, String[] frames, int delay) {
76+
new BukkitRunnable() {
77+
int frame = 0;
78+
79+
@Override
80+
public void run() {
81+
player.sendTitle(ChatColor.translateAlternateColorCodes('&', frames[frame]), "A game modifier is being picked!", 0, 70, 0);
82+
//player.playSound(player.getLocation(), Sound.BLOCK_PISTON_EXTEND, 1f, 1f);
83+
frame++;
84+
85+
if (frame == frames.length) {
86+
cancel();
87+
88+
}
89+
}
90+
}.runTaskTimer(HungerGames.getInstance(), 0L, delay);
91+
}
92+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.github.elic0de.hungergames.animation;
2+
3+
import lombok.NonNull;
4+
5+
public abstract class TextAnimation extends Animation {
6+
7+
public TextAnimation(@NonNull String name, int speed, int pause) {
8+
super(name, speed, pause);
9+
}
10+
11+
public TextAnimation(@NonNull String name, int speed, int pause, String... aliases) {
12+
super(name, speed, pause, aliases);
13+
}
14+
15+
public abstract String animate(@NonNull String string, long step, String... args);
16+
17+
}

src/main/java/com/github/elic0de/hungergames/command/HungerCommand.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import co.aikar.commands.BaseCommand;
44
import co.aikar.commands.annotation.CommandAlias;
5+
import co.aikar.commands.annotation.Default;
56
import co.aikar.commands.annotation.Subcommand;
67
import com.github.elic0de.hungergames.HungerGames;
78
import com.github.elic0de.hungergames.game.HungerGame;
@@ -14,7 +15,12 @@ public class HungerCommand extends BaseCommand {
1415

1516
@Subcommand("start")
1617
private void start(Player player) {
17-
game.startGame(player);
18+
game.startGame(player, false);
19+
}
20+
21+
@Subcommand("start modifier")
22+
private void startModifier(Player player) {
23+
game.startGame(player, true);
1824
}
1925

2026
@Subcommand("end")
@@ -29,8 +35,13 @@ private void random(Player player) {
2935
}
3036

3137
@Subcommand("team create")
32-
private void create(Player player, int count) {
38+
private void create(Player player, @Default("3") int count) {
3339
game.createTeams(count);
3440
player.sendMessage("オンラインプレイヤー数に応じて "+ count + "プレイヤーのチームを作ります");
3541
}
42+
43+
@Subcommand("teleport|tp")
44+
private void teleport(Player player) {
45+
game.teleportStartLocation(player);
46+
}
3647
}

src/main/java/com/github/elic0de/hungergames/game/GameModifier.java

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/main/java/com/github/elic0de/hungergames/game/HungerGame.java

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.github.elic0de.hungergames.dragon.DragonTrait;
1010
import com.github.elic0de.hungergames.game.phase.InGamePhase;
1111
import com.github.elic0de.hungergames.game.phase.WaitingPhase;
12+
import com.github.elic0de.hungergames.modifier.ModifierManager;
1213
import com.github.elic0de.hungergames.user.GameUser;
1314
import com.github.elic0de.hungergames.user.GameUserManager;
1415
import de.themoep.minedown.MineDown;
@@ -36,6 +37,9 @@ public class HungerGame extends AbstractGame {
3637
@Getter
3738
private final DeathChest deathChest;
3839

40+
@Getter
41+
private final ModifierManager modifierManager;
42+
3943
@Getter
4044
private final GameBossBar bossBar = new GameBossBar();
4145

@@ -52,26 +56,14 @@ public class HungerGame extends AbstractGame {
5256
private DragonTrait dragonTrait;
5357

5458
private GameRecords records;
55-
5659
private BukkitTask borderTask;
5760

58-
private final List<ChatColor> colors = List.of(
59-
ChatColor.RED,
60-
ChatColor.BLUE,
61-
ChatColor.AQUA,
62-
ChatColor.YELLOW,
63-
ChatColor.GOLD,
64-
ChatColor.GREEN,
65-
ChatColor.LIGHT_PURPLE,
66-
ChatColor.WHITE,
67-
ChatColor.GRAY
68-
);
69-
7061
public HungerGame() {
7162
scoreboard = Bukkit.getScoreboardManager().getMainScoreboard();
7263
border = new GameBorder(this);
7364
deathChest = new DeathChest();
7465
records = new GameRecords(this);
66+
modifierManager = new ModifierManager(this);
7567
}
7668

7769
public void join(GameUser user) {
@@ -86,24 +78,17 @@ public void leave(GameUser user) {
8678

8779

8880
public void createTeams(int count) {
89-
final AtomicInteger colorIndex = new AtomicInteger();
90-
int teamSize = Math.round(getPlayers().size()/count);
81+
int teamSize = Math.max(Math.round(getPlayers().size()/count), 1);
9182

9283
// 既存のチームを削除
9384
scoreboard.getTeams().forEach(Team::unregister);
9485
// 人数に応じてチームを作る
9586
for(int i = 0; i < teamSize; i++) {
96-
if (colorIndex.get() < colors.size()) {
97-
final ChatColor color = colors.get(colorIndex.get());
98-
final String teamName = scoreboard.getTeam(color.name()) != null ? UUID.randomUUID().toString().substring(0, 6) : color.name();
99-
final Team team = scoreboard.registerNewTeam(teamName);
100-
101-
team.setColor(color);
102-
team.setOption(Team.Option.NAME_TAG_VISIBILITY, Team.OptionStatus.FOR_OTHER_TEAMS);
103-
colorIndex.incrementAndGet();
104-
continue;
105-
}
106-
colorIndex.set(0);
87+
final String teamName = scoreboard.getTeam(String.valueOf(i)) != null ? UUID.randomUUID().toString().substring(0, 6) : String.valueOf(i);
88+
final Team team = scoreboard.registerNewTeam(teamName);
89+
90+
team.setPrefix(String.format("[%s] ", i));
91+
team.setOption(Team.Option.NAME_TAG_VISIBILITY, Team.OptionStatus.FOR_OTHER_TEAMS);
10792
}
10893
}
10994

@@ -113,10 +98,19 @@ public void randomTeam() {
11398
users.forEach(user -> scoreboard.getTeams().stream().min(Comparator.comparing(Team::getSize)).ifPresent(team -> team.addEntry(user.getUsername())));
11499
}
115100

116-
public void startGame(Player player) {
101+
102+
public void teleportStartLocation(Player player) {
103+
final WorldBorder border = player.getWorld().getWorldBorder();
104+
final Location start = border.getCenter().clone().add((border.getSize() / 2) - 2, 130, (border.getSize() / 2) - 2);
105+
player.teleport(start);
106+
}
107+
108+
public void startGame(Player player, boolean modifier) {
117109
if (getPhase() instanceof WaitingPhase) {
118110
final WorldBorder border = player.getWorld().getWorldBorder();
119111
final Location start = border.getCenter().clone().add((border.getSize() / 2) - 2, 130, (border.getSize() / 2) - 2);
112+
// modifierが有効の場合、ランダムにmodifierを加える
113+
// if (modifier) modifierManager.modify();
120114

121115
getPlayers(GameUser.class).forEach(user -> {
122116
// プレイヤーが所属しているチームを生存しているチームとして登録
@@ -235,6 +229,7 @@ public void reset() {
235229
rejoinPlayers.clear();
236230
border.reset();
237231
records.removeAllRecord();
232+
modifierManager.reset();
238233
if (dragonTrait != null) dragonTrait.reset();
239234
}
240235

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.github.elic0de.hungergames.modifier;
2+
3+
import com.github.elic0de.hungergames.animation.Test;
4+
import com.github.elic0de.hungergames.game.HungerGame;
5+
import com.github.elic0de.hungergames.modifier.modifiers.GameModifier;
6+
import com.github.elic0de.hungergames.modifier.modifiers.TrueUHC;
7+
import org.jetbrains.annotations.Nullable;
8+
9+
import java.util.*;
10+
11+
public class ModifierManager {
12+
13+
14+
private final HungerGame game;
15+
private final Random random = new Random();
16+
private GameModifier currentModifier;
17+
18+
private final Map<String, GameModifier> modifiers = new HashMap<>();
19+
20+
public ModifierManager(HungerGame game) {
21+
this.game = game;
22+
registerMissions();
23+
}
24+
25+
private void registerMissions() {
26+
modifiers.put(Modifiers.GET_A_IRON_INGOT.getKey(), new TrueUHC());
27+
}
28+
29+
public void modify() {
30+
Test.animation(getModifiers());
31+
}
32+
33+
public void reset() {
34+
currentModifier = null;
35+
}
36+
37+
private void randomModifier() {
38+
getRandomModifier().ifPresent(mission -> currentModifier = mission);
39+
}
40+
41+
private GameModifier getCurrentMission() {
42+
if (currentModifier == null) {
43+
randomModifier();
44+
}
45+
return currentModifier;
46+
}
47+
48+
private Optional<GameModifier> getRandomModifier() {
49+
Optional<Modifiers> missions = Arrays.stream(Modifiers.values()).skip(random.nextInt(Modifiers.values().length)).findFirst();
50+
return missions.map(value -> getModifier(value.getKey()));
51+
}
52+
53+
@Nullable
54+
public GameModifier getModifier(String key) {
55+
return this.modifiers.get(key);
56+
}
57+
58+
public Collection<GameModifier> getModifiers() {
59+
return modifiers.values();
60+
}
61+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.github.elic0de.hungergames.modifier;
2+
3+
public enum Modifiers {
4+
GET_A_IRON_INGOT("ironIngot");
5+
6+
private final String key;
7+
8+
9+
Modifiers(String key) {
10+
this.key = key;
11+
}
12+
13+
public String getKey() {
14+
return key;
15+
}
16+
}

0 commit comments

Comments
 (0)