-
Notifications
You must be signed in to change notification settings - Fork 207
Expand file tree
/
Copy pathTileEntityTransmutationTable.java
More file actions
183 lines (164 loc) · 7.41 KB
/
TileEntityTransmutationTable.java
File metadata and controls
183 lines (164 loc) · 7.41 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package com.github.alexthe666.alexsmobs.tileentity;
import com.github.alexthe666.alexsmobs.AlexsMobs;
import com.github.alexthe666.alexsmobs.config.AMConfig;
import com.github.alexthe666.alexsmobs.message.MessageUpdateTransmutablesToDisplay;
import com.github.alexthe666.alexsmobs.misc.AMAdvancementTriggerRegistry;
import com.github.alexthe666.alexsmobs.misc.AMSoundRegistry;
import com.github.alexthe666.alexsmobs.misc.TransmutationData;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundSource;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.storage.loot.LootParams;
import net.minecraft.world.level.storage.loot.LootTable;
import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets;
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
import java.util.*;
public class TileEntityTransmutationTable extends BlockEntity {
private static final ResourceLocation COMMON_ITEMS = new ResourceLocation("alexsmobs", "gameplay/transmutation_table_common");
private static final ResourceLocation UNCOMMON_ITEMS = new ResourceLocation("alexsmobs", "gameplay/transmutation_table_uncommon");
private static final ResourceLocation RARE_ITEMS = new ResourceLocation("alexsmobs", "gameplay/transmutation_table_rare");
public int ticksExisted;
private int totalTransmuteCount = 0;
private final Map<UUID, TransmutationData> playerToData = new HashMap<>();
private final ItemStack[] possiblities = new ItemStack[3];
private static final Random RANDOM = new Random();
private UUID rerollPlayerUUID = null;
public TileEntityTransmutationTable(BlockPos pos, BlockState state) {
super(AMTileEntityRegistry.TRANSMUTATION_TABLE.get(), pos, state);
}
public static void commonTick(Level level, BlockPos pos, BlockState state, TileEntityTransmutationTable entity) {
entity.tick();
}
private static ItemStack createFromLootTable(Player player, ResourceLocation loc) {
if(player.level().isClientSide){
return ItemStack.EMPTY;
}else{
LootTable loottable = player.level().getServer().getLootData().getLootTable(loc);
List<ItemStack> loots = loottable.getRandomItems((new LootParams.Builder((ServerLevel) player.level())).withParameter(LootContextParams.THIS_ENTITY, player).create(LootContextParamSets.EMPTY));
return loots.isEmpty() ? ItemStack.EMPTY : loots.get(0);
}
}
public void load(CompoundTag tag) {
super.load(tag);
totalTransmuteCount = tag.getInt("TotalCount");
playerToData.clear();
ListTag playerList = tag.getList("PlayerTransmutationData", 10);
for (int i = 0; i < playerList.size(); ++i) {
CompoundTag compoundtag = playerList.getCompound(i);
UUID uuid = compoundtag.getUUID("UUID");
if(uuid != null){
playerToData.put(uuid, TransmutationData.fromNBT(compoundtag.getCompound("TransmutationData")));
}
}
for(int i = 0; i < 3; i++){
if(tag.contains("Possibility" + i)){
possiblities[i] = ItemStack.of(tag.getCompound("Possibility" + i));
}
}
}
protected void saveAdditional(CompoundTag tag) {
super.saveAdditional(tag);
tag.putInt("TotalCount", totalTransmuteCount);
ListTag list = new ListTag();
for (Map.Entry<UUID, TransmutationData> entry : playerToData.entrySet()) {
CompoundTag innerTag = new CompoundTag();
innerTag.putUUID("UUID", entry.getKey());
innerTag.put("TransmutationData", entry.getValue().saveAsNBT());
list.add(innerTag);
}
tag.put("PlayerTransmutationData", list);
for (int i = 0; i < 3; i++) {
if (possiblities[i] != null && !possiblities[i].isEmpty()) {
tag.put("Possibility" + i, possiblities[i].serializeNBT());
}
}
}
private void randomizeResults(Player player){
rollPossiblity(player, 0);
rollPossiblity(player, 1);
rollPossiblity(player, 2);
int dataIndex = RANDOM.nextInt(2);
if(playerToData.containsKey(player.getUUID()) && !AMConfig.limitTransmutingToLootTables){
TransmutationData data = playerToData.get(player.getUUID());
if(RANDOM.nextFloat() < Math.min(0.01875F * data.getTotalWeight(), 0.2F)){
ItemStack stack = data.getRandomItem(RANDOM);
if(stack != null && !stack.isEmpty()){
possiblities[dataIndex] = stack;
}
}
}
AlexsMobs.sendMSGToAll(new MessageUpdateTransmutablesToDisplay(player.getId(), possiblities[0], possiblities[1], possiblities[2]));
}
public void rollPossiblity(Player player, int i){
if(player == null || player.level().isClientSide || !(player.level() instanceof ServerLevel)){
return;
}
ResourceLocation loot;
int safeIndex = Mth.clamp(i, 0, 2);
switch (safeIndex){
default:
case 0:
loot = COMMON_ITEMS;
break;
case 1:
loot = UNCOMMON_ITEMS;
break;
case 2:
loot = RARE_ITEMS;
break;
}
possiblities[safeIndex] = createFromLootTable(player, loot);
}
public boolean hasPossibilities(){
for(int i = 0; i < 3; i++){
if(possiblities[i] == null || possiblities[i].isEmpty()){
return false;
}
}
return true;
}
public ItemStack getPossibility(int i){
int safeIndex = Mth.clamp(i, 0, 2);
ItemStack possible = possiblities[safeIndex];
return possible == null ? ItemStack.EMPTY : possible;
}
public void postTransmute(Player player, ItemStack from, ItemStack to){
TransmutationData data;
if(playerToData.containsKey(player.getUUID())){
data = playerToData.get(player.getUUID());
}else{
data = new TransmutationData();
}
data.onTransmuteItem(from, to);
playerToData.put(player.getUUID(), data);
totalTransmuteCount += from.getCount();
if(player instanceof ServerPlayer && totalTransmuteCount >= 1000){
AMAdvancementTriggerRegistry.TRANSMUTE_1000_ITEMS.trigger((ServerPlayer)player);
}
setRerollPlayerUUID(player.getUUID());
}
public void tick() {
ticksExisted++;
if(rerollPlayerUUID != null){
Player player = level.getPlayerByUUID(rerollPlayerUUID);
if(player != null){
this.level.playSound(null, this.getBlockPos(), AMSoundRegistry.TRANSMUTE_ITEM.get(), SoundSource.BLOCKS, 1F, 0.9F + player.getRandom().nextFloat() * 0.2F);
this.randomizeResults(player);
}
rerollPlayerUUID = null;
}
}
public void setRerollPlayerUUID(UUID uuid){
this.rerollPlayerUUID = uuid;
}
}