--- a/net/minecraft/world/entity/raid/Raids.java
+++ b/net/minecraft/world/entity/raid/Raids.java
@@ -24,6 +24,7 @@
 import net.minecraft.world.phys.Vec3;
 
 public class Raids extends SavedData {
+
     private static final String RAID_FILE_ID = "raids";
     public final Map<Integer, Raid> raidMap = Maps.newHashMap();
     private final ServerLevel level;
@@ -31,7 +32,11 @@
     private int tick;
 
     public static SavedData.Factory<Raids> factory(ServerLevel world) {
-        return new SavedData.Factory<>(() -> new Raids(world), (nbt, registryLookup) -> load(world, nbt), DataFixTypes.SAVED_DATA_RAIDS);
+        return new SavedData.Factory<>(() -> {
+            return new Raids(world);
+        }, (nbttagcompound, holderlookup_a) -> {
+            return Raids.load(world, nbttagcompound);
+        }, DataFixTypes.SAVED_DATA_RAIDS);
     }
 
     public Raids(ServerLevel world) {
@@ -41,15 +46,16 @@
     }
 
     public Raid get(int id) {
-        return this.raidMap.get(id);
+        return (Raid) this.raidMap.get(id);
     }
 
     public void tick() {
-        this.tick++;
+        ++this.tick;
         Iterator<Raid> iterator = this.raidMap.values().iterator();
 
         while (iterator.hasNext()) {
-            Raid raid = iterator.next();
+            Raid raid = (Raid) iterator.next();
+
             if (this.level.getGameRules().getBoolean(GameRules.RULE_DISABLE_RAIDS)) {
                 raid.stop();
             }
@@ -70,13 +76,7 @@
     }
 
     public static boolean canJoinRaid(Raider raider, Raid raid) {
-        return raider != null
-            && raid != null
-            && raid.getLevel() != null
-            && raider.isAlive()
-            && raider.canJoinRaid()
-            && raider.getNoActionTime() <= 2400
-            && raider.level().dimensionType() == raid.getLevel().dimensionType();
+        return raider != null && raid != null && raid.getLevel() != null ? raider.isAlive() && raider.canJoinRaid() && raider.getNoActionTime() <= 2400 && raider.level().dimensionType() == raid.getLevel().dimensionType() : false;
     }
 
     @Nullable
@@ -86,37 +86,52 @@
         } else if (this.level.getGameRules().getBoolean(GameRules.RULE_DISABLE_RAIDS)) {
             return null;
         } else {
-            DimensionType dimensionType = player.level().dimensionType();
-            if (!dimensionType.hasRaids()) {
+            DimensionType dimensionmanager = player.level().dimensionType();
+
+            if (!dimensionmanager.hasRaids()) {
                 return null;
             } else {
-                List<PoiRecord> list = this.level
-                    .getPoiManager()
-                    .getInRange(poiType -> poiType.is(PoiTypeTags.VILLAGE), pos, 64, PoiManager.Occupancy.IS_OCCUPIED)
-                    .toList();
+                List<PoiRecord> list = this.level.getPoiManager().getInRange((holder) -> {
+                    return holder.is(PoiTypeTags.VILLAGE);
+                }, pos, 64, PoiManager.Occupancy.IS_OCCUPIED).toList();
                 int i = 0;
-                Vec3 vec3 = Vec3.ZERO;
+                Vec3 vec3d = Vec3.ZERO;
 
-                for (PoiRecord poiRecord : list) {
-                    BlockPos blockPos = poiRecord.getPos();
-                    vec3 = vec3.add((double)blockPos.getX(), (double)blockPos.getY(), (double)blockPos.getZ());
-                    i++;
+                for (Iterator iterator = list.iterator(); iterator.hasNext(); ++i) {
+                    PoiRecord villageplacerecord = (PoiRecord) iterator.next();
+                    BlockPos blockposition1 = villageplacerecord.getPos();
+
+                    vec3d = vec3d.add((double) blockposition1.getX(), (double) blockposition1.getY(), (double) blockposition1.getZ());
                 }
 
-                BlockPos blockPos2;
+                BlockPos blockposition2;
+
                 if (i > 0) {
-                    vec3 = vec3.scale(1.0 / (double)i);
-                    blockPos2 = BlockPos.containing(vec3);
+                    vec3d = vec3d.scale(1.0D / (double) i);
+                    blockposition2 = BlockPos.containing(vec3d);
                 } else {
-                    blockPos2 = pos;
+                    blockposition2 = pos;
                 }
 
-                Raid raid = this.getOrCreateRaid(player.serverLevel(), blockPos2);
+                Raid raid = this.getOrCreateRaid(player.serverLevel(), blockposition2);
+
+                /* CraftBukkit - moved down
                 if (!raid.isStarted() && !this.raidMap.containsKey(raid.getId())) {
                     this.raidMap.put(raid.getId(), raid);
                 }
+                */
 
-                if (!raid.isStarted() || raid.getRaidOmenLevel() < raid.getMaxRaidOmenLevel()) {
+                if (!raid.isStarted() || (raid.isInProgress() && raid.getRaidOmenLevel() < raid.getMaxRaidOmenLevel())) { // CraftBukkit - fixed a bug with raid: players could add up Bad Omen level even when the raid had finished
+                    // CraftBukkit start
+                    if (!org.bukkit.craftbukkit.event.CraftEventFactory.callRaidTriggerEvent(raid, player)) {
+                        player.removeEffect(net.minecraft.world.effect.MobEffects.RAID_OMEN);
+                        return null;
+                    }
+
+                    if (!raid.isStarted() && !this.raidMap.containsKey(raid.getId())) {
+                        this.raidMap.put(raid.getId(), raid);
+                    }
+                    // CraftBukkit end
                     raid.absorbRaidOmen(player);
                 }
 
@@ -128,37 +143,43 @@
 
     private Raid getOrCreateRaid(ServerLevel world, BlockPos pos) {
         Raid raid = world.getRaidAt(pos);
+
         return raid != null ? raid : new Raid(this.getUniqueId(), world, pos);
     }
 
     public static Raids load(ServerLevel world, CompoundTag nbt) {
-        Raids raids = new Raids(world);
-        raids.nextAvailableID = nbt.getInt("NextAvailableID");
-        raids.tick = nbt.getInt("Tick");
-        ListTag listTag = nbt.getList("Raids", 10);
+        Raids persistentraid = new Raids(world);
 
-        for (int i = 0; i < listTag.size(); i++) {
-            CompoundTag compoundTag = listTag.getCompound(i);
-            Raid raid = new Raid(world, compoundTag);
-            raids.raidMap.put(raid.getId(), raid);
+        persistentraid.nextAvailableID = nbt.getInt("NextAvailableID");
+        persistentraid.tick = nbt.getInt("Tick");
+        ListTag nbttaglist = nbt.getList("Raids", 10);
+
+        for (int i = 0; i < nbttaglist.size(); ++i) {
+            CompoundTag nbttagcompound1 = nbttaglist.getCompound(i);
+            Raid raid = new Raid(world, nbttagcompound1);
+
+            persistentraid.raidMap.put(raid.getId(), raid);
         }
 
-        return raids;
+        return persistentraid;
     }
 
     @Override
     public CompoundTag save(CompoundTag nbt, HolderLookup.Provider registryLookup) {
         nbt.putInt("NextAvailableID", this.nextAvailableID);
         nbt.putInt("Tick", this.tick);
-        ListTag listTag = new ListTag();
+        ListTag nbttaglist = new ListTag();
+        Iterator iterator = this.raidMap.values().iterator();
 
-        for (Raid raid : this.raidMap.values()) {
-            CompoundTag compoundTag = new CompoundTag();
-            raid.save(compoundTag);
-            listTag.add(compoundTag);
+        while (iterator.hasNext()) {
+            Raid raid = (Raid) iterator.next();
+            CompoundTag nbttagcompound1 = new CompoundTag();
+
+            raid.save(nbttagcompound1);
+            nbttaglist.add(nbttagcompound1);
         }
 
-        nbt.put("Raids", listTag);
+        nbt.put("Raids", nbttaglist);
         return nbt;
     }
 
@@ -173,13 +194,16 @@
     @Nullable
     public Raid getNearbyRaid(BlockPos pos, int searchDistance) {
         Raid raid = null;
-        double d = (double)searchDistance;
+        double d0 = (double) searchDistance;
+        Iterator iterator = this.raidMap.values().iterator();
 
-        for (Raid raid2 : this.raidMap.values()) {
-            double e = raid2.getCenter().distSqr(pos);
-            if (raid2.isActive() && e < d) {
-                raid = raid2;
-                d = e;
+        while (iterator.hasNext()) {
+            Raid raid1 = (Raid) iterator.next();
+            double d1 = raid1.getCenter().distSqr(pos);
+
+            if (raid1.isActive() && d1 < d0) {
+                raid = raid1;
+                d0 = d1;
             }
         }
 
