Refactoring challenge selection and updating.

This commit is contained in:
Thanos paravantis 2016-01-31 16:57:54 +02:00
parent 0e1a14a0c3
commit 1f22883c13
4 changed files with 251 additions and 184 deletions

View File

@ -1 +1 @@
[[{"location":"C:\\@Thanos\\Documents\\Minecraft\\Mineplex\\projects\\Mineplex\\Plugins\\Nautilus.Game.Arcade","type":"PROJECT","hints":{"PROJECT_NAME":"Nautilus.Game.Arcade"}},"ABSENT"],[{"location":"C:\\Program Files\\Java\\jre1.8.0_51","type":"JRE","hints":{}},"jre:jre:1.8.0"],[{"location":"C:\\@Thanos\\Documents\\Minecraft\\Mineplex\\projects\\Mineplex\\Plugins\\Libraries\\craftbukkit.jar","type":"JAR","hints":{}},"ABSENT"]]
[[{"location":"C:\\@Thanos\\Documents\\Minecraft\\Mineplex\\projects\\Mineplex\\Plugins\\Libraries\\craftbukkit.jar","type":"JAR","hints":{}},"ABSENT"],[{"location":"C:\\@Thanos\\Documents\\Minecraft\\Mineplex\\projects\\Mineplex\\Plugins\\Nautilus.Game.Arcade","type":"PROJECT","hints":{"PROJECT_NAME":"Nautilus.Game.Arcade"}},"ABSENT"],[{"location":"C:\\@Thanos\\Documents\\Minecraft\\Mineplex\\projects\\Mineplex\\Plugins\\Mineplex.Core.Common","type":"PROJECT","hints":{"PROJECT_NAME":"Mineplex.Core.Common"}},"ABSENT"]]

View File

@ -75,8 +75,8 @@ org.eclipse.jdt.core.formatter.comment.new_lines_at_block_boundaries=true
org.eclipse.jdt.core.formatter.comment.new_lines_at_javadoc_boundaries=true
org.eclipse.jdt.core.formatter.comment.preserve_white_space_between_code_and_line_comments=false
org.eclipse.jdt.core.formatter.compact_else_if=true
org.eclipse.jdt.core.formatter.continuation_indentation=2
org.eclipse.jdt.core.formatter.continuation_indentation_for_array_initializer=2
org.eclipse.jdt.core.formatter.continuation_indentation=1
org.eclipse.jdt.core.formatter.continuation_indentation_for_array_initializer=1
org.eclipse.jdt.core.formatter.disabling_tag=@formatter\:off
org.eclipse.jdt.core.formatter.enabling_tag=@formatter\:on
org.eclipse.jdt.core.formatter.format_guardian_clause_on_one_line=false

View File

