|
2 | 2 |
|
3 | 3 | import com.hollingsworth.arsnouveau.common.block.tile.MobJarTile; |
4 | 4 | import com.hollingsworth.arsnouveau.common.items.MobJarItem; |
| 5 | +import com.hollingsworth.arsnouveau.common.lib.EntityTags; |
5 | 6 | import com.klikli_dev.occultism.Occultism; |
6 | 7 | import com.klikli_dev.occultism.common.entity.job.CrusherJob; |
7 | 8 | import com.klikli_dev.occultism.common.entity.job.CrystallizerJob; |
8 | 9 | import com.klikli_dev.occultism.common.entity.job.SmelterJob; |
9 | 10 | import com.klikli_dev.occultism.common.entity.job.TraderJob; |
10 | 11 | import com.klikli_dev.occultism.common.entity.spirit.SpiritEntity; |
| 12 | +import com.klikli_dev.occultism.common.item.tool.SoulGemItem; |
| 13 | +import com.klikli_dev.occultism.registry.OccultismItems; |
| 14 | +import com.klikli_dev.occultism.registry.OccultismTags; |
| 15 | +import com.klikli_dev.occultism.util.EntityUtil; |
11 | 16 | import com.mystchonky.arsocultas.ArsOcultas; |
| 17 | +import com.mystchonky.arsocultas.Config; |
12 | 18 | import com.mystchonky.arsocultas.content.spirit_jar.SpiritBehaviour; |
13 | 19 | import net.minecraft.ChatFormatting; |
14 | 20 | import net.minecraft.client.resources.language.I18n; |
| 21 | +import net.minecraft.core.component.DataComponents; |
| 22 | +import net.minecraft.nbt.CompoundTag; |
15 | 23 | import net.minecraft.network.chat.Component; |
| 24 | +import net.minecraft.world.InteractionResult; |
| 25 | +import net.minecraft.world.entity.Entity; |
| 26 | +import net.minecraft.world.entity.EntityType; |
| 27 | +import net.minecraft.world.item.component.CustomData; |
16 | 28 | import net.neoforged.bus.api.SubscribeEvent; |
17 | 29 | import net.neoforged.fml.common.EventBusSubscriber; |
18 | 30 | import net.neoforged.neoforge.event.entity.player.ItemTooltipEvent; |
| 31 | +import net.neoforged.neoforge.event.entity.player.PlayerInteractEvent; |
19 | 32 | import org.apache.commons.lang3.StringUtils; |
20 | 33 |
|
21 | 34 | @EventBusSubscriber(modid = ArsOcultas.MODID) |
@@ -102,4 +115,116 @@ public static void itemTooltips(ItemTooltipEvent event) { |
102 | 115 | } |
103 | 116 | } |
104 | 117 |
|
| 118 | + @SubscribeEvent |
| 119 | + public static void playerRightClick(PlayerInteractEvent.RightClickBlock event) { |
| 120 | + var player = event.getEntity(); |
| 121 | + var stack = event.getItemStack(); |
| 122 | + if (!(stack.getItem() instanceof SoulGemItem)) { |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + var level = event.getLevel(); |
| 127 | + var hit = event.getHitVec(); |
| 128 | + var be = level.getBlockEntity(hit.getBlockPos()); |
| 129 | + if (!(be instanceof MobJarTile jar)) { |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + var gemData = stack.get(DataComponents.ENTITY_DATA); |
| 134 | + var jarEntity = jar.getEntity(); |
| 135 | + |
| 136 | + if (gemData == null && jarEntity != null) { |
| 137 | + if (!Config.SERVER.CONTAINMENT_JARS_SOUL_GEM_PICKUP.get()) { |
| 138 | + // We cancel regardless to prevent an entity dupe. |
| 139 | + event.setCancellationResult(InteractionResult.FAIL); |
| 140 | + event.setCanceled(true); |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + var type = jarEntity.getType(); |
| 145 | + |
| 146 | + if (type.is(EntityTags.JAR_RELEASE_BLACKLIST) || (!type.is(EntityTags.JAR_WHITELIST) && type.is(EntityTags.JAR_BLACKLIST))) { |
| 147 | + player.sendSystemMessage( |
| 148 | + Component.translatable(stack.getDescriptionId() + ".message.entity_type_denied")); |
| 149 | + event.setCancellationResult(InteractionResult.FAIL); |
| 150 | + event.setCanceled(true); |
| 151 | + return; |
| 152 | + } |
| 153 | + |
| 154 | + if (type.is(OccultismTags.Entities.SOUL_GEM_DENY_LIST) && stack.getItem().equals(OccultismItems.SOUL_GEM_ITEM.get())) { |
| 155 | + player.sendSystemMessage( |
| 156 | + Component.translatable(stack.getDescriptionId() + ".message.entity_type_denied")); |
| 157 | + event.setCancellationResult(InteractionResult.FAIL); |
| 158 | + event.setCanceled(true); |
| 159 | + return; |
| 160 | + } |
| 161 | + |
| 162 | + if (type.is(OccultismTags.Entities.TRINITY_GEM_DENY_LIST) && stack.getItem().equals(OccultismItems.TRINITY_GEM_ITEM.get())) { |
| 163 | + player.sendSystemMessage( |
| 164 | + Component.translatable(stack.getDescriptionId() + ".message.entity_type_denied")); |
| 165 | + event.setCancellationResult(InteractionResult.FAIL); |
| 166 | + event.setCanceled(true); |
| 167 | + return; |
| 168 | + } |
| 169 | + |
| 170 | + var entityData = new CompoundTag(); |
| 171 | + var id = jarEntity.getEncodeId(); |
| 172 | + if (id != null) { |
| 173 | + entityData.putString("id", id); |
| 174 | + } |
| 175 | + |
| 176 | + entityData = jarEntity.saveWithoutId(entityData); |
| 177 | + stack.set(DataComponents.ENTITY_DATA, CustomData.of(entityData)); |
| 178 | + jar.removeEntity(); |
| 179 | + |
| 180 | + event.setCancellationResult(InteractionResult.SUCCESS); |
| 181 | + event.setCanceled(true); |
| 182 | + } else if (gemData != null && jarEntity == null) { |
| 183 | + if (!Config.SERVER.CONTAINMENT_JARS_SOUL_GEM_PLACE.get()) { |
| 184 | + return; |
| 185 | + } |
| 186 | + |
| 187 | + CompoundTag entityData = stack.get(DataComponents.ENTITY_DATA).getUnsafe(); |
| 188 | + stack.remove(DataComponents.ENTITY_DATA); |
| 189 | + |
| 190 | + EntityType<?> type = EntityUtil.entityTypeFromNbt(entityData); |
| 191 | + |
| 192 | + if (!type.is(EntityTags.JAR_WHITELIST) && type.is(EntityTags.JAR_BLACKLIST)) { |
| 193 | + player.sendSystemMessage( |
| 194 | + Component.translatable(stack.getDescriptionId() + ".message.entity_type_denied")); |
| 195 | + event.setCancellationResult(InteractionResult.FAIL); |
| 196 | + event.setCanceled(true); |
| 197 | + } |
| 198 | + |
| 199 | + if (type.is(OccultismTags.Entities.SOUL_GEM_DENY_LIST) && stack.getItem().equals(OccultismItems.SOUL_GEM_ITEM.get())) { |
| 200 | + player.sendSystemMessage( |
| 201 | + Component.translatable(stack.getDescriptionId() + ".message.entity_type_denied")); |
| 202 | + event.setCancellationResult(InteractionResult.FAIL); |
| 203 | + event.setCanceled(true); |
| 204 | + return; |
| 205 | + } |
| 206 | + |
| 207 | + if (type.is(OccultismTags.Entities.TRINITY_GEM_DENY_LIST) && stack.getItem().equals(OccultismItems.TRINITY_GEM_ITEM.get())) { |
| 208 | + player.sendSystemMessage( |
| 209 | + Component.translatable(stack.getDescriptionId() + ".message.entity_type_denied")); |
| 210 | + event.setCancellationResult(InteractionResult.FAIL); |
| 211 | + event.setCanceled(true); |
| 212 | + return; |
| 213 | + } |
| 214 | + |
| 215 | + Entity entity = type.create(level); |
| 216 | + if (entity == null) { |
| 217 | + event.setCancellationResult(InteractionResult.FAIL); |
| 218 | + event.setCanceled(true); |
| 219 | + return; |
| 220 | + } |
| 221 | + |
| 222 | + entity.load(entityData); |
| 223 | + jar.setEntityData(entity); |
| 224 | + |
| 225 | + event.setCancellationResult(InteractionResult.SUCCESS); |
| 226 | + event.setCanceled(true); |
| 227 | + } |
| 228 | + } |
| 229 | + |
105 | 230 | } |
0 commit comments