MongoDB Error - Can't get Player Data - mongodb

I'm working on a Framework for a Minecraft Server and I'm running into a constant error while retrieving player data from a MongoDB database. I have both a proxy plugin and a spigot plugin that the framework is shaded into.
For the proxy plugin, I can get and store the player data in my PlayerCache (A map that assigns UUIDs to PlayerData objects), but however, for the spigot plugin, I cannot. It returns a null pointer, saying that the data is not found in the cache, even though I am sure I called the cacheData method.
I believe what is going on is that I'm not allowing enough time for the database to get the information and cache it.
Spigot code:
#EventHandler
public void onLogin(PlayerLoginEvent event)
{
PlayerCache.getInstance().cachePlayer(event.getPlayer().getUniqueId());
}
#EventHandler
public void onJoin(PlayerJoinEvent event)
{
event.setJoinMessage(null);
event.getPlayer().setDisplayName(PlayerCache.getInstance().getCachedRank(
event.getPlayer().getUniqueId()
).getColor() + event.getPlayer().getName()); //Throws a null pointer.
}
PlayerCache:
public void cachePlayer(UUID uuid)
{
PlayerData PD = PlayerManager.getInstance().getPlayerData(uuid);
this.cache.put(uuid, PD);
}
public PlayerRank getCachedRank(UUID uuid)
{
return this.cache.get(uuid).getRank();
}
Hope someone can help.

Related

invalid large object descriptor : 0 hibernate and postgres

