11package dev .tr7zw .entityculling ;
22
3+ import java .io .File ;
4+ import java .io .IOException ;
5+ import java .nio .charset .StandardCharsets ;
6+ import java .nio .file .Files ;
7+
8+ import com .google .gson .Gson ;
9+ import com .google .gson .GsonBuilder ;
10+ import com .logisticscraft .occlusionculling .OcclusionCullingInstance ;
11+
12+ import net .minecraft .client .Minecraft ;
13+ import net .minecraft .client .entity .EntityPlayerSP ;
14+ import net .minecraft .client .settings .KeyBinding ;
15+ import net .minecraft .util .ChatComponentText ;
16+ import net .minecraft .util .EnumChatFormatting ;
317import net .minecraftforge .common .MinecraftForge ;
418import net .minecraftforge .fml .client .registry .ClientRegistry ;
519import net .minecraftforge .fml .common .Mod ;
6- import net .minecraftforge .fml .common .Mod .EventHandler ;
720import net .minecraftforge .fml .common .event .FMLPostInitializationEvent ;
821import net .minecraftforge .fml .common .eventhandler .SubscribeEvent ;
922import net .minecraftforge .fml .common .gameevent .InputEvent ;
10- import net .minecraftforge .fml .common .gameevent .TickEvent . ClientTickEvent ;
11- import net . minecraftforge . fml . common . gameevent . TickEvent . WorldTickEvent ;
23+ import net .minecraftforge .fml .common .gameevent .TickEvent ;
24+ import org . lwjgl . input . Keyboard ;
1225
1326@ Mod (modid = "entityculling" , name = "EntityCulling" , version = "@VER@" , clientSideOnly = true )
14- public class EntityCullingMod extends EntityCullingModBase {
27+ public class EntityCullingMod {
28+
29+ public static EntityCullingMod instance = new EntityCullingMod ();
30+ public OcclusionCullingInstance culling ;
31+ public boolean debugHitboxes = false ;
32+ public static boolean enabled = true ; // public static to make it faster for the jvm
33+ public CullTask cullTask ;
34+ private Thread cullThread ;
35+ protected KeyBinding keybind = new KeyBinding ("key.entityculling.toggle" , Keyboard .KEY_NONE , "text.entityculling.title" );
36+
37+ public Config config ;
38+ private final File settingsFile = new File ("config" , "entityculling.json" );
39+ private final Gson gson = new GsonBuilder ().setPrettyPrinting ().create ();
40+
41+ //stats
42+ public int renderedBlockEntities = 0 ;
43+ public int skippedBlockEntities = 0 ;
44+ public int renderedEntities = 0 ;
45+ public int skippedEntities = 0 ;
46+ //public int tickedEntities = 0;
47+ //public int skippedEntityTicks = 0;
48+
49+ // TODO: Should probably be using FMLPreInitializationEvent
1550 public EntityCullingMod () {
16- onInitialize ();
17- }
51+ instance = this ;
52+ if (settingsFile .exists ()) {
53+ try {
54+ config = gson .fromJson (new String (Files .readAllBytes (settingsFile .toPath ()), StandardCharsets .UTF_8 ),
55+ Config .class );
56+ } catch (Exception ex ) {
57+ System .out .println ("Error while loading config! Creating a new one!" );
58+ ex .printStackTrace ();
59+ }
60+ }
61+ if (config == null ) {
62+ config = new Config ();
63+ writeConfig ();
64+ } else {
65+ if (ConfigUpgrader .upgradeConfig (config )) {
66+ writeConfig (); // Config got modified
67+ }
68+ }
69+ culling = new OcclusionCullingInstance (config .tracingDistance , new Provider ());
70+ cullTask = new CullTask (culling , config .blockEntityWhitelist );
1871
19- @ Override
20- public void initModloader () {
72+ cullThread = new Thread (cullTask , "CullThread" );
73+ cullThread .setUncaughtExceptionHandler ((thread , ex ) -> {
74+ System .out .println ("The CullingThread has crashed! Please report the following stacktrace!" );
75+ ex .printStackTrace ();
76+ });
77+ cullThread .start ();
2178 }
2279
23- @ EventHandler
80+ @ Mod . EventHandler
2481 public void onPostInit (FMLPostInitializationEvent event ) {
2582 ClientRegistry .registerKeyBinding (keybind );
2683 MinecraftForge .EVENT_BUS .register (this );
2784 }
2885
86+ public void writeConfig () {
87+ if (settingsFile .exists ()) {
88+ settingsFile .delete ();
89+ }
90+ try {
91+ Files .write (settingsFile .toPath (), gson .toJson (config ).getBytes (StandardCharsets .UTF_8 ));
92+ } catch (IOException e1 ) {
93+ e1 .printStackTrace ();
94+ }
95+ }
96+
2997 @ SubscribeEvent
30- public void doClientTick ( ClientTickEvent event ) {
31- this . clientTick () ;
98+ public void onWorldTick ( TickEvent . WorldTickEvent event ) {
99+ cullTask . requestCull = true ;
32100 }
33101
34102 @ SubscribeEvent
35- public void doWorldTick ( WorldTickEvent event ) {
36- this . worldTick () ;
103+ public void onClientTick ( TickEvent . ClientTickEvent event ) {
104+ cullTask . requestCull = true ;
37105 }
38106
39107 @ SubscribeEvent
@@ -45,4 +113,20 @@ public void onKeyInput(InputEvent.KeyInputEvent event) {
45113 public void onMouseInput (InputEvent .MouseInputEvent event ) {
46114 keyBindPressed ();
47115 }
116+
117+ public void keyBindPressed () {
118+ if (keybind .isPressed ()) {
119+ enabled = !enabled ;
120+ EntityPlayerSP player = Minecraft .getMinecraft ().thePlayer ;
121+ if (enabled ) {
122+ if (player != null ) {
123+ player .addChatMessage (new ChatComponentText (EnumChatFormatting .GREEN + "Culling on" ));
124+ }
125+ } else {
126+ if (player != null ) {
127+ player .addChatMessage (new ChatComponentText (EnumChatFormatting .RED + "Culling off" ));
128+ }
129+ }
130+ }
131+ }
48132}
0 commit comments