-
Notifications
You must be signed in to change notification settings - Fork 0
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
15 changed files
with
622 additions
and
118 deletions.
There are no files selected for viewing
Binary file not shown.
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
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
32 changes: 32 additions & 0 deletions
32
src/main/java/net/nussi/dedicated_applied_energistics/misc/Lifecycle.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,32 @@ | ||
package net.nussi.dedicated_applied_energistics.misc; | ||
|
||
public abstract class Lifecycle { | ||
private boolean isRunning = false; | ||
|
||
public boolean start() { | ||
if(isRunning) return true; | ||
if(onStart()) { | ||
isRunning = true; | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
public boolean stop() { | ||
if(!isRunning) return true; | ||
if(onStop()) { | ||
isRunning = false; | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
protected abstract boolean onStart(); | ||
protected abstract boolean onStop(); | ||
|
||
public boolean isRunning() { | ||
return isRunning; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/net/nussi/dedicated_applied_energistics/misc/LifecycleTickable.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,36 @@ | ||
package net.nussi.dedicated_applied_energistics.misc; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
public abstract class LifecycleTickable extends Tickable { | ||
private boolean isRunning = false; | ||
private Thread thread = new Thread(this); | ||
private AtomicBoolean ticking = new AtomicBoolean(false); | ||
|
||
public boolean start() { | ||
if(isRunning) return true; | ||
|
||
if(!onStart()) return false; | ||
if(!this.startTicking()) return false; | ||
|
||
isRunning = true; | ||
return true; | ||
} | ||
|
||
public boolean stop() { | ||
if(!isRunning) return true; | ||
|
||
if(!this.stopTicking()) return false; | ||
if(!onStop()) return false; | ||
|
||
isRunning = false; | ||
return true; | ||
} | ||
|
||
protected abstract boolean onStart(); | ||
protected abstract boolean onStop(); | ||
|
||
public boolean isRunning() { | ||
return isRunning; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/net/nussi/dedicated_applied_energistics/misc/Tickable.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,59 @@ | ||
package net.nussi.dedicated_applied_energistics.misc; | ||
|
||
import com.mojang.logging.LogUtils; | ||
import org.slf4j.Logger; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
public abstract class Tickable implements Runnable { | ||
private static final Logger LOGGER = LogUtils.getLogger(); | ||
|
||
private Thread thread = new Thread(this); | ||
private AtomicBoolean ticking = new AtomicBoolean(false); | ||
|
||
protected boolean startTicking() { | ||
if(ticking.get()) return true; | ||
|
||
try { | ||
ticking.set(true); | ||
thread.start(); | ||
return true; | ||
} catch (Exception e) { | ||
LOGGER.error("Failed to start thread"); | ||
e.printStackTrace(); | ||
return false; | ||
} | ||
} | ||
|
||
public boolean stopTicking() { | ||
if(!ticking.get()) return true; | ||
|
||
try { | ||
ticking.set(false); | ||
thread.join(1000); | ||
return true; | ||
} catch (Exception e) { | ||
LOGGER.error("Failed to stop thread"); | ||
e.printStackTrace(); | ||
return false; | ||
} | ||
} | ||
|
||
@Override | ||
public void run() { | ||
while (ticking.get()) { | ||
try { | ||
onTick(); | ||
} catch (Exception e) { | ||
LOGGER.error("Failed to tick"); | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
|
||
protected abstract void onTick() throws Exception; | ||
|
||
public boolean isTicking() { | ||
return ticking.get(); | ||
} | ||
} |
Oops, something went wrong.