@ -145,7 +145,7 @@ public abstract class Challenge implements Listener
_places = (int) Math.ceil(getChallengers().size() / 2D);
}
public boolean finish()
public boolean canFinish()
{
boolean maxTimeReached = UtilTime.elapsed(_startTime, _duration);
ArrayList<Player> players = getChallengers();

View File

@ -1,5 +1,6 @@
package nautilus.game.arcade.game.games.mineware;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@ -130,7 +131,7 @@ public class MineWare extends TeamGame implements IThrown
private HashMap<Player, Integer> _lives = new HashMap<Player, Integer>();
private boolean _crumbling = false;
private boolean _waitingChallenge = true;
private boolean _waiting = true;
private boolean _messagesSent = false;
private boolean _sendingMessages = false;
private boolean _challengeStarted = false;
@ -247,14 +248,11 @@ public class MineWare extends TeamGame implements IThrown
public Challenge selectChallenge()
{
for (int i = 0; i < _challenges.size() * 4; i++)
{
try
{
if (_challengesCopy.isEmpty())
_challengesCopy.addAll(_challenges);
Challenge challenge = UtilMath.randomElement(_challengesCopy).getConstructor(MineWare.class)
.newInstance(this);
Challenge challenge = getChallengeInstance();
if (getChallengers().size() >= challenge.getMinPlayers())
{
@ -273,17 +271,26 @@ public class MineWare extends TeamGame implements IThrown
continue;
}
}
catch (Exception e)
return null;
}
private Challenge getChallengeInstance()
{
try
{
return UtilMath.randomElement(_challengesCopy).getConstructor(MineWare.class).newInstance(this);
}
catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException
| SecurityException e)
{
System.out.println("There was a problem loading a new challenge.");
System.out.println("Error: " + e.getMessage());
}
}
return null;
}
@SuppressWarnings("deprecation")
@EventHandler
public void onChallengeUpdate(UpdateEvent event)
{
@ -305,50 +312,20 @@ public class MineWare extends TeamGame implements IThrown
if (!UtilTime.elapsed(_challengeTime, 1500))
return;
for (Player player : GetPlayers(false))
if (_lives.containsKey(player) && _lives.get(player) > 0)
Manager.Clear(player);
resetDeadPlayers();
_challenge = selectChallenge();
if (_challenge == null)
{
UtilServer.broadcast(F.main(GetName(),
"No suitable challenge, " + _challengesCopy.size() + " challenges remaining."));
keepSearchingIfNull(_challenge);
_challengesCopy.clear();
_challengesCopy.addAll(_challenges);
_challenge = selectChallenge();
if (_challenge == null)
{
SetState(GameState.Dead);
return;
}
}
for (Block block : _lastOrderBlocks)
{
if (block.getState() instanceof InventoryHolder)
{
InventoryHolder holder = (InventoryHolder) block.getState();
holder.getInventory().clear();
}
block.setTypeIdAndData(Material.AIR.getId(), (byte) 0, false);
}
// if (_currentCrumble == true)
resetLastChallengeMap();
_crumbling = false;
_deathEffect.removeSpawnedEntities();
_challenge.generateRoom();
ArrayList<Location> spawns = _challenge.getSpawns();
_playersTeam.SetSpawns(_challenge.getSpawns());
SpectatorSpawn = UtilWorld.averageLocation(spawns).add(0, 7, 0);
ArrayList<Location> toTeleport = new ArrayList<Location>();
ArrayList<Location> spawns = setupChallengeSpawnLocations();
ArrayList<Location> selectedSpawnpoints = new ArrayList<Location>();
for (int i = 0; i < spawns.size(); i++)
{
@ -357,12 +334,12 @@ public class MineWare extends TeamGame implements IThrown
for (Location spawn : spawns)
{
if (toTeleport.contains(spawn))
if (selectedSpawnpoints.contains(spawn))
continue;
double dist = 0;
for (Location loc : toTeleport)
for (Location loc : selectedSpawnpoints)
dist += loc.distance(spawn);
if (furthest == null || furthestDist < dist)
@ -372,28 +349,11 @@ public class MineWare extends TeamGame implements IThrown
}
}
toTeleport.add(furthest);
selectedSpawnpoints.add(furthest);
}
int i = 0;
for (Player player : getChallengers())
{
player.teleport(toTeleport.get(i++));
if (i >= toTeleport.size())
i = 0;
}
for (Player player : GetPlayers(false))
if (!IsAlive(player))
{
if (_chickenTeam != null)
if (_chickenTeam.HasPlayer(player))
continue;
player.teleport(SpectatorSpawn);
}
teleportPlayersToSpawnpoints(selectedSpawnpoints);
teleportSpectatorsToSpawn();
if (_challenge instanceof ChallengeSeperateRooms)
{
@ -402,46 +362,24 @@ public class MineWare extends TeamGame implements IThrown
}
_challengeTime = System.currentTimeMillis();
_waitingChallenge = true;
_waiting = true;
}
else if (_waitingChallenge)
else if (_waiting)
{
if (!UtilTime.elapsed(_challengeTime, 1000))
return;
if (!_sendingMessages && !_messagesSent)
{
for (Player player : GetPlayers(true))
{
player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, Integer.MAX_VALUE, 1));
player.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, Integer.MAX_VALUE, 1));
}
startChallengeDescriptionThread();
Thread newThread = new Thread(new Runnable()
{
@Override
public void run()
{
showChallengeDescription(_challenge);
// return;
}
});
else if (_messagesSent)
{
removeEffectsFromPlayers();
newThread.start();
return;
}
if (_messagesSent)
{
for (Player player : GetPlayers(true))
{
if (player.hasPotionEffect(PotionEffectType.BLINDNESS))
{
player.removePotionEffect(PotionEffectType.BLINDNESS);
player.removePotionEffect(PotionEffectType.NIGHT_VISION);
}
}
_waitingChallenge = false;
_waiting = false;
_challengeStarted = true;
_challenge.start();
@ -453,11 +391,11 @@ public class MineWare extends TeamGame implements IThrown
}
else
{
if (_challenge.finish())
if (_challenge.canFinish())
{
if (_challenge.canCrumble() && !_challenge.getModifiedBlocks().isEmpty())
if (canStartCrumbling())
{
if (!_crumbling && _challenge.getLost().size() >= _challenge.getCurrentPlaying().size() / 2)
if (justStartedCrumbling())
{
_crumbling = true;
@ -468,21 +406,142 @@ public class MineWare extends TeamGame implements IThrown
UtilPlayer.message(player,
F.main(GetName(), "Challenge ends when there's only 1 player alive."));
}
else
{
if ((_challenge.getCurrentPlaying().size() - _challenge.getLost().size()) <= 1 && _crumbling)
else if (canEndChallengeFromCrumble())
{
endCurrentChallenge();
_crumbling = false;
}
}
}
else
{
endCurrentChallenge();
}
}
else
{
updateChallengeTimer();
}
}
}
private void resetDeadPlayers()
{
for (Player player : GetPlayers(false))
if (_lives.containsKey(player) && _lives.get(player) > 0)
Manager.Clear(player);
}
@SuppressWarnings("deprecation")
private void resetLastChallengeMap()
{
for (Block block : _lastOrderBlocks)
{
if (block.getState() instanceof InventoryHolder)
{
InventoryHolder holder = (InventoryHolder) block.getState();
holder.getInventory().clear();
}
block.setTypeIdAndData(Material.AIR.getId(), (byte) 0, false);
}
}
private ArrayList<Location> setupChallengeSpawnLocations()
{
ArrayList<Location> spawns = _challenge.getSpawns();
_playersTeam.SetSpawns(spawns);
SpectatorSpawn = UtilWorld.averageLocation(spawns).add(0, 7.0D, 0);
return spawns;
}
private void teleportSpectatorsToSpawn()
{
for (Player player : GetPlayers(false))
{
if (!IsAlive(player))
{
if (_chickenTeam != null)
if (_chickenTeam.HasPlayer(player))
continue;
player.teleport(SpectatorSpawn);
}
}
}
private void keepSearchingIfNull(Challenge challenge)
{
if (_challenge == null)
{
UtilServer.broadcast(F.main(GetName(), "No suitable challenge was found."));
_challengesCopy.clear();
_challengesCopy.addAll(_challenges);
_challenge = selectChallenge();
if (_challenge == null)
{
SetState(GameState.Dead);
return;
}
}
}
private void teleportPlayersToSpawnpoints(ArrayList<Location> spawnpoints)
{
int i = 0;
for (Player player : getChallengers())
{
player.teleport(spawnpoints.get(i++));
if (i >= spawnpoints.size())
i = 0;
}
}
private void startChallengeDescriptionThread()
{
Thread thread = new Thread(new Runnable()
{
@Override
public void run()
{
showChallengeDescription(_challenge);
}
});
thread.start();
}
private void removeEffectsFromPlayers()
{
for (Player player : GetPlayers(true))
{
if (player.hasPotionEffect(PotionEffectType.BLINDNESS) && player.hasPotionEffect(PotionEffectType.NIGHT_VISION))
{
player.removePotionEffect(PotionEffectType.BLINDNESS);
player.removePotionEffect(PotionEffectType.NIGHT_VISION);
}
}
}
private boolean canStartCrumbling()
{
return _challenge.canCrumble() && !_challenge.getModifiedBlocks().isEmpty();
}
private boolean justStartedCrumbling()
{
return !_crumbling && _challenge.getLost().size() >= _challenge.getCurrentPlaying().size() / 2;
}
private boolean canEndChallengeFromCrumble()
{
return (_challenge.getCurrentPlaying().size() - _challenge.getLost().size()) <= 1 && _crumbling;
}
private void updateChallengeTimer()
{
for (Player player : UtilServer.getPlayers())
{
@ -501,13 +560,27 @@ public class MineWare extends TeamGame implements IThrown
}
}
}
}
}
public void endCurrentChallenge()
{
_challengeTime = System.currentTimeMillis();
resetPlayersOnChallengeEnding();
Manager.getPluginManager().callEvent(new ChallengeEndEvent(_challenge));
HandlerList.unregisterAll(_challenge);
_challenge.end();
_challengeStarted = false;
sortLastChallengeBlocks();
_challenge = null;
EndCheck();
}
private void resetPlayersOnChallengeEnding()
{
for (Player player : getChallengers())
{
Manager.GetDisguise().undisguise(player);
@ -519,13 +592,10 @@ public class MineWare extends TeamGame implements IThrown
if (_challenge.isCompleted(player))
AddGems(player, 3, "Completed Challenge", true, true);
}
}
Manager.getPluginManager().callEvent(new ChallengeEndEvent(_challenge));
HandlerList.unregisterAll(_challenge);
_challenge.end();
_challengeStarted = false;
private void sortLastChallengeBlocks()
{
_lastOrderBlocks = new ArrayList<Block>(_challenge.getModifiedBlocks());
Collections.sort(_lastOrderBlocks, new Comparator<Block>()
@ -536,9 +606,6 @@ public class MineWare extends TeamGame implements IThrown
return new Integer(o2.getY()).compareTo(o1.getY());
}
});
_challenge = null;
EndCheck();
}
@EventHandler
@ -550,7 +617,7 @@ public class MineWare extends TeamGame implements IThrown
if (_challenge == null)
return;
if (_waitingChallenge)
if (_waiting)
return;
Manager.getPluginManager().callEvent(new ChallengeEndEvent(_challenge));
@ -775,7 +842,7 @@ public class MineWare extends TeamGame implements IThrown
if (!PrepareFreeze)
return;
if (!_waitingChallenge)
if (!_waiting)
return;
Location from = event.getFrom();
@ -1076,7 +1143,7 @@ public class MineWare extends TeamGame implements IThrown
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event)
{
if (!_waitingChallenge)
if (!_waiting)
return;
event.setCancelled(true);
@ -1097,7 +1164,7 @@ public class MineWare extends TeamGame implements IThrown
@Override
public boolean isInsideMap(Player player)
{
if (_challenge != null && !_waitingChallenge)
if (_challenge != null && !_waiting)
return _challenge.isInsideMap(player);
return true;
@ -1218,7 +1285,7 @@ public class MineWare extends TeamGame implements IThrown
public boolean isWaitingChallenge()
{
return _waitingChallenge;
return _waiting;
}
public boolean areMessagesSent()