ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/event/CommonEvents.java

199 lines
9.7 KiB
Java
Raw Normal View History

2016-07-03 20:57:00 +02:00
/*
* This file ("CommonEvents.java") is part of the Actually Additions mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://ellpeck.de/actaddlicense
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
2016-07-03 20:57:00 +02:00
*/
package de.ellpeck.actuallyadditions.mod.event;
2018-05-10 11:38:58 +02:00
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
2021-11-14 19:18:07 +01:00
import de.ellpeck.actuallyadditions.mod.config.CommonConfig;
2016-07-03 20:57:00 +02:00
import de.ellpeck.actuallyadditions.mod.config.values.ConfigBoolValues;
import de.ellpeck.actuallyadditions.mod.data.PlayerData;
import de.ellpeck.actuallyadditions.mod.data.WorldData;
import de.ellpeck.actuallyadditions.mod.items.ActuallyItems;
2021-11-25 21:27:45 +01:00
import de.ellpeck.actuallyadditions.mod.items.DrillItem;
2023-12-20 22:02:25 +01:00
import de.ellpeck.actuallyadditions.mod.items.Sack;
2016-08-09 20:56:09 +02:00
import de.ellpeck.actuallyadditions.mod.network.PacketHandlerHelper;
2017-06-17 00:55:55 +02:00
import de.ellpeck.actuallyadditions.mod.tile.FilterSettings;
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA;
2017-06-17 00:55:55 +02:00
import de.ellpeck.actuallyadditions.mod.util.ItemUtil;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
2024-03-04 20:21:48 +01:00
import net.minecraft.core.registries.BuiltInRegistries;
2024-03-02 21:23:08 +01:00
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.monster.Spider;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
2024-03-04 20:21:48 +01:00
import net.neoforged.bus.api.Event;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.neoforge.event.entity.living.LivingDropsEvent;
import net.neoforged.neoforge.event.entity.player.EntityItemPickupEvent;
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
import net.neoforged.neoforge.event.level.BlockEvent;
2016-07-03 20:57:00 +02:00
2021-02-26 22:15:48 +01:00
import java.util.Locale;
2019-05-02 09:10:29 +02:00
public class CommonEvents {
2016-07-03 20:57:00 +02:00
2022-01-09 18:58:55 +01:00
//TODO spawner shards are yeeted right?
2017-06-17 00:55:55 +02:00
@SubscribeEvent
public void onBlockBreakEvent(BlockEvent.BreakEvent event) {
2021-02-26 22:15:48 +01:00
BlockState state = event.getState();
if (state != null && state.getBlock() == Blocks.SPAWNER) {
// TODO: [port] add back once we've unflattened
// event.getDrops().add(new ItemStack(InitItems.itemMisc, 1, TheMiscItems.SPAWNER_SHARD.ordinal()));
2017-06-17 00:55:55 +02:00
}
}
@SubscribeEvent
2019-05-02 09:10:29 +02:00
public void onItemPickup(EntityItemPickupEvent event) {
2021-02-26 22:15:48 +01:00
if (event.isCanceled() || event.getResult() == Event.Result.ALLOW) {
return;
}
2017-06-17 00:55:55 +02:00
2024-03-03 01:20:53 +01:00
Player player = event.getEntity();
ItemEntity item = event.getItem();
if (item != null && item.isAlive()) {
ItemStack stack = item.getItem();
2019-05-02 09:10:29 +02:00
if (StackUtil.isValid(stack)) {
2024-03-02 21:23:08 +01:00
for (int i = 0; i < player.getInventory().getContainerSize(); i++) {
if (i != player.getInventory().selected) {
2017-06-17 00:55:55 +02:00
2024-03-02 21:23:08 +01:00
ItemStack invStack = player.getInventory().getItem(i);
2023-12-20 22:02:25 +01:00
if (StackUtil.isValid(invStack) && invStack.getItem() instanceof Sack && invStack.hasTag()) {
if (invStack.getOrCreateTag().getBoolean("AutoInsert")) {
2017-06-17 00:55:55 +02:00
boolean changed = false;
2023-12-20 22:02:25 +01:00
boolean isVoid = ((Sack) invStack.getItem()).isVoid;
2023-03-16 00:17:57 +01:00
ItemStackHandlerAA inv = new ItemStackHandlerAA(28); //TODO whats going on here
2021-11-25 21:27:45 +01:00
DrillItem.loadSlotsFromNBT(inv, invStack);
2017-06-17 00:55:55 +02:00
2024-03-05 23:23:49 +01:00
FilterSettings filter = new FilterSettings(4, false, false);
filter.readFromNBT(invStack.getOrCreateTag(), "Filter");
2019-05-02 09:10:29 +02:00
if (filter.check(stack)) {
if (isVoid) {
stack.setCount(0);
2017-06-17 00:55:55 +02:00
changed = true;
2019-05-02 09:10:29 +02:00
} else {
for (int j = 0; j < inv.getSlots(); j++) {
2017-06-17 00:55:55 +02:00
ItemStack bagStack = inv.getStackInSlot(j);
2019-05-02 09:10:29 +02:00
if (StackUtil.isValid(bagStack)) {
if (ItemUtil.canBeStacked(bagStack, stack)) {
int maxTransfer = Math.min(stack.getCount(), stack.getMaxStackSize() - bagStack.getCount());
if (maxTransfer > 0) {
inv.setStackInSlot(j, StackUtil.grow(bagStack, maxTransfer));
stack.shrink(maxTransfer);
2017-06-17 00:55:55 +02:00
changed = true;
}
}
2019-05-02 09:10:29 +02:00
} else {
2017-06-17 00:55:55 +02:00
inv.setStackInSlot(j, stack.copy());
stack.setCount(0);
2017-06-17 00:55:55 +02:00
changed = true;
}
2019-05-02 09:10:29 +02:00
if (!StackUtil.isValid(stack)) {
2017-06-17 00:55:55 +02:00
break;
}
}
}
}
2019-05-02 09:10:29 +02:00
if (changed) {
if (!isVoid) {
2021-11-25 21:27:45 +01:00
DrillItem.writeSlotsToNBT(inv, invStack);
2017-06-17 00:55:55 +02:00
}
event.setResult(Event.Result.ALLOW);
}
}
}
}
2019-05-02 09:10:29 +02:00
if (!StackUtil.isValid(stack)) {
2017-06-17 00:55:55 +02:00
break;
}
}
}
item.setItem(stack);
2017-06-17 00:55:55 +02:00
}
}
2022-01-09 18:58:55 +01:00
//TODO Checking Achievements? yeet?
2021-02-26 22:15:48 +01:00
/*public static void checkAchievements(ItemStack gotten, PlayerEntity player, InitAchievements.Type type){
2017-02-13 15:34:35 +01:00
if(gotten != null && player != null){
2016-08-05 04:49:04 +02:00
for(TheAchievements ach : TheAchievements.values()){
if(ach.type == type){
if(ItemUtil.contains(ach.itemsToBeGotten, gotten, true)){
ach.get(player);
2016-07-07 17:59:45 +02:00
}
}
}
}
2017-06-17 00:48:49 +02:00
}*/
2016-07-07 17:59:45 +02:00
2022-01-09 18:58:55 +01:00
//TODO this isnt how this should be done im pretty sure...
2016-07-03 20:57:00 +02:00
@SubscribeEvent
2019-05-02 09:10:29 +02:00
public void onEntityDropEvent(LivingDropsEvent event) {
2024-03-03 01:20:53 +01:00
if (event.getEntity().level() != null && !event.getEntity().level().isClientSide && event.getSource().getEntity() instanceof Player) {
2016-07-03 20:57:00 +02:00
//Drop Cobwebs from Spiders
2024-03-03 01:20:53 +01:00
if (ConfigBoolValues.DO_SPIDER_DROPS.isEnabled() && event.getEntity() instanceof Spider) {
if (event.getEntity().level().random.nextInt(20) <= event.getLootingLevel() * 2) {
event.getDrops().add(new ItemEntity(event.getEntity().level(), event.getEntity().getX(), event.getEntity().getY(), event.getEntity().getZ(), new ItemStack(Blocks.COBWEB, event.getEntity().level().random.nextInt(2 + event.getLootingLevel()) + 1)));
2016-07-03 20:57:00 +02:00
}
}
}
}
@SubscribeEvent
2019-05-02 09:10:29 +02:00
public void onLogInEvent(PlayerEvent.PlayerLoggedInEvent event) {
2024-03-03 01:20:53 +01:00
if (!event.getEntity().level().isClientSide && event.getEntity() instanceof ServerPlayer player) {
PacketHandlerHelper.syncPlayerData(player, true);
ActuallyAdditions.LOGGER.info("Sending Player Data to player " + player.getName() + " with UUID " + player.getUUID() + ".");
2016-07-03 20:57:00 +02:00
}
}
2022-01-09 18:58:55 +01:00
//TODO im pretty sure this can be done with normal advancements...
2016-07-03 20:57:00 +02:00
@SubscribeEvent
2019-05-02 09:10:29 +02:00
public void onCraftedEvent(PlayerEvent.ItemCraftedEvent event) {
2017-06-17 00:48:49 +02:00
//checkAchievements(event.crafting, event.player, InitAchievements.Type.CRAFTING);
2016-07-03 20:57:00 +02:00
if (CommonConfig.Other.GIVE_BOOKLET_ON_FIRST_CRAFT.get()) {
2024-03-03 01:20:53 +01:00
if (!event.getEntity().level().isClientSide && StackUtil.isValid(event.getCrafting()) && event.getCrafting().getItem() != ActuallyItems.ITEM_BOOKLET.get()) {
2016-07-03 20:57:00 +02:00
2024-03-04 20:21:48 +01:00
String name = BuiltInRegistries.ITEM.getKey(event.getCrafting().getItem()).toString();
2019-05-02 09:10:29 +02:00
if (name != null && name.toLowerCase(Locale.ROOT).contains(ActuallyAdditions.MODID)) {
2024-03-03 01:20:53 +01:00
PlayerData.PlayerSave save = PlayerData.getDataFromPlayer(event.getEntity());
2019-05-02 09:10:29 +02:00
if (save != null && !save.bookGottenAlready) {
save.bookGottenAlready = true;
2024-03-03 01:20:53 +01:00
WorldData.get(event.getEntity().getCommandSenderWorld()).setDirty();
2016-07-03 20:57:00 +02:00
2024-03-03 01:20:53 +01:00
ItemEntity entityItem = new ItemEntity(event.getEntity().level(), event.getEntity().getX(), event.getEntity().getY(), event.getEntity().getZ(), new ItemStack(ActuallyItems.ITEM_BOOKLET.get()));
entityItem.setPickUpDelay(0);
2024-03-03 01:20:53 +01:00
event.getEntity().level().addFreshEntity(entityItem);
2016-07-03 20:57:00 +02:00
}
}
}
}
}
2022-01-09 18:58:55 +01:00
//TODO im pretty sure this can be done with normal advancements...
2016-07-03 20:57:00 +02:00
@SubscribeEvent
2019-05-02 09:10:29 +02:00
public void onSmeltedEvent(PlayerEvent.ItemSmeltedEvent event) {
2017-06-17 00:48:49 +02:00
//checkAchievements(event.smelting, event.player, InitAchievements.Type.SMELTING);
2016-07-03 20:57:00 +02:00
}
2022-01-09 18:58:55 +01:00
//TODO im pretty sure this can be done with normal advancements...
2016-07-03 20:57:00 +02:00
@SubscribeEvent
2019-05-02 09:10:29 +02:00
public void onPickupEvent(EntityItemPickupEvent event) {
//checkAchievements(event.getItem().getItem(), event.getEntityPlayer(), InitAchievements.Type.PICK_UP);
2016-07-03 20:57:00 +02:00
}
}