Skip to content

Commit 0935bd3

Browse files
[edited] applied changes made to 1.18 branch
[edited] bump version for update
1 parent 360d5ed commit 0935bd3

16 files changed

+389
-85
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ Here is a list of supported devices:
4040
- Mouse cursor movements
4141
- Official gamepads such as Xbox controllers or Playstation DualShock are supported
4242
- Knockoff/alternate gamepads (like a Logitech controller) should work
43-
- Joysticks/hotas/racing wheels and other devices are supported, unless they requires some funky drivers in which case I can't do anything for you
43+
- Joysticks/hotas/racing wheels and other devices should be supported, but I have been proven wrong multiple times already, so if it does not you can still try a gamepad/xbox controller emulator.
4444
- WiiMotes are (sadly) not supported
4545

4646
## Remarks
4747

48-
Due to a Create limitation, if a network is updated too many times it will break. This can happen if you connect an axis output to an Adjustable Chain Gearshift, for example. I might do something for this in the future.
48+
Due to a Create limitation, if a network is updated too many times it will break. This can happen if you connect an axis output to an Adjustable Chain Gearshift, for example. This behavior has been disabled since version 1.2.3 of the mod.
4949

5050
The joystick axis are directly read from GLFW. With most if not all gamepads, the +X axis points to the right, and the +Y axis points down.
5151

