mirror of
https://github.com/GTNewHorizons/ForgeMultipart.git
synced 2026-11-19 03:49:05 +00:00
refactor: port slotted tile trait to java
This commit is contained in:
1 parent
7894c98352
commit
39e0858006
7 files changed
+177
-87
No files matched your search
@@ -0,0 +1,71 @@
|
||||
package codechicken.multipart.scalatraits;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
|
||||
import codechicken.multipart.TMultiPart;
|
||||
import codechicken.multipart.TSlottedPart;
|
||||
import codechicken.multipart.TileMultipart;
|
||||
|
||||
/** Mixin implementation for slotted parts. */
|
||||
public class TSlottedTile extends TileMultipart {
|
||||
|
||||
public TMultiPart[] v_partMap = new TMultiPart[27];
|
||||
|
||||
@Override
|
||||
public void copyFrom(TileMultipart that) {
|
||||
super.copyFrom(that);
|
||||
if (that instanceof TSlottedTile) {
|
||||
v_partMap = ((TSlottedTile) that).v_partMap;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public TMultiPart partMap(int slot) {
|
||||
return v_partMap[slot];
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearParts() {
|
||||
super.clearParts();
|
||||
Arrays.fill(v_partMap, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void partRemoved(TMultiPart part, int position) {
|
||||
super.partRemoved(part, position);
|
||||
if (part instanceof TSlottedPart) {
|
||||
for (int index = 0; index < 27; index++) {
|
||||
if (Objects.equals(partMap(index), part)) {
|
||||
v_partMap[index] = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canAddPart(TMultiPart part) {
|
||||
if (part instanceof TSlottedPart) {
|
||||
int slotMask = ((TSlottedPart) part).getSlotMask();
|
||||
for (int index = 0; index < v_partMap.length; index++) {
|
||||
if ((slotMask & (1 << index)) != 0 && partMap(index) != null) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return super.canAddPart(part);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bindPart(TMultiPart part) {
|
||||
super.bindPart(part);
|
||||
if (part instanceof TSlottedPart) {
|
||||
int slotMask = ((TSlottedPart) part).getSlotMask();
|
||||
for (int index = 0; index < 27; index++) {
|
||||
if ((slotMask & (1 << index)) > 0) {
|
||||
v_partMap[index] = part;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
package codechicken.multipart.scalatraits
|
||||
|
||||
import codechicken.multipart.TileMultipart
|
||||
import codechicken.multipart.TMultiPart
|
||||
import codechicken.multipart.TSlottedPart
|
||||
|
||||
/** Mixin implementation for TSlottedPart. Puts parts into a slot array for
|
||||
* quick access at the cost of memory consumption
|
||||
*/
|
||||
trait TSlottedTile extends TileMultipart {
|
||||
var v_partMap = new Array[TMultiPart](27)
|
||||
|
||||
override def copyFrom(that: TileMultipart) {
|
||||
super.copyFrom(that)
|
||||
if (that.isInstanceOf[TSlottedTile])
|
||||
v_partMap = that.asInstanceOf[TSlottedTile].v_partMap
|
||||
}
|
||||
|
||||
override def partMap(slot: Int) = v_partMap(slot)
|
||||
|
||||
override def clearParts() {
|
||||
super.clearParts()
|
||||
for (i <- 0 until v_partMap.length)
|
||||
v_partMap(i) = null
|
||||
}
|
||||
|
||||
override def partRemoved(part: TMultiPart, p: Int) {
|
||||
super.partRemoved(part, p)
|
||||
if (part.isInstanceOf[TSlottedPart])
|
||||
for (i <- 0 until 27)
|
||||
if (partMap(i) == part)
|
||||
v_partMap(i) = null
|
||||
}
|
||||
|
||||
override def canAddPart(part: TMultiPart): Boolean = {
|
||||
if (part.isInstanceOf[TSlottedPart]) {
|
||||
val slotMask = part.asInstanceOf[TSlottedPart].getSlotMask
|
||||
for (i <- 0 until v_partMap.length)
|
||||
if ((slotMask & 1 << i) != 0 && partMap(i) != null)
|
||||
return false
|
||||
}
|
||||
|
||||
return super.canAddPart(part)
|
||||
}
|
||||
|
||||
override def bindPart(part: TMultiPart) {
|
||||
super.bindPart(part)
|
||||
if (part.isInstanceOf[TSlottedPart]) {
|
||||
val mask = part.asInstanceOf[TSlottedPart].getSlotMask
|
||||
for (i <- 0 until 27)
|
||||
if ((mask & 1 << i) > 0)
|
||||
v_partMap(i) = part
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user