-
Notifications
You must be signed in to change notification settings - Fork 754
/
ServantTileEntity.java
153 lines (134 loc) · 4.5 KB
/
ServantTileEntity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package slimeknights.tconstruct.common.multiblock;
import lombok.Getter;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Registry;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.Tag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.registries.ForgeRegistries;
import slimeknights.mantle.block.entity.MantleBlockEntity;
import slimeknights.mantle.util.BlockEntityHelper;
import slimeknights.tconstruct.library.utils.TagUtil;
import javax.annotation.Nullable;
public class ServantTileEntity extends MantleBlockEntity implements IServantLogic {
private static final String TAG_MASTER_POS = "masterPos";
private static final String TAG_MASTER_BLOCK = "masterBlock";
@Getter
@Nullable
private BlockPos masterPos;
@Nullable
private Block masterBlock;
public ServantTileEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) {
super(type, pos, state);
}
/** Checks if this servant has a master */
public boolean hasMaster() {
return masterPos != null;
}
/**
* Called to change the master
* @param master New master
* @param block New master block
*/
protected void setMaster(@Nullable BlockPos master, @Nullable Block block) {
masterPos = master;
masterBlock = block;
this.setChangedFast();
}
/**
* Checks that this servant has a valid master. Clears the master if invalid
* @return True if this servant has a valid master
*/
protected boolean validateMaster() {
if (masterPos == null) {
return false;
}
// ensure the master block is correct
assert level != null;
if (level.getBlockState(masterPos).getBlock() == masterBlock) {
return true;
}
// master invalid, so clear
setMaster(null, null);
return false;
}
@Override
public boolean isValidMaster(IMasterLogic master) {
// if we have a valid master, the passed master is only valid if its our current master
if (validateMaster()) {
return master.getMasterPos().equals(this.masterPos);
}
// otherwise, we are happy with any master
return true;
}
@Override
public void notifyMasterOfChange(BlockPos pos, BlockState state) {
if (validateMaster()) {
assert masterPos != null;
BlockEntityHelper.get(IMasterLogic.class, level, masterPos).ifPresent(te -> te.notifyChange(pos, state));
}
}
@Override
public void setPotentialMaster(IMasterLogic master) {
BlockPos newMaster = master.getMasterPos();
// if this is our current master, simply update the master block
if (newMaster.equals(this.masterPos)) {
masterBlock = master.getMasterBlock().getBlock();
this.setChangedFast();
// otherwise, only set if we don't have a master
} else if (!validateMaster()) {
setMaster(newMaster, master.getMasterBlock().getBlock());
}
}
@Override
public void removeMaster(IMasterLogic master) {
if (masterPos != null && masterPos.equals(master.getMasterPos())) {
setMaster(null, null);
}
}
/* NBT */
/**
* Reads the master from NBT
* @param tags NBT to read
*/
protected void readMaster(CompoundTag tags) {
BlockPos masterPos = TagUtil.readPos(tags, TAG_MASTER_POS);
Block masterBlock = null;
// if the master position is valid, get the master block
if (masterPos != null && tags.contains(TAG_MASTER_BLOCK, Tag.TAG_STRING)) {
ResourceLocation masterBlockName = ResourceLocation.tryParse(tags.getString(TAG_MASTER_BLOCK));
if (masterBlockName != null && ForgeRegistries.BLOCKS.containsKey(masterBlockName)) {
masterBlock = ForgeRegistries.BLOCKS.getValue(masterBlockName);
}
}
// if both valid, set
if (masterBlock != null) {
this.masterPos = masterPos;
this.masterBlock = masterBlock;
}
}
@Override
public void load(CompoundTag tags) {
super.load(tags);
readMaster(tags);
}
/**
* Writes the master position and master block to the given compound
* @param tags Tags
*/
protected CompoundTag writeMaster(CompoundTag tags) {
if (masterPos != null && masterBlock != null) {
tags.put(TAG_MASTER_POS, TagUtil.writePos(masterPos));
tags.putString(TAG_MASTER_BLOCK, Registry.BLOCK.getKey(masterBlock).toString());
}
return tags;
}
@Override
public void saveAdditional(CompoundTag tags) {
super.saveAdditional(tags);
writeMaster(tags);
}
}