Skip to content

Commit

Permalink
Restore most parts of a player on game completion
Browse files Browse the repository at this point in the history
* Create PlayerRestorer to restore a player
* Inform p[layers of their death
* Add missing headers
  • Loading branch information
mattysweeps committed Mar 9, 2019
1 parent edb526e commit a958645
Show file tree
Hide file tree
Showing 14 changed files with 332 additions and 10 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pluginGroup=io.github.m0pt0pmatt.survivalgames
pluginId=survivalgames
pluginVersion=1.0.9
pluginVersion=1.0.10
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
@Plugin(
id = "survival-games",
name = "Survival Games",
version = "1.0.9",
version = "1.0.10",
description = "Survival Games for Sponge."
)
public class SurvivalGamesPlugin {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public class RootCommand extends ParentCommand {
.put(toEntry(JoinCommand.getInstance()))
.put(toEntry(LeaveCommand.getInstance()))
.put(toEntry(SpectateCommand.getInstance()))
.put(toEntry(new TestCommand()))
.build();

private static final SurvivalGamesCommand INSTANCE = new RootCommand();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.github.m0pt0pmatt.survivalgames.command.executor;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import io.github.m0pt0pmatt.survivalgames.game.PlayerRestorer;
import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.args.CommandContext;
import org.spongepowered.api.command.args.GenericArguments;
import org.spongepowered.api.data.DataContainer;
import org.spongepowered.api.data.DataQuery;
import org.spongepowered.api.data.manipulator.mutable.RepresentedPlayerData;
import org.spongepowered.api.entity.EntitySnapshot;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.text.Text;

import java.util.HashSet;
import java.util.Set;

public class TestCommand extends LeafCommand {

boolean which = false;
PlayerRestorer restorer;

protected TestCommand() {
super(RootCommand.getInstance(), "test", GenericArguments.none());
}

@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {

if (src instanceof Player) {

Player player = (Player) src;

if (which) {
src.sendMessage(Text.of("restore"));
restorer.restore(player);
} else {
src.sendMessage(Text.of("create"));
restorer = new PlayerRestorer(player);
}
which = !which;
}

return CommandResult.success();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* This file is part of SurvivalGames, licensed under the MIT License (MIT).
*
* Copyright (c) Matthew Broomfield <[email protected]>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package io.github.m0pt0pmatt.survivalgames.game;

import com.flowpowered.math.vector.Vector3d;
import org.spongepowered.api.data.key.Keys;
import org.spongepowered.api.data.value.mutable.MutableBoundedValue;
import org.spongepowered.api.data.value.mutable.Value;
import org.spongepowered.api.entity.Entity;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.entity.living.player.gamemode.GameMode;
import org.spongepowered.api.item.inventory.Inventory;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.scoreboard.Scoreboard;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;

public final class PlayerRestorer {

private final Value<Text> displayNameData;
private final List<ItemStack> enderChestInventory;
private final Value<GameMode> gameModeData;
private final Scoreboard scoreboard;

private final MutableBoundedValue<Integer> foodLevel;
private final MutableBoundedValue<Double> exhaustion;
private final MutableBoundedValue<Double> saturation;

private final List<ItemStack> inventory;

private final MutableBoundedValue<Double> health;
private final MutableBoundedValue<Double> maxHealth;

private final Vector3d headRotation;

private final Entity vehicle;
private final List<Entity> passengers;
private final Vector3d rotation;
private final Vector3d velocity;

private final Location<World> location;

public PlayerRestorer(Player player) {
displayNameData = player.getDisplayNameData().displayName().copy();
enderChestInventory = inventory(player.getEnderChestInventory());
gameModeData = player.getGameModeData().type().copy();
scoreboard = player.getScoreboard();
foodLevel = player.getFoodData().foodLevel();
exhaustion = player.getFoodData().exhaustion();
saturation = player.getFoodData().saturation();
inventory = inventory(player.getInventory());
health = player.getHealthData().health().copy();
maxHealth = player.getHealthData().maxHealth().copy();
headRotation = player.getHeadRotation().clone();
vehicle = player.getVehicle().orElse(null);
passengers = player.getPassengers();
rotation = player.getRotation().clone();
velocity = player.getVelocity().clone();
location = player.getLocation();
}

private ArrayList<ItemStack> inventory(Inventory inventory) {
ArrayList<ItemStack> items = new ArrayList<>(inventory.size());
inventory.slots().forEach(i -> items.add(i.peek().orElse(null)));
inventory.clear();
return items;
}

private void restoreInventory(Inventory inventory, List<ItemStack> items) {
inventory.clear();

Iterator<ItemStack> ii = items.iterator();
Iterator<Inventory> si = inventory.slots().iterator();

for(;ii.hasNext();) {
ItemStack item = ii.next();
Inventory slot = si.next();
if (item != null) {
slot.set(item);
}
}
}

public void restore(Player player) {

player.offer(Keys.DISPLAY_NAME, displayNameData.get());
restoreInventory(player.getEnderChestInventory(), enderChestInventory);
player.offer(Keys.GAME_MODE, gameModeData.get());

player.setScoreboard(scoreboard);

player.offer(Keys.FOOD_LEVEL, foodLevel.get());
player.offer(Keys.EXHAUSTION, exhaustion.get());
player.offer(Keys.SATURATION, saturation.get());

restoreInventory(player.getInventory(), inventory);

player.offer(Keys.HEALTH, health.get());
player.offer(Keys.MAX_HEALTH, maxHealth.get());

player.setHeadRotation(headRotation);
Optional.ofNullable(vehicle).ifPresent(player::setVehicle);
if (passengers.size() > 0) {
player.clearPassengers();
passengers.forEach(player::addPassenger);
}
player.setRotation(rotation);
player.setVelocity(velocity);
player.setLocation(location);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class SurvivalGame {
private final Set<CommandBlock> commandBlocks;
private final Set<UUID> activeMobSpawners;
private final Set<UUID> activeEventIntervals;
private final Map<UUID, EntitySnapshot> playerSnapshots;
private final Map<UUID, PlayerRestorer> playerSnapshots;

public SurvivalGame(String name, GameConfig config) {
this.name = checkNotNull(name);
Expand Down Expand Up @@ -102,7 +102,7 @@ public Set<UUID> getActiveEventIntervals() {
return activeEventIntervals;
}

public Map<UUID, EntitySnapshot> getPlayerSnapshots() {
public Map<UUID, PlayerRestorer> getPlayerSnapshots() {
return playerSnapshots;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.spongepowered.api.event.entity.DamageEntityEvent;
import org.spongepowered.api.item.inventory.Inventory;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.util.TextMessageException;
import org.spongepowered.api.world.World;

Expand Down Expand Up @@ -121,6 +122,9 @@ private static void performDeadPlayer(SurvivalGame survivalGame, Player player,
// Post the death event
Sponge.getEventManager().post(new PlayerDeathEvent(cause, survivalGame, player));

// Tell the poor player
player.sendMessage(Text.of("You were killed. Wait for the game to end."));

// Check for a win condition.
try {
WinChecker.checkWin(survivalGame);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
/*
* This file is part of SurvivalGames, licensed under the MIT License (MIT).
*
* Copyright (c) Matthew Broomfield <[email protected]>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package io.github.m0pt0pmatt.survivalgames.task;

import io.github.m0pt0pmatt.survivalgames.game.SurvivalGame;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
/*
* This file is part of SurvivalGames, licensed under the MIT License (MIT).
*
* Copyright (c) Matthew Broomfield <[email protected]>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package io.github.m0pt0pmatt.survivalgames.task.player;

import io.github.m0pt0pmatt.survivalgames.game.PlayerRestorer;
import io.github.m0pt0pmatt.survivalgames.game.SurvivalGame;
import io.github.m0pt0pmatt.survivalgames.task.Task;
import org.spongepowered.api.entity.EntitySnapshot;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.util.TextMessageException;

import java.util.Iterator;
Expand All @@ -22,11 +48,11 @@ public static Task getInstance() {

@Override
public void execute(SurvivalGame survivalGame) throws TextMessageException {
for(Iterator<Map.Entry<UUID, EntitySnapshot>> i = survivalGame.getPlayerSnapshots().entrySet().iterator(); i.hasNext();) {
Map.Entry<UUID, EntitySnapshot> entry = i.next();
for(Iterator<Map.Entry<UUID, PlayerRestorer>> i = survivalGame.getPlayerSnapshots().entrySet().iterator(); i.hasNext();) {
Map.Entry<UUID, PlayerRestorer> entry = i.next();
i.remove();

entry.getValue().restore();
Sponge.getServer().getPlayer(entry.getKey()).ifPresent(p -> entry.getValue().restore(p));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.flowpowered.math.vector.Vector3d;
import com.flowpowered.math.vector.Vector3i;
import io.github.m0pt0pmatt.survivalgames.command.CommandKeys;
import io.github.m0pt0pmatt.survivalgames.game.PlayerRestorer;
import io.github.m0pt0pmatt.survivalgames.game.SurvivalGame;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -68,7 +69,7 @@ public void execute(SurvivalGame survivalGame, Player player) throws TextMessage
private static void spawnPlayer(SurvivalGame survivalGame,
Player player, World world, Vector3d spawnPoint, Vector3d centerVector) {

survivalGame.getPlayerSnapshots().put(player.getUniqueId(), player.createSnapshot());
survivalGame.getPlayerSnapshots().put(player.getUniqueId(), new PlayerRestorer(player));

player.setLocation(world.getLocation(spawnPoint).add(new Vector3d(0.5, 0, 0.5)));
player.lookAt(centerVector);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import com.flowpowered.math.vector.Vector3d;
import io.github.m0pt0pmatt.survivalgames.command.CommandKeys;
import io.github.m0pt0pmatt.survivalgames.game.PlayerRestorer;
import io.github.m0pt0pmatt.survivalgames.game.SurvivalGame;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -68,7 +69,7 @@ public void execute(SurvivalGame survivalGame, Player player) throws TextMessage
private static void spawnPlayer(SurvivalGame survivalGame,
Player player, World world, Vector3d spawnPoint, Vector3d centerVector) {

survivalGame.getPlayerSnapshots().put(player.getUniqueId(), player.createSnapshot());
survivalGame.getPlayerSnapshots().put(player.getUniqueId(), new PlayerRestorer(player));
player.setLocation(world.getLocation(spawnPoint).add(new Vector3d(0.5, 10, 0.5)));
player.lookAt(centerVector);
player.offer(Keys.GAME_MODE, GameModes.SPECTATOR);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
/*
* This file is part of SurvivalGames, licensed under the MIT License (MIT).
*
* Copyright (c) Matthew Broomfield <[email protected]>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package io.github.m0pt0pmatt.survivalgames.thread;

public class BlankProgressable implements Progressable {
Expand Down
Loading

0 comments on commit a958645

Please sign in to comment.