Add an armour decay for offline players

This commit is contained in:
Sam 2017-05-09 16:27:56 +01:00
parent 26df99a4cf
commit 10b0f1e1ed
4 changed files with 51 additions and 6 deletions

View File

@ -18,7 +18,10 @@ import mineplex.gemhunters.spawn.SpawnModule;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.inventory.ItemStack;
import java.awt.*;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
@ReflectivelyCreateMiniPlugin
@ -61,6 +64,7 @@ public class JoinModule extends MiniPlugin
player.setMaxHealth(data.getMaxHealth());
player.setFoodLevel(data.getHunger());
player.getInventory().addItem(data.getItems());
loseDurability(data.getArmour(), data.getSaveTime());
player.getInventory().setArmorContents(data.getArmour());
_inventory.unlockSlots(player, data.getSlots(), false);
});
@ -86,4 +90,22 @@ public class JoinModule extends MiniPlugin
});
}
private void loseDurability(ItemStack[] items, long time)
{
long diff = System.currentTimeMillis() - time;
long hours = TimeUnit.MILLISECONDS.toHours(diff);
for (ItemStack item : items)
{
short max = item.getType().getMaxDurability();
short change = (short) ((max / 100D) * hours);
if (item.getDurability() + change > max * 0.15)
{
continue;
}
item.setDurability((short) (item.getDurability() + change));
}
}
}

View File

@ -16,8 +16,9 @@ public class PersistenceData
private final int _slots;
private final ItemStack[] _items;
private final ItemStack[] _armour;
private final long _saveTime;
public PersistenceData(int gems, Location location, QuestPlayerData questData, int health, int maxHealth, int hunger, int slots, ItemStack[] items, ItemStack[] armour)
public PersistenceData(int gems, Location location, QuestPlayerData questData, int health, int maxHealth, int hunger, int slots, ItemStack[] items, ItemStack[] armour, long saveTime)
{
_gems = gems;
_location = location;
@ -28,6 +29,7 @@ public class PersistenceData
_items = items;
_slots = slots;
_armour = armour;
_saveTime = saveTime;
}
public int getGems()
@ -75,4 +77,8 @@ public class PersistenceData
return _armour;
}
public long getSaveTime()
{
return _saveTime;
}
}

View File

@ -23,7 +23,9 @@ import org.bukkit.event.player.AsyncPlayerPreLoginEvent.Result;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack;
import org.jooq.util.derby.sys.Sys;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@ -92,8 +94,9 @@ public class PersistenceModule extends MiniPlugin
int slots = _inventory.getSlots(player);
ItemStack[] items = player.getInventory().getContents();
ItemStack[] armour = player.getInventory().getArmorContents();
long saveTime = System.currentTimeMillis();
PersistenceData data = new PersistenceData(gems, location, quest, health, maxHealth, hunger, slots, items, armour);
PersistenceData data = new PersistenceData(gems, location, quest, health, maxHealth, hunger, slots, items, armour, saveTime);
runAsync(() ->
{

View File

@ -6,6 +6,7 @@ import mineplex.gemhunters.quest.QuestPlayerData;
import mineplex.serverdata.database.DBPool;
import mineplex.serverdata.database.RepositoryBase;
import mineplex.serverdata.database.column.ColumnInt;
import mineplex.serverdata.database.column.ColumnTimestamp;
import mineplex.serverdata.database.column.ColumnVarChar;
import net.minecraft.server.v1_8_R3.ItemMapEmpty;
import org.bukkit.Bukkit;
@ -14,8 +15,11 @@ import org.bukkit.Material;
import org.bukkit.craftbukkit.v1_8_R3.inventory.CraftItemStack;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jooq.util.derby.sys.Sys;
import java.lang.reflect.Constructor;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@ -25,8 +29,8 @@ public class PersistenceRepository extends RepositoryBase
{
private static final String GET_DATA = "SELECT * FROM gemHunters WHERE accountId=?;";
private static final String INSERT_DATA = "INSERT INTO gemHunters VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
private static final String UPDATE_DATA = "UPDATE gemHunters SET gems=?,health=?,maxHealth=?,hunger=?,x=?,y=?,z=?,yaw=?,pitch=?,quests=?,slots=?,items=?,armour=? WHERE accountId=?;";
private static final String INSERT_DATA = "INSERT INTO gemHunters VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
private static final String UPDATE_DATA = "UPDATE gemHunters SET gems=?,health=?,maxHealth=?,hunger=?,x=?,y=?,z=?,yaw=?,pitch=?,quests=?,slots=?,items=?,armour=?,saveTime=? WHERE accountId=?;";
private static final String DELETE_DATA = "DELETE FROM gemHunters WHERE accountId=?;";
private static final Gson GSON;
private static final ItemStack AIR = new ItemStack(Material.AIR);
@ -98,10 +102,17 @@ public class PersistenceRepository extends RepositoryBase
armourList.add(CraftItemStack.deserialize(map));
}
Timestamp saveTime = resultSet.getTimestamp("saveTime");
if (saveTime == null)
{
saveTime = new Timestamp(System.currentTimeMillis());
}
_exists.add(accountId);
Location location = new Location(Bukkit.getWorlds().get(0), x, y, z, yaw, pitch);
PersistenceData data = new PersistenceData(gems, location, questData, health, maxHealth, hunger, slots, itemsList.toArray(new ItemStack[0]), armourList.toArray(new ItemStack[0]));
PersistenceData data = new PersistenceData(gems, location, questData, health, maxHealth, hunger, slots, itemsList.toArray(new ItemStack[0]), armourList.toArray(new ItemStack[0]), saveTime.getTime());
response.accept(data);
}
}, new ColumnInt("accountId", accountId));
@ -125,6 +136,7 @@ public class PersistenceRepository extends RepositoryBase
ItemStack[] armour = data.getArmour();
List<Map<String, Object>> itemsMap = new ArrayList<>(items.length);
List<Map<String, Object>> armourMap = new ArrayList<>(armour.length);
Timestamp saveTime = new Timestamp(data.getSaveTime());
for (ItemStack itemStack : items)
{
@ -162,6 +174,7 @@ public class PersistenceRepository extends RepositoryBase
new ColumnInt("slots", slots),
new ColumnVarChar("items", 10000, GSON.toJson(itemsMap)),
new ColumnVarChar("armour", 1000, GSON.toJson(armourMap)),
new ColumnTimestamp("saveTime", saveTime),
new ColumnInt("accountId", accountId)
);
}
@ -181,7 +194,8 @@ public class PersistenceRepository extends RepositoryBase
new ColumnVarChar("quests", 500, GSON.toJson(data.getQuestData())),
new ColumnInt("slots", slots),
new ColumnVarChar("items", 10000, GSON.toJson(itemsMap)),
new ColumnVarChar("armour", 1000, GSON.toJson(armourMap))
new ColumnVarChar("armour", 1000, GSON.toJson(armourMap)),
new ColumnTimestamp("saveTime", saveTime)
);
}