mirror of
https://github.com/Minestom/Minestom.git
synced 2024-07-01 08:54:57 +02:00
Allow registering instance extending Instance and not necessary InstanceContainer
This commit is contained in:
parent
aea788cd64
commit
af9851c334
|
@ -674,7 +674,7 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
|
||||||
public void setInstance(Instance instance) {
|
public void setInstance(Instance instance) {
|
||||||
Check.notNull(instance, "instance cannot be null!");
|
Check.notNull(instance, "instance cannot be null!");
|
||||||
Check.stateCondition(!instance.isRegistered(),
|
Check.stateCondition(!instance.isRegistered(),
|
||||||
"Instances need to be registered with InstanceManager#createInstanceContainer or InstanceManager#createSharedInstance");
|
"Instances need to be registered with InstanceManager#createInstanceContainer or InstanceManager#registerInstance");
|
||||||
|
|
||||||
if (this.instance != null) {
|
if (this.instance != null) {
|
||||||
this.instance.removeEntity(this);
|
this.instance.removeEntity(this);
|
||||||
|
|
|
@ -17,14 +17,17 @@ public final class InstanceManager {
|
||||||
private final Set<Instance> instances = new CopyOnWriteArraySet<>();
|
private final Set<Instance> instances = new CopyOnWriteArraySet<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register an {@link InstanceContainer}
|
* Register an {@link Instance} internally
|
||||||
|
* <p>
|
||||||
|
* Note: not necessary if you created your instance using {@link #createInstanceContainer()} or {@link #createSharedInstance(InstanceContainer)}
|
||||||
|
* but only if you instantiated your instance object manually
|
||||||
*
|
*
|
||||||
* @param instanceContainer the instance to register
|
* @param instance the {@link Instance} to register
|
||||||
* @return the registered {@link InstanceContainer}
|
|
||||||
*/
|
*/
|
||||||
public InstanceContainer registerInstanceContainer(InstanceContainer instanceContainer) {
|
public void registerInstance(Instance instance) {
|
||||||
registerInstance(instanceContainer);
|
Check.stateCondition(instance instanceof SharedInstance,
|
||||||
return instanceContainer;
|
"Please use InstanceManager#registerSharedInstance to register a shared instance");
|
||||||
|
UNSAFE_registerInstance(instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -35,8 +38,9 @@ public final class InstanceManager {
|
||||||
* @return the created {@link InstanceContainer}
|
* @return the created {@link InstanceContainer}
|
||||||
*/
|
*/
|
||||||
public InstanceContainer createInstanceContainer(DimensionType dimensionType, StorageLocation storageLocation) {
|
public InstanceContainer createInstanceContainer(DimensionType dimensionType, StorageLocation storageLocation) {
|
||||||
final InstanceContainer instance = new InstanceContainer(UUID.randomUUID(), dimensionType, storageLocation);
|
final InstanceContainer instanceContainer = new InstanceContainer(UUID.randomUUID(), dimensionType, storageLocation);
|
||||||
return registerInstanceContainer(instance);
|
registerInstance(instanceContainer);
|
||||||
|
return instanceContainer;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -82,7 +86,7 @@ public final class InstanceManager {
|
||||||
Check.notNull(instanceContainer, "SharedInstance needs to have an InstanceContainer to be created!");
|
Check.notNull(instanceContainer, "SharedInstance needs to have an InstanceContainer to be created!");
|
||||||
|
|
||||||
instanceContainer.addSharedInstance(sharedInstance);
|
instanceContainer.addSharedInstance(sharedInstance);
|
||||||
registerInstance(sharedInstance);
|
UNSAFE_registerInstance(sharedInstance);
|
||||||
return sharedInstance;
|
return sharedInstance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -134,11 +138,13 @@ public final class InstanceManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register the {@link Instance} internally
|
* Register an {@link Instance} internally
|
||||||
|
* <p>
|
||||||
|
* Unsafe because it does not check if {@code instance} is a {@link SharedInstance} to verify its container
|
||||||
*
|
*
|
||||||
* @param instance the {@link Instance} to register
|
* @param instance the {@link Instance} to register
|
||||||
*/
|
*/
|
||||||
private void registerInstance(Instance instance) {
|
private void UNSAFE_registerInstance(Instance instance) {
|
||||||
instance.setRegistered(true);
|
instance.setRegistered(true);
|
||||||
this.instances.add(instance);
|
this.instances.add(instance);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user