generated from TropheusJ/fabric-example-mod
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
.../core/src/main/java/io/github/fabricators_of_create/porting_lib/core/event/BaseEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
24 changes: 24 additions & 0 deletions
24
...ts/src/main/java/io/github/fabricators_of_create/porting_lib/level/events/LevelEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package io.github.fabricators_of_create.porting_lib.level.events; | ||
|
||
import io.github.fabricators_of_create.porting_lib.core.event.BaseEvent; | ||
import net.minecraft.world.level.LevelAccessor; | ||
|
||
/** | ||
* This event is fired whenever an event involving a {@link LevelAccessor} occurs. | ||
*/ | ||
public abstract class LevelEvent extends BaseEvent { | ||
private final LevelAccessor level; | ||
|
||
public LevelEvent(LevelAccessor level) | ||
{ | ||
this.level = level; | ||
} | ||
|
||
/** | ||
* {@return the level this event is affecting} | ||
*/ | ||
public LevelAccessor getLevel() | ||
{ | ||
return level; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...java/io/github/fabricators_of_create/porting_lib/level/events/SleepFinishedTimeEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package io.github.fabricators_of_create.porting_lib.level.events; | ||
|
||
import net.fabricmc.fabric.api.event.Event; | ||
import net.fabricmc.fabric.api.event.EventFactory; | ||
import net.minecraft.server.level.ServerLevel; | ||
|
||
/** | ||
* This event is fired when all players are asleep and the time should be set to day.<br> | ||
* <p> | ||
* setTimeAddition(wakeUpTime) sets a new time that will be added to the dayTime.<br> | ||
*/ | ||
public class SleepFinishedTimeEvent extends LevelEvent { | ||
public static final Event<SleepFinishedCallback> SLEEP_FINISHED = EventFactory.createArrayBacked(SleepFinishedCallback.class, callbacks -> event -> { | ||
for (SleepFinishedCallback e : callbacks) | ||
e.onSleepFinished(event); | ||
}); | ||
private long newTime; | ||
private final long minTime; | ||
|
||
public SleepFinishedTimeEvent(ServerLevel level, long newTime, long minTime) { | ||
super(level); | ||
this.newTime = newTime; | ||
this.minTime = minTime; | ||
} | ||
|
||
/** | ||
* @return the new time | ||
*/ | ||
public long getNewTime() { | ||
return newTime; | ||
} | ||
|
||
/** | ||
* Sets the new time which should be set when all players wake up | ||
* | ||
* @param newTimeIn The new time at wakeup | ||
* @return {@code false} if newTimeIn was lower than current time | ||
*/ | ||
public boolean setTimeAddition(long newTimeIn) { | ||
if (minTime > newTimeIn) | ||
return false; | ||
this.newTime = newTimeIn; | ||
return true; | ||
} | ||
|
||
@Override | ||
public void sendEvent() { | ||
SLEEP_FINISHED.invoker().onSleepFinished(this); | ||
} | ||
|
||
@FunctionalInterface | ||
public interface SleepFinishedCallback { | ||
void onSleepFinished(SleepFinishedTimeEvent event); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...c/main/java/io/github/fabricators_of_create/porting_lib/level/mixin/ServerLevelMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package io.github.fabricators_of_create.porting_lib.level.mixin; | ||
|
||
import io.github.fabricators_of_create.porting_lib.level.events.SleepFinishedTimeEvent; | ||
import net.minecraft.core.Holder; | ||
import net.minecraft.core.RegistryAccess; | ||
import net.minecraft.resources.ResourceKey; | ||
import net.minecraft.server.level.ServerLevel; | ||
|
||
import net.minecraft.util.profiling.ProfilerFiller; | ||
import net.minecraft.world.level.Level; | ||
|
||
import net.minecraft.world.level.dimension.DimensionType; | ||
import net.minecraft.world.level.storage.WritableLevelData; | ||
|
||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.ModifyArg; | ||
|
||
import java.util.function.Supplier; | ||
|
||
@Mixin(ServerLevel.class) | ||
public abstract class ServerLevelMixin extends Level { | ||
protected ServerLevelMixin(WritableLevelData writableLevelData, ResourceKey<Level> resourceKey, RegistryAccess registryAccess, Holder<DimensionType> holder, Supplier<ProfilerFiller> supplier, boolean bl, boolean bl2, long l, int i) { | ||
super(writableLevelData, resourceKey, registryAccess, holder, supplier, bl, bl2, l, i); | ||
} | ||
|
||
@ModifyArg(method = "tick", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/level/ServerLevel;setDayTime(J)V")) | ||
private long sleepFinishedEvent(long newTime) { | ||
SleepFinishedTimeEvent event = new SleepFinishedTimeEvent((ServerLevel) (Object) this, newTime, getDayTime()); | ||
event.sendEvent(); | ||
return event.getNewTime(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"schemaVersion": 1, | ||
"id": "porting_lib_level_events", | ||
"version": "${version}", | ||
"name": "Porting Lib Level Events", | ||
"description": "Provides common level events for mods." | ||
} |
12 changes: 12 additions & 0 deletions
12
modules/level_events/src/main/resources/porting_lib_level_events.mixins.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"required": true, | ||
"minVersion": "0.8", | ||
"package": "io.github.fabricators_of_create.porting_lib.level.mixin", | ||
"compatibilityLevel": "JAVA_17", | ||
"injectors": { | ||
"defaultRequire": 1 | ||
}, | ||
"mixins": [ | ||
"ServerLevelMixin" | ||
] | ||
} |