Class OrderedBlockListComparator<T>
- Type Parameters:
T- the object that represents the state of the blocks usually aBlockState, but can include Nbt Compounds depending on the case or Blocks, for example. For example,CompoundOrderedBlockListComparatoruses aPair<BlockState, NbtCompound>to connect Nbt to a BlockState
- Direct Known Subclasses:
CompoundOrderedBlockListComparator,DefaultOrderedBlockListComparator
Class to manage ordered BlockList.
For memory saving, we use a link between states and BlockPos.
- Since that
BlockStatetakes a lot of memory, we use a unique index represented by a short to make the link. - We also want the order of the BlockPos, so we can't compact the BlockPos into one BlockState easily and without having important performance losses.
- Since that it is highly improbable that there are more than 32 000 different
Tobjects, we use the short (instead of int), allowing us to save two Bytes of data per BlocPos.
public class XOrderedBlockListComparator extends OrderedBlockListComparator<BlockState> {
//constructor
//abstracts methods
//custom methods
}
To know what to put in the <> read that:-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected LinkedHashMap<net.minecraft.util.math.BlockPos, Short> we're using the short to make the link between the T objects (blockStates in the most cases) and the BlockPos.we're using BiMap to be able to get the short from the T objects and the other way around -
Constructor Summary
ConstructorsConstructorDescriptionConstructor to init an emptyOrderedBlockListComparator.OrderedBlockListComparator(Map<T, List<net.minecraft.util.math.BlockPos>> info) constructor to init aOrderedBlockListComparator.OrderedBlockListComparator(T states, List<net.minecraft.util.math.BlockPos> posList) constructor to init aOrderedBlockListComparator. -
Method Summary
Modifier and TypeMethodDescriptionbooleanMethod to know if no BlockPos are present in theposMapnet.minecraft.util.math.BlockPosgetBlockPos(int index) Retrieves the BlockPos at a specified index.Set<net.minecraft.util.math.BlockPos> getBlockPos(List<T> states) method to get all the BlockPos related to a list of statesSet<net.minecraft.util.math.BlockPos> getBlockPos(T state) Method to gate all the BlockPos related to provided stateSet<net.minecraft.util.math.BlockPos> Retrieves a set of all BlockPos entries in the position map.net.minecraft.util.math.BlockPosRetrieves the first BlockPos in the position map.net.minecraft.util.Pair<net.minecraft.util.math.BlockPos, T> Method to get the first BlockPos pair.net.minecraft.util.math.BlockPosRetrieves the last BlockPos in the position map.net.minecraft.util.Pair<net.minecraft.util.math.BlockPos, T> method to get the last BlockPos pairList<net.minecraft.util.math.BlockPos> LinkedHashMap<net.minecraft.util.math.BlockPos, Short> Method to get the posMap related to the objectnet.minecraft.util.Pair<net.minecraft.util.math.BlockPos, T> getPosPair(int index) net.minecraft.util.math.BlockPosRetrieves a random BlockPos from the position map.net.minecraft.util.math.BlockPosgetRandomBlockPos(net.minecraft.util.math.random.Random random) Retrieves a random BlockPos from the position map using a provided random generator.shortgetStateIndex(T state) method to get the index related to theTobject.getT()Retrieves a list of all states in the states map.booleanmethod to know if no blockPos are presentbooleanisPresent(net.minecraft.util.math.BlockPos pos) Method to know if a BlockPos is present in the comparatorbooleanMethod to know if an objectTis present in the comparatorbooleanmethod to know if no state is presentabstract voidplace(net.minecraft.world.StructureWorldAccess world, int index) method to place the Block related to the indexvoidplaceAll(net.minecraft.world.StructureWorldAccess worldAccess) Method to place all the blocks in the comparatorvoidplaceAllWithDeletion(net.minecraft.world.StructureWorldAccess worldAccess) Method to place all the blocks in the comparator and removing the BlockPosvoidplaceAllWithVerification(net.minecraft.world.StructureWorldAccess worldAccess) Method to place all the blocks in the comparator with the BlockPos getting verifiedvoidplaceAllWithVerificationDeletion(net.minecraft.world.StructureWorldAccess worldAccess) Method to place all the blocks in the comparator with the BlockPos getting verified and the getting deletedabstract voidplaceFirst(net.minecraft.world.StructureWorldAccess world) method to place the first Blockabstract voidplaceFirstWithDeletion(net.minecraft.world.StructureWorldAccess world) Method to place the first Block and deleting it.abstract booleanplaceFirstWithVerification(net.minecraft.world.StructureWorldAccess world) Method to place the first Block.abstract booleanplaceFirstWithVerificationDeletion(net.minecraft.world.StructureWorldAccess world) Method to place the first Block and deleting it.abstract voidplaceLast(net.minecraft.world.StructureWorldAccess world) Method to place the last Block of the comparator.abstract voidplaceLastWithDeletion(net.minecraft.world.StructureWorldAccess world) Method to place the last Block of the comparator and removing it then.abstract booleanplaceLastWithVerification(net.minecraft.world.StructureWorldAccess world) Method to place the last Block.abstract booleanplaceLastWithVerificationDeletion(net.minecraft.world.StructureWorldAccess world) Method to place the last Block of the comparator and removing it then.abstract voidplaceWithDeletion(net.minecraft.world.StructureWorldAccess world, int index) method to place the block with the deletion of the BlockPosabstract booleanplaceWithVerification(net.minecraft.world.StructureWorldAccess world, int index) Method to place the block related to the index.abstract booleanplaceWithVerificationDeletion(net.minecraft.world.StructureWorldAccess world, int index) Method to place the block with the deletion of the BlockPos The method also performs verification to know if the block can be placed.intposSize()Retrieves the number of entries in the position map.voidMethod to add aTobject as well as a list of BlockPos.voidprotected <U extends OrderedBlockListComparator<T>>
voidput(U comparator) voidMethod to add aTobject as well as a list of BlockPos.voidClears all elements from posList, posMap, and statesMap.net.minecraft.util.math.BlockPosremoveBlockPos(int index) Removes the BlockPos at the specified index from the posList and posMap.voidremoveBlockPos(List<net.minecraft.util.math.BlockPos> posList) Removes multiple BlockPos entries from the position map.voidremoveBlockPos(net.minecraft.util.math.BlockPos pos) Removes a single BlockPos entry from the position map.net.minecraft.util.Pair<net.minecraft.util.math.BlockPos, T> removeBlockPosPair(int index) Removes the BlockPos at the specified index from the posList and retrieves its associated state.net.minecraft.util.Pair<net.minecraft.util.math.BlockPos, T> Removes and returns the first BlockPos and its associated state from the posList.net.minecraft.util.math.BlockPosRemoves and returns the first BlockPos from the posList and posMap.net.minecraft.util.Pair<net.minecraft.util.math.BlockPos, T> Removes and returns the last BlockPos and its associated state from the posList.net.minecraft.util.math.BlockPosRemoves and returns the last BlockPos from the posList and posMap.voidremoveState(List<T> states) method to remove a list of object from thestateMapvoidremoveState(T state) Method to remove aTobject from thestateMap.voidreplaceState(T oldState, T newState) Replaces an old state with a new state in the states map.voidsetPosList(List<net.minecraft.util.math.BlockPos> posList) voidsetPosMap(LinkedHashMap<net.minecraft.util.math.BlockPos, Short> indexes) Method to set the posMapintmethod to get the size of the stateMap
-
Field Details
-
statesMap
we're using BiMap to be able to get the short from the T objects and the other way around -
posMap
we're using the short to make the link between the T objects (blockStates in the most cases) and the BlockPos.
-
-
Constructor Details
-
OrderedBlockListComparator
constructor to init aOrderedBlockListComparator.- Parameters:
states- theTobject that will init thestateMap.posList- the BlockPos List related to thestatesthat will init theposMap
-
OrderedBlockListComparator
constructor to init aOrderedBlockListComparator.- Parameters:
info- a map that provides a list of BlockPos and theTobjects related to them.That will be used to initialize the
posMapas well as thestateMap.
-
OrderedBlockListComparator
public OrderedBlockListComparator()Constructor to init an emptyOrderedBlockListComparator.
-
-
Method Details
-
put
Method to add aTobject as well as a list of BlockPos.- In the case there's already a
Tobject, the method will only add the BlockPos list to theposMapwith the related index.- In the other case, the
Tobject will be added to thestateMapwith a new index. And will put the blockPos List in theposMap- Parameters:
state- the object that will be compared, added and assigned an index in the case it is not presentposList- the List of BlockPos that you want to add
-
put
-
put
-
putAll
Method to add aTobject as well as a list of BlockPos.- In the case there's already a
Tobject, the method will only add the BlockPos list to theposMapwith the related index.- In the other case, the
Tobject will be added to thestateMapwith a new index. And will put the blockPos List in theposMapWe're calling the put method for all the objects in the map
- Parameters:
info- a map ofTobject linked with aList<BLockPos>
-
isPresent
Method to know if an objectTis present in the comparator- Parameters:
state- the state that will be tested to know if it is present or not- Returns:
-true if the state is present.
-false if not
-
isPresent
public boolean isPresent(net.minecraft.util.math.BlockPos pos) Method to know if a BlockPos is present in the comparator- Parameters:
pos- the state that will be tested to know if it is present or not- Returns:
-true if the pos is present.
-false if not
-
arePosEmpty
public boolean arePosEmpty()Method to know if no BlockPos are present in theposMap- Returns:
-true if no BlockPos are present in the map.
-false if at least one BlockPos is present.
-
getStateIndex
method to get the index related to theTobject.- Parameters:
state- the state that will be compared- Returns:
- The index related to the
Tif it is present.- We return -1 in case it is not present.
Be careful when using this method.
Always check that the value is not equals to -1
-
removeState
method to remove a list of object from thestateMap- Parameters:
states- the objects that will be removed
-
removeState
Method to remove a
Tobject from thestateMap.Removing that object will also remove every
BlockPosrelated to that object- Parameters:
state- the object that will be removed form thestateMap
-
replaceState
Replaces an old state with a new state in the states map.- Parameters:
oldState- the state to be replacednewState- the state to replace with
-
removeBlockPos
Removes multiple BlockPos entries from the position map.- Parameters:
posList- the list of BlockPos objects to be removed- Throws:
NullPointerException- if posList or any BlockPos in it is null
-
removeBlockPos
public void removeBlockPos(net.minecraft.util.math.BlockPos pos) Removes a single BlockPos entry from the position map.- Parameters:
pos- the BlockPos to be removed- Throws:
NullPointerException- if pos is null
-
removeBlockPos
public net.minecraft.util.math.BlockPos removeBlockPos(int index) Removes the BlockPos at the specified index from the posList and posMap.- Parameters:
index- the index of the BlockPos to remove.- Returns:
- the removed BlockPos.
- Throws:
IndexOutOfBoundsException- if the index is out of range.
-
removeBlockPosPair
Removes the BlockPos at the specified index from the posList and retrieves its associated state.- Parameters:
index- the index of the BlockPos to remove.- Returns:
- a Pair containing the removed BlockPos and its associated state.
- Throws:
IndexOutOfBoundsException- if the index is out of range.
-
removeFirstPos
public net.minecraft.util.math.BlockPos removeFirstPos()Removes and returns the first BlockPos from the posList and posMap.- Returns:
- the first BlockPos that was removed.
- Throws:
IndexOutOfBoundsException- if the list is empty.
-
removeLastPos
public net.minecraft.util.math.BlockPos removeLastPos()Removes and returns the last BlockPos from the posList and posMap.- Returns:
- the first BlockPos that was removed.
- Throws:
IndexOutOfBoundsException- if the list is empty.
-
removeFirstBlockPos
Removes and returns the first BlockPos and its associated state from the posList.- Returns:
- a Pair containing the first BlockPos and its associated state.
- Throws:
IndexOutOfBoundsException- if the list is empty.
-
removeLastBlockPosPair
Removes and returns the last BlockPos and its associated state from the posList.- Returns:
- a Pair containing the last BlockPos and its associated state.
- Throws:
IndexOutOfBoundsException- if the list is empty.
-
removeAll
public void removeAll()Clears all elements from posList, posMap, and statesMap. After this operation, all structures will be empty. -
getT
Retrieves a list of all states in the states map.- Returns:
- a list of all states
-
getStatesMap
-
getPosMap
Method to get the posMap related to the object- Returns:
- the indexes (a Map of BlockPos and indexes)
-
setPosMap
Method to set the posMap- Parameters:
indexes- the posMap that will be set (a Map of BlockPos and indexes)
-
setPosList
-
getPosList
-
getBlockPos
Method to gate all the BlockPos related to provided state- Parameters:
state- the parameter that will be used to get the related BlockPos- Returns:
- a Set of BlockPos that correspond to the provided state.
-
getBlockPos
method to get all the BlockPos related to a list of states- Parameters:
states- the states that will be used to get all the related blockPos- Returns:
- a set of BlockPos that are linked to the provided states.
-
getBlockPosSet
Retrieves a set of all BlockPos entries in the position map.- Returns:
- a set of all BlockPos objects
-
getBlockPos
public net.minecraft.util.math.BlockPos getBlockPos(int index) Retrieves the BlockPos at a specified index.- Parameters:
index- the index of the BlockPos to retrieve- Returns:
- the BlockPos at the specified index
- Throws:
IndexOutOfBoundsException- if the index is out of bounds
-
getFirstBlockPos
public net.minecraft.util.math.BlockPos getFirstBlockPos()Retrieves the first BlockPos in the position map.- Returns:
- the first BlockPos
- Throws:
NoSuchElementException- if the position map is empty
-
getLastBlockPos
public net.minecraft.util.math.BlockPos getLastBlockPos()Retrieves the last BlockPos in the position map.- Returns:
- the last BlockPos
- Throws:
NoSuchElementException- if the position map is empty
-
getRandomBlockPos
public net.minecraft.util.math.BlockPos getRandomBlockPos()Retrieves a random BlockPos from the position map.- Returns:
- a random BlockPos
- Throws:
IllegalStateException- if the position map is empty
-
getRandomBlockPos
public net.minecraft.util.math.BlockPos getRandomBlockPos(net.minecraft.util.math.random.Random random) Retrieves a random BlockPos from the position map using a provided random generator.- Parameters:
random- the Random object to use for generating random indices- Returns:
- a random BlockPos
- Throws:
IllegalStateException- if the position map is empty
-
getPosPair
-
getFirstPosPair
Method to get the first BlockPos pair.- Returns:
- the pair of BlockPos and BlockState/nbt
-
getLastPosPair
method to get the last BlockPos pair- Returns:
- the pair of BlockPos and BlockState/nbt
-
posSize
public int posSize()Retrieves the number of entries in the position map.- Returns:
- the size of the position map
-
stateSize
public int stateSize()method to get the size of the stateMap- Returns:
- the size of the state map
-
isPosEmpty
public boolean isPosEmpty()method to know if no blockPos are present- Returns:
- true is no blockPos are present, false if not
-
isStateEmpty
public boolean isStateEmpty()method to know if no state is present- Returns:
- true is no blockPos are present, false if not
-
placeAll
public void placeAll(net.minecraft.world.StructureWorldAccess worldAccess) Method to place all the blocks in the comparator- Parameters:
worldAccess- the world where the blocks will be placed
-
placeAllWithVerification
public void placeAllWithVerification(net.minecraft.world.StructureWorldAccess worldAccess) Method to place all the blocks in the comparator with the BlockPos getting verified- Parameters:
worldAccess- the world where the blocks will be placed
-
placeAllWithDeletion
public void placeAllWithDeletion(net.minecraft.world.StructureWorldAccess worldAccess) Method to place all the blocks in the comparator and removing the BlockPos- Parameters:
worldAccess- the world where the blocks will be placed
-
placeAllWithVerificationDeletion
public void placeAllWithVerificationDeletion(net.minecraft.world.StructureWorldAccess worldAccess) Method to place all the blocks in the comparator with the BlockPos getting verified and the getting deleted- Parameters:
worldAccess- the world where the blocks will be placed
-
place
public abstract void place(net.minecraft.world.StructureWorldAccess world, int index) method to place the Block related to the index- Parameters:
world- the world the block will be placedindex- the index of the BlockPos
-
placeWithDeletion
public abstract void placeWithDeletion(net.minecraft.world.StructureWorldAccess world, int index) method to place the block with the deletion of the BlockPos- Parameters:
world- the world the block will be placedindex- the index of the block
-
placeWithVerification
public abstract boolean placeWithVerification(net.minecraft.world.StructureWorldAccess world, int index) Method to place the block related to the index. The method also performs verification to know if the block can be placed.- Parameters:
world- the world the block will be placedindex- the index of the block- Returns:
- true if the block was placed, false if not
-
placeWithVerificationDeletion
public abstract boolean placeWithVerificationDeletion(net.minecraft.world.StructureWorldAccess world, int index) Method to place the block with the deletion of the BlockPos The method also performs verification to know if the block can be placed.- Parameters:
world- the world the block will be placedindex- the index of the block- Returns:
- true if the block was placed, false if not
-
placeFirst
public abstract void placeFirst(net.minecraft.world.StructureWorldAccess world) method to place the first Block- Parameters:
world- the world the block will be placed
-
placeFirstWithDeletion
public abstract void placeFirstWithDeletion(net.minecraft.world.StructureWorldAccess world) Method to place the first Block and deleting it. You shouldn't use this method in normal case since that the method is pretty costly O(n). Use insteadplaceLastWithDeletion()that is faster O(1).- Parameters:
world- the world where the block will be placed
-
placeFirstWithVerification
public abstract boolean placeFirstWithVerification(net.minecraft.world.StructureWorldAccess world) Method to place the first Block.The method also performs verification to know if the block can be placed.
- Parameters:
world- the world where the block will be placed- Returns:
- true if the block was placed, false if not.
-
placeFirstWithVerificationDeletion
public abstract boolean placeFirstWithVerificationDeletion(net.minecraft.world.StructureWorldAccess world) Method to place the first Block and deleting it.
The method also performs verification to know if the block can be placed.
You shouldn't use this method in normal case since that the method is pretty costly O(n).
Use instead
placeLastWithDeletion()that is faster O(1).- Parameters:
world- the world where the block will be placed- Returns:
- true if the block was placed, false if not.
-
placeLastWithDeletion
public abstract void placeLastWithDeletion(net.minecraft.world.StructureWorldAccess world) Method to place the last Block of the comparator and removing it then. Consider using this method because it gives you better performance.- Parameters:
world- the world the last block will be placed
-
placeLast
public abstract void placeLast(net.minecraft.world.StructureWorldAccess world) Method to place the last Block of the comparator.- Parameters:
world- the world the last block will be placed
-
placeLastWithVerification
public abstract boolean placeLastWithVerification(net.minecraft.world.StructureWorldAccess world) Method to place the last Block.- Parameters:
world- the world the last block will be placed The method also performs verification to know if the block can be placed.- Returns:
- true if the block was placed, false if not
-
placeLastWithVerificationDeletion
public abstract boolean placeLastWithVerificationDeletion(net.minecraft.world.StructureWorldAccess world) Method to place the last Block of the comparator and removing it then. The method also performs verification to know if the block can be placed. Consider using this method because it gives you better performance.- Parameters:
world- the world the last block will be placed- Returns:
- true if the block was placed, false if not
-