52-
IF YOU USE A NINTENDO CONTROLLER, YOU MIGHT WANNA TAKE A LOOK [AT THIS](https://github.com/getItemFromBlock/Create-Tweaked-Controllers/issues/1).
52+
IF YOU USE A NINTENDO CONTROLLER, YOU MIGHT WANT TO TAKE A LOOK AT THIS: [https://github.com/getItemFromBlock/Create-Tweaked-Controllers/issues/1](https://github.com/getItemFromBlock/Create-Tweaked-Controllers/issues/1).

build.gradle

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ buildscript {
99
dependencies {
1010

1111
classpath group: 'net.minecraftforge.gradle', name: 'ForgeGradle', version: '5.1.+', changing: true
12+
classpath group: 'org.spongepowered', name: 'mixingradle', version: '0.7-SNAPSHOT'
1213
}
1314
}
1415
// Only edit below this line, the above code adds and enables the necessary things for Forge to be setup.
@@ -17,10 +18,17 @@ plugins {
1718
id 'maven-publish'
1819
}
1920
apply plugin: 'net.minecraftforge.gradle'
21+
apply plugin: 'org.spongepowered.mixin'
22+
23+
mixin {
24+
add sourceSets.main, "${modid}.refmap.json"
25+
26+
config "${modid}.mixins.json"
27+
}
2028

2129
jarJar.enable()
2230

23-
version = '1.20.1-1.2.2'
31+
version = "${modversion}"
2432
group = 'com.getItemFromBlock.create_tweaked_controllers' // http://maven.apache.org/guides/mini/guide-naming-conventions.html
2533
archivesBaseName = 'create_tweaked_controllers'
2634

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ org.gradle.jvmargs=-Xmx3G
44
org.gradle.daemon=false
55

66
modid = create_tweaked_controllers
7-
modversion = 1.20.1-1.2.2
7+
modversion = 1.20.1-1.2.4
88

99
minecraft_version = 1.20.1
1010
minecraft_range = [1.20.1,1.21)

src/main/java/com/getitemfromblock/create_tweaked_controllers/config/ModClientConfig.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class ModClientConfig
1010
public static final ForgeConfigSpec.ConfigValue<Boolean> USE_CUSTOM_MAPPINGS;
1111
public static final ForgeConfigSpec.ConfigValue<Boolean> TOGGLE_MOUSE_FOCUS;
1212
public static final ForgeConfigSpec.ConfigValue<Boolean> AUTO_RESET_MOUSE_FOCUS;
13+
//public static final ForgeConfigSpec.ConfigValue<Boolean> AUTO_DETECT_INPUT_TYPE;
1314
public static final ForgeConfigSpec.ConfigValue<Integer> CONFIG_BUTTON_MAIN_MENU_ROW;
1415
public static final ForgeConfigSpec.ConfigValue<Integer> CONFIG_BUTTON_MAIN_MENU_OFFSET;
1516
public static final ForgeConfigSpec.ConfigValue<Integer> CONFIG_BUTTON_INGAME_MENU_ROW;
@@ -25,6 +26,8 @@ public class ModClientConfig
2526
.define("toggle_mouse_focus", false);
2627
AUTO_RESET_MOUSE_FOCUS = BUILDER.comment("Does the mouse cursor inputs are automatically reset when the controller item is put down, default is true")
2728
.define("auto_reset_mouse_focus", true);
29+
//AUTO_DETECT_INPUT_TYPE = BUILDER.comment("Does the mod will automatically change the controller profile dependind on what is available on your computer, default is true")
30+
// .define("auto_detect_input_type", true);
2831
CONFIG_BUTTON_MAIN_MENU_ROW = BUILDER.comment("Row of the Controller Settings button in the main menu, default is 2")
2932
.defineInRange("config_button_main_menu_row", 2 , 0, 4);
3033
CONFIG_BUTTON_MAIN_MENU_OFFSET = BUILDER.comment("X Offset of the Controller Settings button in the main menu, default is 4")

src/main/java/com/getitemfromblock/create_tweaked_controllers/controller/ControlProfile.java

Lines changed: 182 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,40 @@ public class ControlProfile
2828
public GenericInput[] layout = new GenericInput[25];
2929
public boolean hasJoystickInput = false;
3030
public ArrayList<KeyMapping> duplicatedKeys = new ArrayList<KeyMapping>();
31+
32+
public static final byte CURRENT_VERSION_MAJOR = (byte) 0x01;
33+
public static final byte CURRENT_VERSION_MINOR = (byte) 0x00;
3134

3235
public ControlProfile()
3336
{
37+
//this(ControlType.KEYBOARD_MOUSE);
3438
this(0);
3539
}
3640

41+
public ControlProfile(ControlType type)
42+
{
43+
CheckProfileUpgrade();
44+
Load(type);
45+
Save(type);
46+
}
47+
3748
public ControlProfile(int id)
3849
{
3950
Load(0);
4051
Save(0);
4152
}
4253

54+
public void Load(ControlType type)
55+
{
56+
Load("config/gamepad_profiles/gamepad_profile_" + type.toString(), type);
57+
UpdateProfileData();
58+
}
59+
60+
public void Save(ControlType type)
61+
{
62+
Save("config/gamepad_profiles/gamepad_profile_" + type.toString());
63+
}
64+
4365
public void Load(int id)
4466
{
4567
Load("config/gamepad_profiles/gamepad_profile_" + id);
@@ -51,6 +73,31 @@ public void Save(int id)
5173
Save("config/gamepad_profiles/gamepad_profile_" + id);
5274
}
5375

76+
private void CheckProfileUpgrade()
77+
{
78+
File f = new File("config/gamepad_profiles/gamepad_profile_0");
79+
if(!f.exists() || f.isDirectory())
80+
{
81+
return;
82+
}
83+
Load("config/gamepad_profiles/gamepad_profile_0", ControlType.CUSTOM_0, false);
84+
Save(ControlType.CUSTOM_0);
85+
try
86+
{
87+
f.delete();
88+
}
89+
catch (Exception e)
90+
{
91+
CreateTweakedControllers.error("Error upgrading old controller profile: could not delete old file \"config/gamepad_profiles/gamepad_profile_0\"!");
92+
CreateTweakedControllers.error(e.getMessage());
93+
for (StackTraceElement line : e.getStackTrace())
94+
{
95+
CreateTweakedControllers.error(line.toString());
96+
}
97+
return;
98+
}
99+
}
100+
54101
static final int[] keys =
55102
{
56103
GLFW.GLFW_KEY_SEMICOLON,
@@ -63,20 +110,32 @@ public void Save(int id)
63110
GLFW.GLFW_KEY_Y,
64111
GLFW.GLFW_KEY_T,
65112
GLFW.GLFW_KEY_H,
66-
GLFW.GLFW_KEY_J,
113+
GLFW.GLFW_KEY_ENTER,
67114
GLFW.GLFW_KEY_UP,
68115
GLFW.GLFW_KEY_RIGHT,
69116
GLFW.GLFW_KEY_DOWN,
70117
GLFW.GLFW_KEY_LEFT
71118
};
72119

73-
public void InitDefaultLayout()
120+
public void InitDefaultLayout(ControlType fallbackType)
121+
{
122+
if (fallbackType == ControlType.JOYSTICK)
123+
{
124+
InitDefaultGamepadLayout();
125+
}
126+
else
127+
{
128+
InitDefaultKeyboardLayout();
129+
}
130+
}
131+
132+
public void InitDefaultKeyboardLayout()
74133
{
75134
for (int i = 0; i < 15; i++)
76135
{
77-
if (i == 9 || i == 10)
136+
if (i == 9)
78137
{
79-
layout[i] = new MouseButtonInput(i - 9);
138+
layout[i] = new MouseButtonInput(0);
80139
}
81140
else
82141
{
@@ -114,12 +173,121 @@ else if (i > 22)
114173
}
115174
}
116175

117-
public void Load(String path)
176+
public void InitDefaultGamepadLayout()
177+
{
178+
for (int i = 0; i < 15; i++)
179+
{
180+
layout[i] = new JoystickButtonInput(i);
181+
}
182+
for (int i = 15; i < 25; i++)
183+
{
184+
if (i == 16 || i == 18 || i == 20 || i == 22)
185+
{
186+
layout[i] = new JoystickAxisInput(i - 15, 0.05f, -1.0f);
187+
}
188+
else if (i > 22)
189+
{
190+
layout[i] = new JoystickAxisInput(i - 15, -1.0f, 1.0f);
191+
}
192+
else
193+
{
194+
layout[i] = new JoystickAxisInput(i - 15, 0.05f, 1.0f);
195+
}
196+
}
197+
}
198+
199+
private void Load(String path, ControlType fallbackType)
200+
{
201+
Load(path, fallbackType, true);
202+
}
203+
204+
static final byte[] headerNameData =
205+
{
206+
'C',
207+
'T',
208+
'C',
209+
'P',
210+
'R',
211+
'F',
212+
};
213+
214+
private void Load(String path, ControlType fallbackType, boolean hasHeader)
118215
{
119216
File f = new File(path);
120217
if(!f.exists() || f.isDirectory())
121218
{
122-
InitDefaultLayout();
219+
InitDefaultLayout(fallbackType);
220+
return;
221+
}
222+
else try
223+
{
224+
FileInputStream file = new FileInputStream(f);
225+
DataInputStream buf = new DataInputStream(file);
226+
int version = 0;
227+
if (hasHeader)
228+
{
229+
byte[] header = buf.readNBytes(8);
230+
boolean valid = header.length == 8;
231+
for (int i = 0; i < 6 && valid; i++)
232+
{
233+
if (header[i] != headerNameData[i])
234+
{
235+
valid = false;
236+
}
237+
}
238+
if (!valid)
239+
{
240+
file.close();
241+
throw new IOException("Corrupted Profile Data!");
242+
}
243+
version = 256 * header[6] + header[7];
244+
}
245+
for (int i = 0; i < layout.length; i++)
246+
{
247+
switch (InputType.GetType(buf.readByte()))
248+
{
249+
case NONE:
250+
layout[i] = null;
251+
break;
252+
case JOYSTICK_BUTTON:
253+
layout[i] = new JoystickButtonInput();
254+
break;
255+
case JOYSTICK_AXIS:
256+
layout[i] = new JoystickAxisInput();
257+
break;
258+
case MOUSE_BUTTON:
259+
layout[i] = new MouseButtonInput();
260+
break;
261+
case MOUSE_AXIS:
262+
layout[i] = new MouseAxisInput();
263+
break;
264+
case KEYBOARD_KEY:
265+
layout[i] = new KeyboardInput();
266+
break;
267+
default:
268+
throw new IOException("Corrupted Profile Data!");
269+
}
270+
if (layout[i] != null) layout[i].Deserialize(buf);
271+
}
272+
file.close();
273+
}
274+
catch (IOException e)
275+
{
276+
CreateTweakedControllers.error("Error loading controller profile \""+path+"\"!");
277+
for (StackTraceElement line : e.getStackTrace())
278+
{
279+
CreateTweakedControllers.error(line.toString());
280+
}
281+
return;
282+
}
283+
}
284+
285+
private void Load(String path)
286+
{
287+
File f = new File(path);
288+
if(!f.exists() || f.isDirectory())
289+
{
290+
InitDefaultLayout(ControlType.KEYBOARD_MOUSE);
123291
return;
124292
}
125293
else try
@@ -179,6 +347,14 @@ public void Save(String path)
179347
f.createNewFile();
180348
FileOutputStream file = new FileOutputStream(f);
181349
DataOutputStream buf = new DataOutputStream(file);
350+
/*
351+
for (int i = 0; i < headerNameData.length; i++)
352+
{
353+
buf.writeByte(headerNameData[i]);
354+
}
355+
buf.writeByte(CURRENT_VERSION_MAJOR);
356+
buf.writeByte(CURRENT_VERSION_MINOR);
357+
*/
182358
for (int i = 0; i < layout.length; i++)
183359
{
184360
if (layout[i] != null)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.getitemfromblock.create_tweaked_controllers.controller;
2+
3+
import com.getitemfromblock.create_tweaked_controllers.input.JoystickInputs;
4+
5+
public enum ControlType
6+
{
7+
KEYBOARD_MOUSE,
8+
JOYSTICK,
9+
CUSTOM_0,
10+
CUSTOM_1;
11+
12+
public boolean IsAdapted()
13+
{
14+
switch (this)
15+
{
16+
case KEYBOARD_MOUSE:
17+
return !JoystickInputs.HasJoystick();
18+
case JOYSTICK:
19+
return JoystickInputs.HasJoystick();
20+
default:
21+
return true;
22+
}
23+
}
24+
}

src/main/java/com/getitemfromblock/create_tweaked_controllers/controller/TweakedControlsUtil.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ public class TweakedControlsUtil
1414
private static boolean lastFocusKeyState = false;
1515
private static boolean isFocusActive = false;
1616
private static boolean wasFocusActive = false;
17+
private static ControlType controlType = ControlType.KEYBOARD_MOUSE;
18+
19+
public static ControlType GetActiveProfileType()
20+
{
21+
return controlType;
22+
}
1723

1824
public static void FreeFocus()
1925
{
@@ -22,6 +28,26 @@ public static void FreeFocus()
2228
wasFocusActive = false;
2329
}
2430

31+
public static void SelectProfileType(ControlType type)
32+
{
33+
profile.Load(type);
34+
controlType = type;
35+
}
36+
37+
private static void BackgroundUpdate()
38+
{
39+
//if (!ModClientConfig.AUTO_DETECT_INPUT_TYPE.get() || controlType.IsAdapted()) return;
40+
if (JoystickInputs.HasJoystick())
41+
{
42+
controlType = ControlType.JOYSTICK;
43+
}
44+
else
45+
{
46+
controlType = ControlType.KEYBOARD_MOUSE;
47+
}
48+
profile.Load(controlType);
49+
}
50+
2551
public static void GuiUpdate()
2652
{
2753
MouseCursorHandler.Update();
@@ -96,6 +122,7 @@ public static void Update(boolean useFullPrec)
96122
{
97123
if (ModClientConfig.USE_CUSTOM_MAPPINGS.get())
98124
{
125+
//BackgroundUpdate();
99126
HandleMouseKeyBinds();
100127
if (profile.hasJoystickInput)
101128
{
@@ -164,4 +191,4 @@ public static void FillGamepadInputs(boolean useFullPrec)
164191
}
165192
}
166193
}
167-
}
194+
}

0 commit comments

Comments
 (0)