We have a n tier application where we are reading the BLOB objects stored in the postgres database.
At times when we are trying to access the blob object through input stream we get "org.postgresql.util.PSQLException: ERROR: invalid large-object descriptor: 0" after reading the other blogs, this exception comes whenever we are trying to access the BLOB outside the transaction (transaction is committed).
But, in our case we get this exception even though the transaction is active. The BLOB is read within the transaction.
Any pointers as to why this exception is occuring even though the transaction is active?
Your description of the problem does not have specifics but in my code this error showed up when I tried to use Large Object outside the data access method. As in your case, the object was formed in the method. This is consistent with what other people noticed in this forum: Large Object exists only within the data access method (or transaction). I needed byte[] so I converted Large Object within the method, wrapped it up in a Data Transfer Object and was able to use it in other layers. This are relevant code snippets:
//This is Data Access Class
#Named
public class SupportDocsDAO {
protected ResultSet resultSet;
private LargeObject lob;
// SupportDocs is an Entity class in Data Transfer Objects package
private SupportDocs supportDocsDTO;
public LargeObject getLob() {
return lob;
}
public void setLob(LargeObject lob) {
this.lob = lob;
}
public SupportDocs getSupportDocsDTO() {
return supportDocsDTO;
}
public void setSupportDocsDTO(SupportDocs supportDocsDTO) {
this.supportDocsDTO = supportDocsDTO;
}
//.... other code
public SupportDocs fetchSupportDocForDescr(SupportDocs supportDocs1) {
Session session = HibernateUtil.getSessionFactory().openSession();
session.doWork(new Work() {
#Override
public void execute(java.sql.Connection connection) throws SQLException {
java.sql.PreparedStatement ps = null;
try {
LargeObjectManager lobm =
connection.unwrap(org.postgresql.PGConnection.class).getLargeObjectAPI();
ps = connection.prepareCall("{call ret_lo_supportdocs_id(?)}");
ps.setInt(1, supportDocs1.getSuppDocId());
ps.execute();
resultSet = ps.getResultSet();
while (resultSet.next()) {
supportDocsDTO.setFileNameDoc(resultSet.getString("filenamedoc"));
supportDocsDTO.setExtensionSd(resultSet.getString("extensionsd"));
long oid = resultSet.getLong("suppdoc_oid");
setLob(lobm.open(oid, LargeObjectManager.READ));
//This is the conversion of Large Object into byte[]
supportDocsDTO.setSuppDocImage(lob.read(lob.size()));
System.out.println("object size: " + lob.size());
}
// other code, catch, cleanup with finally, and return supportDocsDTO
This works without problems. I can recreate images and videos from obtained byte[].

Bukkit How to change an int in the config file then be able to change it again without reloading (Custom config file class.))

Okay so I am making a custom feature for my OP-Prison server, one of the things that I need to do is get an integer from the players.yml file, check if it is >= one, if it is take away one, save it and then if it is still above one then they can repeat the action untill it's 0.
The issue comes with the fact that I have to restart the server for the file to change, and even when I do, it will only go down by one integer at a time, before having to reload it again.
GUI Creation code:
Main main = Main.getPlugin(Main.class);
#SuppressWarnings("unused")
private FileControl fc;
#SuppressWarnings("unused")
private FileControl playerfc;
public static String inventoryname = Utils.chat(Main.pl.getFileControl().getConfig().getString("Backpacks.White.InventoryName"));
public List<Player> WhiteOpened = new ArrayList<>();
public static Inventory whiteBackpack(Player player) {
Inventory whiteBackpack = Bukkit.createInventory(null, 27, (inventoryname));
UUID uuid = player.getUniqueId();
whiteBackpack.setItem(10,
new ItemCreator(Material.INK_SACK).setData(8)
.setDisplayname(Utils.chat("&fCommon Packages &8» &f&l" + Main.pl.getPlayerFile().getConfig().getInt("Users." + uuid + ".Packages.Common")))
.getItem());
return whiteBackpack;
}
Code for updating the config + item when the Commonpackage is clicked:
#EventHandler
public void whiteBackpackInteract(InventoryClickEvent event) {
Player player = (Player) event.getWhoClicked();
UUID uuid = player.getUniqueId();
ItemStack clicked = event.getCurrentItem();
String title = event.getInventory().getName();
if (title.equals(inventoryname)) {
// Making it so that the item cannot be moved
event.setCancelled(true);
if (clicked != null) {
if (event.getSlot() == 10) {
// Getting the user's common packages section in the config and checking if it is greater than or equal to 1.
if (Main.pl.getPlayerFile().getConfig().getInt("Users." + uuid + ".Packages.Common") >= 1) {
// Saving the user's common package section to 'currentCommon'
Integer currentCommon = Main.pl.getPlayerFile().getConfig().getInt("Users." + uuid + ".Packages.Common");
// Taking away one from 'currentCommon' and saving it to 'newCommon'
Integer newCommon = currentCommon - 1;
// Getting the 'players.yml' file
File file = new File(main.getDataFolder(), "players.yml");
FileConfiguration config = YamlConfiguration.loadConfiguration(file);
// Checking if the current common keys is greater than or equal to 1
if (currentCommon >= 1) {
try {
//Now, Here's where the error lies.
//Gets the player's common package count and sets it to the 'newCommon' count
config.set("Users." + uuid + ".Packages.Common", newCommon);
//Saves the players.yml file
config.save(file);
} catch (IOException e) {
e.printStackTrace();
}
// Updates the inventory they're currently in (Atleast it's meant to...)
player.updateInventory();
// Sends them a message (This is just for testing purposes, making sure it's working.)
player.sendMessage(Utils.chat("&8(&9Vexil&8) &fCommon Package"));
}
}
}
}
}
}
If there is any other code that you need, just ask I'll happily provide it for you.
Right now, you need to restart the server for it to save the data to the file. This should not happen, since you are calling the method config.save(file). The following is simply speculation, but it's the only cause that I think can easily explain what is going on.
In the object that is returned by getPlayerFile().getConfig(), there is likely a variable that stores a FileConfiguration object. That variable houses all the data from the players.yml file. In your whiteBackpackInteract() method, you load the data all over again. You then continue on to write to this NEW FileConfiguration variable, rather than the one that is stored in getPlayerfile().getConfig(). Since you then proceed to save to the file directly, the variables stored in the getPlayerfile().getConfig() is never told that you changed some values around. To fix this, you need to change the following:
config.set("Users." + uuid + ".Packages.Common", newCommon);
config.save(file);
to this:
Main.pl.getPlayerFile().getConfig().set("Users." + uuid + ".Packages.Common", newCommon);
Main.pl.getPlayerFile().getConfig().save(file);
and then delete this line of code:
FileConfiguration config = YamlConfiguration.loadConfiguration(file);
This should solve your problem entirely. If it does not, I would recommend not using your friend's custom config API and instead just use the ones that are built in. Using third party code that you don't properly understand can very often lead to problems such as this.
The following are not the bugs, but are suggestions to help improve your code:
You should be sure to put your comments ABOVE or to the RIGHT over the code they describe. People read from top to bottom, so the comments (before I made the suggested edit to your post) were all below the code they describe.
Typically, you want to try to make sure that if code doesn't need to be run, it isn't. Since the int newCommon is not used until inside that if statement, you should move it in there.
You are using Main.getPlugin();
Now while that doesn't seem like such a bad thing, your getting an unassigned variable, I have no idea how it is working but you're assigning Main to Main. There are 2 proper ways to actually get the main class.
The first, and generally best way, is to use dependency injection.
So basically,
public class Main extends JavaPlugin {
#Override
public void onEnable() {
BackpackListener listener new Backpacklistener(this);
getServer().getPluginManager().registerEvents(listener, this);
}
}
public class BackpackListener implements Listener {
private Main instance;
private BackpackUtil util;
public BackpackListener(Main instance) {
this.instance = instance;
util = new BackpackUtil();
}
#EventHandler
public void onClick(InventoryClickEvent event) {
//code
util.whiteBackpack(instance);
}
public class BackpackUtil {
public Inventory whiteBackpack(Main instance) {
FileConfiguration config = instance.getConfig();
//Do things
instance.saveConfig();
}
}
The next way you can do it is less optimal, and frowned upon, but still an easier option.
public class Main() {
public static Main instance;
#Override
public void onEnable() {
instance = this;
}
}
public class ConfigHelper() {
Main instance = Main.instance;
FileConfiguration config = instance.getConfig();
//Do things
instance.saveConfig();
}
It's good to get out of the habit of using the second method (It's called a singleton), because normally the main class will change, or have multiple instances, etc... but with Spigot there can only be one main instance and one thread.

Keeping Unity networking code separate from game logic

I want to keep network code separate from my game logic. Not only do I need to do that to be able to share game logic between single and multiplayer game modes, I also want it because of the Separation Of Concerns thing.
My current approach is to generate the code for my network related classes in such a way that there is an online and an offline version. I do this using T4 templates.
The resulting classes look like this:
Standalone/Singleplayer version:
// T4 GENERATED CODE
// Head (Singleplayer version)
class StandaloneHelloWorld : MonoBehaviour, IHelloWorld
{
private string name;
public void SayHello()
{
SayHelloInternal();
}
// Body
public string Name
{
get { return name; }
set { name = value; }
}
void SayHelloInternal()
{
Debug.Log(Name + ": Hello World");
}
}
Multiplayer version:
// T4 GENERATED CODE
// Head (Multiplayer version)
class NetworkedHelloWorld : NetworkBehaviour, IHelloWorld
{
[SyncVar]
private string name;
public void SayHello()
{
CmdSayHello();
}
[Command]
void CmdSayHello()
{
RpcSayHello();
}
[ClientRpc]
void RpcSayHello()
{
SayHelloInternal();
}
// Body
public string Name
{
get { return name; }
set { name = value; }
}
void SayHelloInternal()
{
Debug.Log(Name + ": Hello World");
}
}
They both share an interface to hide the implementation from the callers:
interface IHelloWorld
{
string Name { get; set; }
void SayHello();
}
So as you can see, both implementations use the same body, sharing most of the code, while the entry points depend on the implementation being networked or not. Also note that the two implementations inherit different base classes.
Advantages:
Singleplayer code has no dependencies towards networked code and vice versa
No duplicate code (none that has to be maintained manually at least)
Disadvantages:
Support for interfaces in Unity is limited. I would not be able to reference scene instances of IHelloWorld from inside the Editor.
Having to maintain separate Prefabs for singleplayer/multiplayer game modes
Having to meddle with T4/code generation
Do you know of better ways to deal with this? How did you solve this problem?
You could structure the code in an event-based fashion. This will allow systems to register to events they're interested in. This naturally separates the logic from the network code.
As an example, let's say you want to fire a projectile.
You can fire it by calling:
new Event(EventType.FireProjectile, pos, dir, template)
You can then register systems that are interested in this event:
CollisionSystem.Register(EventType.FireProjectile, (e) => {
CollisionSystem.AddCollider(e.template.bounds);
});
AudioSystem.Register(EventType.FireProjectile, (e) => {
AudioSystem.PlaySound("Woosh");
});
AISystem.Register(EventType.FireProjectile, (e) => {
AISystem.AlertAtPosition(e.pos);
});
What's cool is next you can register this event to the NetworkSystem that will serialize it, move it across the net, deserialize it, and fire it off on the client's machine. So as far as the client is concerned this event was called locally.
NetworkSystem.Register(EventType.FireProjectile, (e) => {
NetworkSystem.Broadcast(e, Channel.Reliable);
});
This is pretty great, except that you'll soon realize that this will cause an infinite loop of events. As you send a FireProjectile event to the other client, they catch it and fire it. Instantly their NetworkSystem catches it and fires it over the net.
To fix this you need two events for every action – a request: FireProjectile, and response: ProjectileFired.
I've worked with a codebase like this for a personal project a while ago. It's in C++, but if you're interested you can read more here. Notice how the server and the client are registering to certain events, which they will forward across.

Is there a way to use Solr's streaming API with spring data solr?

I have a use case where I need to fetch the ids of my entire solr collection. For that, with solrj, I use the Streaming API like this :
CloudSolrServer server = new CloudSolrServer("zkHost1:2181,zkHost2:2181,zkHost3:2181");
SolrQuery query = new SolrQuery("*:*");
server.queryAndStreamResponse(tmpQuery, handler);
Where handler is a class that implements StreamingResponseCallback, ommited in my code for brevity.
Now, the Spring data repositories abstraction give me the ability to search by pages, by cursors, but I can't seem to find a way to handle the streaming use case.
Is there a workaround ?
SolrTemplate allows to access the underlying SolrClient in a callback style. So you could use that one to work around the current limitations.
The result conversion using the MappingSolrConverter available via the SolrTemplate is broken at the moment (I need to check why) - but you get the idea of how to do it.
solrTemplate.execute(new SolrCallback<Void>() {
#Override
public Void doInSolr(SolrClient solrClient) throws SolrServerException, IOException {
SolrQuery sq = new SolrQuery("*:*");
solrClient.queryAndStreamResponse("collection1", sq, new StreamingResponseCallback() {
#Override
public void streamSolrDocument(SolrDocument doc) {
// the bean conversion fails atm
// ExampleSolrBean bean = solrTemplate.getConverter().read(ExampleSolrBean.class, doc);
System.out.println(doc);
}
#Override
public void streamDocListInfo(long numFound, long start, Float maxScore) {
// do something useful
}
});
return null;
}
});

Enyim Memcached client doesn't work with expiration parameter passed

when using Enyim memcached client to store data with a expire timespan, i find that it doesn't work.
Could anyone help?
In my test code, I store the date with 10 minutes expiration time, and i try to get it from cache immediately but got a null object.
Enyim.Caching.MemcachedClient client = new Enyim.Caching.MemcachedClient();
client.Store(Enyim.Caching.Memcached.StoreMode.Set, "one", 1, new TimeSpan(0, 10, 0));
object obj;
client.TryGet("one", out obj); // obj == null
Assert.AreNotEqual(null, obj); // this will fail
I'm not sure if you're being vague w/ your example or if you really are using an object type but I had the same problem using custom objects. Integers, strings, etc. would work fine but my custom object was always NULL as soon as I put it in the cache. Turns out I didn't have the Serializable attribute on my object. Once I put that on there everything worked as expected.
Hyacinthus is pretty precise on his question, the answers are somewhat irrelevant. I am having the same problem ONLY when setting an expiration, either as a Timespan or as a DateTime.
There is also an issue for this at Enyim's github https://github.com/enyim/EnyimMemcached/issues/110
We have a tweaked version at my company's codebase, but it is outdated. I will try to locate the fix and get back to this, and send a pull request to Enyim, when I find the time.
EDIT:
I also found this on github.
I can confirm that this was not happening with other build of the memcached server. I think it's a bug with this particular build:
Build with this issue:
http://www.urielkatz.com/archive/detail/memcached-64-bit-windows
Build WITHOUT this issue:
http://blog.elijaa.org/index.php?post/2010/08/25/Memcached-1.4.5-for-Windows
Care to check the server version you are using?
My initial comment still stands, as I run tests using the two different dlls, and the tweaked one works while the one shipped with CouchBase fails
Please check your server.
1.Use MemCacheD Manager
Or
2.Use telnet 127.0.0.1 11211(change to your sever setting)
and type:stats
It would show your the stats.
See the "time" item stat.
It's second format you must convert it,
Simply you can compare with "1262517674",
If smaller than "1262517674",your memcached server it too old.
Please Upgrade Or change your memcached version.
Otherwise Just change your memcached version
The answer is that memcached(windows) 1.4.4 64-bit version has this bug. If it fits your needs you can try 1.4.4 32-bit version for windows, or you can try to find another 64-bit compiled version. This problem also took my whole day and I finally installed "1.4.4 32-bit version" and viola, everything works perfect with 1.4.4. 32-bit.
Create a memcached client object is a costly operation, so try to create it on start of the application and reuse that object.
This how I initiate MemcachedClient Object and access Membese Build using Enyim client.
public class MemcachedManager
{
protected static MemcachedClient mc = null;
public MemcachedManager()
{
if (mc == null) initMemCached();
}
private void initMemCached()
{
MemcachedClientConfiguration config = new MemcachedClientConfiguration();
string ipstr = "192.168.1.1;192.168.1.2"; // List of Memcaced nodes
string[] ips = ipstr.Split(';');
foreach (string ip in ips)
{
config.Servers.Add(new IPEndPoint(IPAddress.Parse(ip), 11211));
}
config.Protocol = MemcachedProtocol.Binary;
mc = new MemcachedClient(config);
}
public void setWithTimeOut(string key, string value, int minutes)
{
mc.Store(Enyim.Caching.Memcached.StoreMode.Set, key, value, TimeSpan.FromMinutes(minutes));
}
public string getString(string key)
{
return (string)mc.Get(key);
}
public void setString(string key, string value)
{
mc.Store(Enyim.Caching.Memcached.StoreMode.Set, key, value);
}
}
you need to get a sington first.
eg:
using System;
using Enyim.Caching;
using Enyim.Caching.Memcached;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UnitTestProject1
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
MemcachedClient client = MemcachedManager.Instance;//new MemcachedClient();
client.Store(StoreMode.Set, "b", "bb", new TimeSpan(1, 1, 300));
var x = client.Get("b");
client.Store(StoreMode.Set, "a", "aa");
var y = client.Get("a");
client.Store(StoreMode.Set, "c", "cc", DateTime.Now.AddSeconds(300));
var z = client.Get("c");
client.Store(StoreMode.Set, "c", "ccc");
var t = client.Get("c");
Console.WriteLine("{0},{1},{2}",x,y,z);
}
}
public static class MemcachedManager
{
private readonly static MemcachedClient _instance;
static MemcachedManager()
{
_instance = new MemcachedClient();
}
public static MemcachedClient Instance { get { return _instance; } }
}
}